@bhugueney/

rotatify

Python

turn any image into a rotating gif

fork
loading
Files
  • main.py
  • pep.png
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from PIL import Image
import os
import re
import os.path as path
import mimetypes
from pathlib import PurePath

def make_it_spin(img_path):
  try:
    img = Image.open(img_path)
  except:
    print("try uploading an image")
    exit(1)

  img = img.convert(mode='RGBA')
  
  sequence = []
  
  for rot in range(0, 360, 10):
    rotated = Image.new('RGBA', img.size, color=(255, 255, 255, 255))
    rotated = Image.alpha_composite(rotated, img.rotate(rot))
    sequence.append(rotated)
  
  newPath = 'rotating_' + PurePath(img_path).stem + '.gif'
  
  sequence[0].save(
    newPath,
    save_all=True,
    append_images=sequence[1:],
    loop=0,
    duration=33
  )
  
  print('turned "%s" into a gif called "%s"' % (img_path, newPath))

for f in os.listdir('.'):
  t =  mimetypes.guess_type(f)[0]
  if path.isfile(f) and \
      t is not None and \
      not re.search('.+_rotating.gif', f) \
      and re.search('image/.+', t):

    make_it_spin(f)