Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, glob, time
- from PIL import Image
- import numpy as np
- # Set up directories
- script_dir = os.path.dirname(os.path.abspath(__file__))
- root_dir = os.path.dirname(script_dir)
- textures_dir = os.path.abspath(root_dir + '/Textures')
- # Loop .png files
- for png_file_path in glob.glob(os.path.abspath(textures_dir + '/**/*.png')):
- if png_file_path.endswith('_Mask.png'):
- continue
- pcx_file_path = png_file_path.replace('.png', '.pcx')
- if os.path.isfile(pcx_file_path):
- png_file_mtime = os.path.getmtime(png_file_path)
- pcx_file_mtime = os.path.getmtime(pcx_file_path)
- pcx_image = Image.open(pcx_file_path)
- if png_file_mtime < pcx_file_mtime:
- continue
- # Load the image
- png_image = Image.open(png_file_path)
- # Apply the mask, if it exists
- mask_file_path = png_file_path.replace('.png', '_Mask.png')
- if os.path.isfile(mask_file_path):
- mask_image = Image.open(mask_file_path)
- mask_image = mask_image.quantize(colors=2)
- png_image.paste(mask_image)
- # Reduce the amount of colours to 255
- png_image = png_image.quantize(colors=255)
- # Make #ff00ff the first colour of the palette
- palette = png_image.getpalette()
- first_entry = palette[:3]
- last_entry = palette[-3:]
- new_palette = [ *last_entry, *palette[3:765], *first_entry ]
- na = np.array(png_image)
- mask0 = na == 0
- mask255 = na == 255
- na[mask0] = 255
- na[mask255] = 0
- new_png_image = Image.fromarray(na)
- new_png_image.putpalette(new_palette)
- png_image.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement