Advertisement
Guest User

Untitled

a guest
Sep 16th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. import os, glob, time
  2.  
  3. from PIL import Image
  4. import numpy as np
  5.  
  6. # Set up directories
  7. script_dir = os.path.dirname(os.path.abspath(__file__))
  8. root_dir = os.path.dirname(script_dir)
  9. textures_dir = os.path.abspath(root_dir + '/Textures')
  10.  
  11. # Loop .png files
  12. for png_file_path in glob.glob(os.path.abspath(textures_dir + '/**/*.png')):
  13.     if png_file_path.endswith('_Mask.png'):
  14.         continue
  15.  
  16.     pcx_file_path = png_file_path.replace('.png', '.pcx')
  17.  
  18.     if os.path.isfile(pcx_file_path):
  19.         png_file_mtime = os.path.getmtime(png_file_path)
  20.         pcx_file_mtime = os.path.getmtime(pcx_file_path)
  21.    
  22.         pcx_image = Image.open(pcx_file_path)
  23.  
  24.         if png_file_mtime < pcx_file_mtime:
  25.             continue
  26.  
  27.     # Load the image
  28.     png_image = Image.open(png_file_path)
  29.  
  30.     # Apply the mask, if it exists
  31.     mask_file_path = png_file_path.replace('.png', '_Mask.png')
  32.  
  33.     if os.path.isfile(mask_file_path):
  34.         mask_image = Image.open(mask_file_path)
  35.         mask_image = mask_image.quantize(colors=2)
  36.         png_image.paste(mask_image)
  37.  
  38.     # Reduce the amount of colours to 255
  39.     png_image = png_image.quantize(colors=255)
  40.    
  41.     # Make #ff00ff the first colour of the palette
  42.     palette = png_image.getpalette()
  43.     first_entry = palette[:3]
  44.     last_entry = palette[-3:]
  45.     new_palette = [ *last_entry, *palette[3:765], *first_entry ]
  46.    
  47.     na = np.array(png_image)
  48.     mask0 = na == 0
  49.     mask255 = na == 255
  50.     na[mask0] = 255
  51.     na[mask255] = 0
  52.    
  53.     new_png_image = Image.fromarray(na)
  54.     new_png_image.putpalette(new_palette)
  55.  
  56.     png_image.show()
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement