1+ from PIL import Image ,ImageSequence
2+ import cv2
3+ import numpy as np
4+
5+ import re
6+ from pixel_transform import *
7+
8+ def gif_edit (path , k = 16 , scale = 1 , blur = 0 , erode = 0 , alpha = True , to_tw = False , dither = False , saturation = 0 , contrast = 0 ):
9+ gif = Image .open (path )
10+ path .replace ('\\ ' , '/' )
11+ file_name = re .split ("/|\." , path )[- 2 ]
12+ file_locat = path .split (file_name + '.gif' )[0 ]
13+
14+ img_list = []
15+ for frame in ImageSequence .Iterator (gif ):
16+ # frame = frame.convert('RGBA')
17+ # opencv_img = np.array(frame, dtype=np.uint8)
18+ # opencv_img = cv2.cvtColor(opencv_img, cv2.COLOR_RGBA2BGRA)
19+
20+ # edit area
21+ frame = transform (frame , k = k , scale = scale , blur = blur , erode = erode , alpha = alpha , to_tw = to_tw , dither = dither , saturation = saturation , contrast = contrast )
22+
23+ img_list .append (frame )
24+
25+ # output list
26+ output = []
27+ for i in img_list :
28+ img = i
29+ img = cv2 .cvtColor (img , cv2 .COLOR_BGRA2RGBA )
30+ # since OpenCV is BGRA, need to change to RGBA
31+ img = Image .fromarray (img )
32+ # turn into PIL format
33+ img = img .convert ('RGB' )
34+ # turn into RGB ( if RGBA will turn black into alpha )
35+ output .append (img )
36+ # add to output
37+
38+ # save as gif
39+ # output[0].save(file_locat + file_name + 'edited' + ".gif", save_all=True, append_images=output[1:], duration=200, loop=0, disposal=0)
40+ output [0 ].save (file_name + 'edited' + ".gif" , save_all = True , append_images = output [1 :], duration = 100 , loop = 0 , disposal = 0 )
41+ return img_list [0 ]
42+
43+ def show_gif (img_list ):
44+ # show gif
45+ loop = True
46+ while loop :
47+ for i in img_list :
48+ cv2 .imshow ('gif' , i )
49+ if cv2 .waitKey (200 ) == ord ('q' ):
50+ loop = False
51+ break
52+ cv2 .destroyAllWindows ()
0 commit comments