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