-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgif.py
66 lines (63 loc) · 2.43 KB
/
gif.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import sys
import numpy as np
from PIL import Image, ImageChops
from moviepy.editor import VideoFileClip
SOURCE_FOLDER = sys.argv[-2]
VIDEO_MODE = int(sys.argv[-1])
TRIM_GROUP_MODE = False
''' Trim all images in the SOURCE_FOLDER by the common minimum bbox '''
def trim_group(imgs):
bboxs = []
for img in imgs:
bg = Image.new(img.mode, img.size, img.getpixel((0,0)))
diff = ImageChops.difference(img, bg)
bbox = diff.getbbox()
bboxs.append(bbox)
bboxs = np.array(bboxs)
common_bbox = np.zeros((4))
common_bbox[0] = np.min(bboxs, axis=0)[0]
common_bbox[1] = np.min(bboxs, axis=0)[1]
common_bbox[2] = np.max(bboxs, axis=0)[2]
common_bbox[3] = np.max(bboxs, axis=0)[3]
imgs_cropped = []
for img in imgs:
img = img.crop(common_bbox)
imgs_cropped.append(img)
return imgs_cropped
if __name__ == "__main__":
if VIDEO_MODE:
''' Create gif from videos '''
file_names = os.listdir(SOURCE_FOLDER)
for file_name in file_names:
if file_name.endswith(('.mp4','.mov','.avi','.webm')):
file_path = os.path.join(SOURCE_FOLDER, file_name)
video = VideoFileClip(file_path)
# video = video.subclip((1,00.00),(1,30.00)) # trim the video by start time and end time
# video = video.resize(0.5) # resize the video
# video = video.speedx(1.5) # change the speed of the video
video.write_gif(os.path.join(SOURCE_FOLDER, file_name.split('.')[0]+".gif"), fps=15)
else:
''' Create gif from a set of images '''
images = []
file_names = os.listdir(SOURCE_FOLDER)
file_names.sort()
for file_name in file_names:
if file_name.endswith(('.png','.jpg','.jpeg')):
file_path = os.path.join(SOURCE_FOLDER, file_name)
image = Image.open(file_path)
''' Keep transparency '''
if image.mode == 'RGBA':
alpha = image.getchannel('A')
# Convert the image into P mode but only use 255 colors in the palette out of 256
image = image.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255)
# Set all pixel values below 128 to 255 , and the rest to 0
mask = Image.eval(alpha, lambda a: 255 if a <= 128 else 0)
# Paste the color of index 255 and use alpha as a mask
image.paste(255, mask)
# The transparency index is 255
image.info['transparency'] = 255
images.append(image)
if TRIM_GROUP_MODE:
images = trim_group(images)
images[0].save(os.path.join(SOURCE_FOLDER, file_names[0].split('.')[0]+".gif"), save_all=True, append_images=images[1:], duration=300, loop=0, disposal=2)