yum-archive/Tooner

A toon shader for Unity's BIRP.

git clone https://git.yummers.dev/yum-archive/Tooner

yumImplement surface stable fractal ditheringfb26b02

master
3.7 KiB97 linesraw
1"""
2Shift image to corner, wrapping it toroidally.
3"""
4
5from PIL import Image
6import numpy as np
7import argparse
8import os
9
10def shift_to_corner(img, shift_side=True, shift_up=True):
11    """
12    Shifts an image to edges with toroidal wrapping based on specified directions.
13    
14    Args:
15        img (PIL.Image): Input Pillow image
16        shift_side (bool): Whether to shift horizontally to the right edge
17        shift_up (bool): Whether to shift vertically to the top edge
18    
19    Returns:
20        PIL.Image: Shifted image
21    """
22    # Convert image to numpy array
23    img_array = np.array(img)
24    
25    # Get dimensions
26    height, width = img_array.shape[:2]
27    half_height = height // 2
28    half_width = width // 2
29    
30    # Create new array for the shifted image
31    shifted = np.zeros_like(img_array)
32    
33    if shift_side and shift_up:
34        # Original behavior - shift to upper right corner
35        shifted[half_height:, half_width:] = img_array[:half_height, :half_width]    # Q1 -> BR
36        shifted[half_height:, :half_width] = img_array[:half_height, half_width:]    # Q2 -> BL
37        shifted[:half_height, half_width:] = img_array[half_height:, :half_width]    # Q3 -> TR
38        shifted[:half_height, :half_width] = img_array[half_height:, half_width:]    # Q4 -> TL
39    elif shift_side:
40        # Only shift horizontally to right
41        shifted[:, half_width:] = img_array[:, :half_width]    # Left half -> Right
42        shifted[:, :half_width] = img_array[:, half_width:]    # Right half -> Left
43    elif shift_up:
44        # Only shift vertically to top
45        shifted[half_height:, :] = img_array[:half_height, :]  # Top half -> Bottom
46        shifted[:half_height, :] = img_array[half_height:, :]  # Bottom half -> Top
47    else:
48        # No shift, return original image
49        shifted = img_array.copy()
50    
51    # Convert back to PIL Image and return
52    return Image.fromarray(shifted)
53
54def shift_to_corner_from_file(image_path, output_path, shift_side=True, shift_up=True):
55    """
56    Wrapper function that shifts an image file to edges with toroidal wrapping.
57    
58    Args:
59        image_path (str): Path to the input image
60        output_path (str): Path where the shifted image will be saved
61        shift_side (bool): Whether to shift horizontally to the right edge
62        shift_up (bool): Whether to shift vertically to the top edge
63    
64    Returns:
65        PIL.Image: Shifted image
66    """
67    img = Image.open(image_path)
68    result = shift_to_corner(img, shift_side=shift_side, shift_up=shift_up)
69    result.save(output_path)
70    return result
71
72def get_output_path(input_path):
73    """
74    Generate output path by adding '_shifted' before the file extension.
75    
76    Args:
77        input_path (str): Path to the input image
78    Returns:
79        str: Path for the output image
80    """
81    base, ext = os.path.splitext(input_path)
82    return f"{base}_shifted{ext}"
83
84if __name__ == "__main__":
85    parser = argparse.ArgumentParser(description='Shift an image to the corner with toroidal wrapping.')
86    parser.add_argument('input_image', help='Path to the input image file')
87    parser.add_argument('--no-side', action='store_false', dest='shift_side',
88                      help='Disable horizontal shifting (default: enabled)')
89    parser.add_argument('--no-up', action='store_false', dest='shift_up',
90                      help='Disable vertical shifting (default: enabled)')
91    
92    args = parser.parse_args()
93    output_path = get_output_path(args.input_image)
94    
95    shift_to_corner_from_file(args.input_image, output_path, 
96                             shift_side=args.shift_side, 
97                             shift_up=args.shift_up)