Last active
August 29, 2015 14:01
-
-
Save ethanal/0bc539132a7e798b4ec5 to your computer and use it in GitHub Desktop.
Script to to optimize images for maximum jpeg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cStringIO as StringIO | |
from random import randint | |
import sys | |
from PIL import Image | |
import wand.image | |
input_filepath = sys.argv[1] | |
img = Image.open(input_filepath) | |
star = Image.open("star.png") | |
frames = [] | |
open_buffs = [] | |
d = 10 | |
inc = 2 | |
transform = (range(0, d, inc) + range(-d, d, inc)[::-1] + range(-d, 0, inc)) | |
frame_number = 0 | |
star_positions = None | |
star_rotations = None | |
num_stars = 10 | |
for d in transform: | |
t_img = img.rotate(d) | |
if frame_number % 2 == 0: | |
star_positions = [tuple((randint(0, t_img.size[i] - star.size[i]) for i in range(2))) for i in range(num_stars)] | |
star_rotations = [randint(0, 360) for i in range(num_stars)] | |
for s in range(num_stars): | |
t_star = star.rotate(star_rotations[s]) | |
t_img.paste(t_star, star_positions[s], mask=t_star) | |
frame_number += 1 | |
buff = StringIO.StringIO() | |
t_img.save(buff, "JPEG", quality=10) | |
buff.seek(0) | |
frames.append(Image.open(buff)) | |
open_buffs.append(buff) | |
gif = wand.image.Image(width=img.size[0], height=img.size[1]) | |
for frame in frames[::-1]: | |
f_buff = StringIO.StringIO() | |
frame.save(f_buff, "JPEG") | |
gif.sequence.insert(0, wand.image.Image(blob=f_buff.getvalue())) | |
f_buff.close() | |
gif.save(filename=input_filepath + "_gif") | |
[buff.close() for buff in open_buffs] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Download from http://i.imgur.com/B41UerN.png |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment