Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- import os, sys
- # add the file name as the 1st argument to the script
- imgpath = sys.argv[1]
- img = Image.open(imgpath).convert("RGB")
- w, h = img.size
- # truncate so w and h are divisible by 2
- w = w - (w % 2)
- h = h - (h % 2)
- img = img.crop((0, 0, w, h))
- pixels = img.load()
- # halve the dimensions
- sw, sh = w // 2, h // 2
- # image array
- simgarr = [Image.new("RGB", (sw, sh)) for _ in range(4)]
- spixelsarr = [simg.load() for simg in simgarr]
- for y in range(h):
- for x in range(w):
- if x % 2: # x is 1
- if y % 2: # y is 1
- spixelsarr[3][x//2, y//2] = pixels[x, y]
- else: # y is 0
- spixelsarr[1][x//2, y//2] = pixels[x, y]
- else: # x is 0
- if y % 2: # y is 1
- spixelsarr[2][x//2, y//2] = pixels[x, y]
- else: # y is 0
- spixelsarr[0][x//2, y//2] = pixels[x, y]
- # needed for good gif quality
- simgarr = [x.quantize(dither=Image.NONE) for x in simgarr]
- outpath = f"{os.path.splitext(imgpath)[0]}_animated.gif"
- simgarr[0].save(outpath, save_all=True, append_images=simgarr[1: ], optimize=False, duration=60, loop=0, quality=95)
- os.system(outpath)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement