GDavila commited on
Commit
a85e64f
1 Parent(s): 1ef58b5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ color_step = st.slider('color_step', value=10, min_value=1, max_value=179, step=1)
4
+
5
+ #duration of each frame of the gif in milliseconds
6
+ duration_parameter = st.slider('duration_parameter aka duration of each frame of the gif in milliseconds', value=10, min_value=1, max_value=2000, step=10)
7
+
8
+ #Loop parameter = number of times gif loops. 0 = loops infinitely.
9
+ loop_parameter = st.slider('Loop parameter aka number of times gif loops', value=0, min_value=0, max_value=10, step=1)
10
+
11
+
12
+ if color_step == 0:
13
+ my_hue_list = [0]
14
+ else:
15
+ my_hue_list = list( range(0, 180, color_step) ) #Color step basically gives step range of this list, ie if color_step = 2 then it is [0,2,4,6,....,178]
16
+
17
+ import cv2
18
+ import numpy as np
19
+
20
+ user_image_name = "/content/input_image.png"
21
+
22
+ # load image with alpha channel
23
+ img = cv2.imread( user_image_name , cv2.IMREAD_UNCHANGED)
24
+
25
+ # extract alpha channel
26
+ #alpha = img[:,:,3]
27
+
28
+ # extract bgr channels
29
+ bgr = img[:,:,0:3]
30
+
31
+ # convert to HSV
32
+ hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
33
+ #h = hsv[:,:,0]
34
+ #s = hsv[:,:,1]
35
+ #v = hsv[:,:,2]
36
+ h,s,v = cv2.split(hsv)
37
+
38
+
39
+ if color_step == 0:
40
+ my_hue_list = [0]
41
+ else:
42
+ my_hue_list = list( range(0, 180, color_step) ) #Color step basically gives step range of this list, ie if color_step = 2 then it is [0,2,4,6,....,178]
43
+ #180 at end means highest it can go is 179 (same as hue param )
44
+ #including 0 makes original image part of the outputs/gif
45
+ print(my_hue_list)
46
+
47
+ #H,S,V = Hue , Saturation, Value (ie color value) parameters
48
+ #Hue has range [0,179] , Saturation [0,255] , Value [0,255]
49
+
50
+ for i in my_hue_list:
51
+ # modify hue channel by adding difference and modulo 180 (modulo because hue parameter only goes up to index 180, shouldn't exceed that )
52
+ hnew = np.mod(h + i, 180).astype(np.uint8) #<<<<<<<<<<<<<<<< where the iter comes in
53
+
54
+ # recombine channels
55
+ hsv_new = cv2.merge([hnew,s,v])
56
+
57
+ # convert back to bgr
58
+ bgr_new = cv2.cvtColor(hsv_new, cv2.COLOR_HSV2BGR)
59
+
60
+ # put alpha back into bgr_new
61
+ #bgra = cv2.cvtColor(bgr_new, cv2.COLOR_BGR2BGRA)
62
+ #bgra[:,:,3] = alpha
63
+
64
+ # save output AS FILE LABELED BY ITERABLE
65
+ output_filename = 'output_bgr_new_' + str(i) +'.png' #<<<<<<<<<<<<<<<< where the iter comes in
66
+ cv2.imwrite(output_filename, bgr_new)
67
+
68
+
69
+ '''for this demo prob need to retain image objects bgr_new in an img_array by appending them to that then build them into a gif from the array'''
70
+
71
+
72
+ for i in my_hue_list:
73
+ output_filename = 'output_bgr_new_' + str(i) +'.png' #<<<<<<<<<<<<<<<< where the iter comes in
74
+ Image(filename='/main_outputs/' + output_filename)
75
+
76
+ # filepaths
77
+ fp_in = "/main_outputs/*.png"
78
+ fp_out = "/" + original_filename + "_HueShiftGIF_color_step_" + str(color_step) + "_duration_" + str(duration_parameter) + "_loop_" + str(loop_parameter) + ".gif"
79
+
80
+ # https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
81
+ img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
82
+ img.save(fp=fp_out, format='GIF', append_images=imgs,
83
+ save_all=True, duration=duration_parameter, loop=loop_parameter)
84
+
85
+
86
+
87
+ st.image(fp_out)