44from os import path
55import requests
66import arg_handler
7- from pygifsicle import optimize
87
9- my_arg_handler = arg_handler . rgbif_args ()
8+ input_gif_path = "input.gif"
109
11- input_gif_path = my_arg_handler . input_path
10+ output_gif_path = "output.gif"
1211
13- output_gif_path = my_arg_handler . output_path
12+ operator = "color"
1413
15- operator = my_arg_handler . operator
14+ emoji = False
1615
17- emoji = my_arg_handler . emoji
16+ minimum_frames = 24
1817
19- minimum_frames = my_arg_handler .minimum_frames
20-
21- intensity = my_arg_handler .intensity
22-
23- imgs = ImageList ()
24- single_gif = Image ()
18+ intensity = 1.0
2519
2620def isLocal (uri ):
2721 if path .isfile (uri ):
@@ -34,79 +28,90 @@ def scrape(imgURL):
3428 img_data = Blob (res .content )
3529 return img_data
3630
37- def extendImageToMinimumFrames (img ):
31+ def extendImageToMinimumFrames (img , min_frames ):
3832 extendedImg = ImageList ()
39- while len (extendedImg ) < minimum_frames :
33+ while len (extendedImg ) < min_frames :
4034 for frame in img :
4135 extendedImg .append (frame )
42- #print(len(extendedImg))
4336 return extendedImg
4437
45- def applyRGB (operator_string ):
46- filterlist = ImageList ()
47- total_frames = len (imgs )
48- #print(total_frames)
49- for (i ,frame ) in enumerate (imgs ):
38+ def rgbFrames (src_imagelist ,src_image_size ,operator_string ,intensity ):
39+ print (intensity ,operator_string )
40+ processed_imagelist = ImageList ()
41+ total_frames = len (src_imagelist )
42+
43+ for (i ,frame ) in enumerate (src_imagelist ):
5044 percent = float (i )/ float (total_frames - 1 )
51- sH = 0.5 * math .sin (percent * math .pi * 2 )+ 0.5
52- sS = 0.5 * intensity * math .sin (percent * math .pi * 2 * 10 )+ 0.5
53- sL = 0.25 * math .sin (percent * math .pi * 4 )+ 0.5
54- #
55- h = sH
56- s = sS
57- l = sL
45+ h = 0.5 * math .sin (percent * math .pi * 2 )+ 0.5
46+ s = intensity * (0.5 * math .sin (percent * math .pi * 2 * 10 )+ 0.5 )
47+ l = 0.25 * math .sin (percent * math .pi * 4 )+ 0.5
48+
5849 c = ColorHSL (h ,s ,l )
5950
60- filter = Image (Geometry (single_gif .columns (),single_gif .rows ()),c )
51+ filter = Image (Geometry (src_image_size .columns (),src_image_size .rows ()),c )
6152
6253 if operator_string == "color" :
6354 frame .composite (filter , 0 ,0 ,CompositeOperator .ColorizeCompositeOp )
6455 elif operator_string == "hue" :
6556 frame .composite (filter , 0 ,0 ,CompositeOperator .HueCompositeOp )
6657 elif operator_string == "overlay" :
6758 frame .composite (filter , 0 ,0 ,CompositeOperator .OverlayCompositeOp )
68- elif operator_string == "diff " :
59+ elif operator_string == "difference " :
6960 frame .composite (filter , 0 ,0 ,CompositeOperator .DifferenceCompositeOp )
7061 elif operator_string == "dissolve" :
7162 frame .composite (filter , 0 ,0 ,CompositeOperator .DissolveCompositeOp )
7263 elif operator_string == "multiply" :
7364 frame .composite (filter , 0 ,0 ,CompositeOperator .MultiplyCompositeOp )
7465
7566 frame .transparent (c )
76- filterlist .append (frame )
77- return filterlist
67+ processed_imagelist .append (frame )
7868
79- if __name__ == "__main__" :
80- if not isLocal (input_gif_path ):
81- scraped_data = scrape (input_gif_path )
82- single_gif = Image (scraped_data )
83- imgs .readImages (scraped_data )
84- else :
85- single_gif = Image (input_gif_path )
86- imgs .readImages (input_gif_path )
87-
88- if len (imgs ) < minimum_frames :
89- imgs = extendImageToMinimumFrames (imgs )
90-
91- if operator == "default" :
92- new_gif_color = applyRGB ("color" )
93- new_gif_hue = applyRGB ("hue" )
94- #
95- if emoji :
96- new_gif_color .scaleImages (Geometry (48 ,48 ))
97- new_gif_hue .scaleImages (Geometry (48 ,48 ))
98- #
99- new_gif_color .writeImages (r"\Desktop\output_color.gif" )
100- new_gif_hue .writeImages (r"\Desktop\output_hue.gif" )
101- #
102- #optimize(r"\Desktop\output_color.gif")
103- #optimize(r"\Desktop\output_hue.gif")
69+ return processed_imagelist
70+
71+ def creatRGBIF (uri = "input.gif" ,min_frames = 24 ,operation = "color" ,intensity = 1 ,emoji = False ,output_path = "output.gif" ):
72+ input_gif_imagelist = ImageList ()
73+ input_gif_single_frame = Image ()
74+
75+ #check if input path directs to an existing local file, else interpret the path as an url and retrieve the gif data online
76+ if not isLocal (uri ):
77+ scraped_data = scrape (uri )
78+ input_gif_single_frame = Image (scraped_data )
79+ input_gif_imagelist .readImages (scraped_data )
10480 else :
105- new_gif = applyRGB (operator )
106- #
107- if emoji :
108- new_gif .scaleImages (Geometry (48 ,48 ))
109- #
110- new_gif .writeImages (output_gif_path )
111- #
112- #optimize(output_gif_path)
81+ input_gif_single_frame = Image (uri )
82+ input_gif_imagelist .readImages (uri )
83+
84+ #extend the gif till it reaches the minimum amount of frames
85+ if len (input_gif_imagelist ) < min_frames :
86+ input_gif_imagelist = extendImageToMinimumFrames (input_gif_imagelist ,min_frames )
87+
88+ output_gif_imagelist = rgbFrames (input_gif_imagelist ,input_gif_single_frame ,operation ,intensity )
89+
90+ if emoji :
91+ output_gif_imagelist .scaleImages (Geometry (48 ,48 ))
92+
93+ if not output_path .endswith (".gif" ):
94+ output_path += ".gif"
95+
96+ output_gif_imagelist .writeImages (output_path )
97+
98+
99+
100+ if __name__ == "__main__" :
101+
102+ #if this script is ran in a console, assign command line arguments
103+ my_arg_handler = arg_handler .rgbif_args ()
104+
105+ input_gif_path = my_arg_handler .input_path
106+
107+ output_gif_path = my_arg_handler .output_path
108+
109+ operator = my_arg_handler .operator
110+
111+ emoji = my_arg_handler .emoji
112+
113+ minimum_frames = my_arg_handler .minimum_frames
114+
115+ intensity = my_arg_handler .intensity
116+ #
117+ creatRGBIF (uri = input_gif_path ,min_frames = minimum_frames ,operation = operator ,intensity = intensity ,emoji = emoji ,output_path = output_gif_path )
0 commit comments