Advertisement
Guest User

flickering_pilotredsun

a guest
May 21st, 2024
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | Software | 0 0
  1. from PIL import Image
  2. import os, sys
  3.  
  4. # add the file name as the 1st argument to the script
  5. imgpath = sys.argv[1]
  6. img = Image.open(imgpath).convert("RGB")
  7.  
  8. w, h = img.size
  9.  
  10. # truncate so w and h are divisible by 2
  11. w = w - (w % 2)
  12. h = h - (h % 2)
  13. img = img.crop((0, 0, w, h))
  14. pixels = img.load()
  15.  
  16. # halve the dimensions
  17. sw, sh = w // 2, h // 2
  18.  
  19. # image array
  20. simgarr = [Image.new("RGB", (sw, sh)) for _ in range(4)]
  21. spixelsarr = [simg.load() for simg in simgarr]
  22. for y in range(h):
  23.     for x in range(w):
  24.         if x % 2: # x is 1
  25.             if y % 2: # y is 1
  26.                 spixelsarr[3][x//2, y//2] = pixels[x, y]
  27.             else: # y is 0
  28.                 spixelsarr[1][x//2, y//2] = pixels[x, y]
  29.         else: # x is 0
  30.             if y % 2: # y is 1
  31.                 spixelsarr[2][x//2, y//2] = pixels[x, y]
  32.             else: # y is 0
  33.                 spixelsarr[0][x//2, y//2] = pixels[x, y]
  34.    
  35. # needed for good gif quality    
  36. simgarr = [x.quantize(dither=Image.NONE) for x in simgarr]
  37.  
  38. outpath = f"{os.path.splitext(imgpath)[0]}_animated.gif"
  39. simgarr[0].save(outpath, save_all=True, append_images=simgarr[1: ], optimize=False, duration=60, loop=0, quality=95)
  40. os.system(outpath)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement