Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,7 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
|
3 |
color_step = st.slider('color_step', value=10, min_value=1, max_value=179, step=1)
|
4 |
|
@@ -14,78 +17,81 @@ if color_step == 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 |
-
|
18 |
-
import numpy as np
|
19 |
|
20 |
user_image_name = st.file_uploader("upload your image", type=['png', 'jpg'], accept_multiple_files=False)
|
21 |
|
22 |
if user_image_name is not None:
|
23 |
-
st.image(
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
#alpha
|
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 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
''
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
#
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
|
6 |
color_step = st.slider('color_step', value=10, min_value=1, max_value=179, step=1)
|
7 |
|
|
|
17 |
else:
|
18 |
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]
|
19 |
|
20 |
+
|
|
|
21 |
|
22 |
user_image_name = st.file_uploader("upload your image", type=['png', 'jpg'], accept_multiple_files=False)
|
23 |
|
24 |
if user_image_name is not None:
|
25 |
+
st.image(user_image_object )
|
26 |
+
|
27 |
+
user_image_name = "input_image.png"
|
28 |
+
|
29 |
+
#re-encode for streamlit interface
|
30 |
+
user_image_object.save(user_image_name )
|
31 |
+
|
32 |
+
# load image with alpha channel
|
33 |
+
img = cv2.imread( user_image_name , cv2.IMREAD_UNCHANGED)
|
34 |
+
|
35 |
+
# extract alpha channel
|
36 |
+
#alpha = img[:,:,3]
|
37 |
+
|
38 |
+
# extract bgr channels
|
39 |
+
bgr = img[:,:,0:3]
|
40 |
+
|
41 |
+
# convert to HSV
|
42 |
+
hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
|
43 |
+
#h = hsv[:,:,0]
|
44 |
+
#s = hsv[:,:,1]
|
45 |
+
#v = hsv[:,:,2]
|
46 |
+
h,s,v = cv2.split(hsv)
|
47 |
+
|
48 |
+
|
49 |
+
if color_step == 0:
|
50 |
+
my_hue_list = [0]
|
51 |
+
else:
|
52 |
+
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]
|
53 |
+
#180 at end means highest it can go is 179 (same as hue param )
|
54 |
+
#including 0 makes original image part of the outputs/gif
|
55 |
+
print(my_hue_list)
|
56 |
+
|
57 |
+
#H,S,V = Hue , Saturation, Value (ie color value) parameters
|
58 |
+
#Hue has range [0,179] , Saturation [0,255] , Value [0,255]
|
59 |
+
|
60 |
+
for i in my_hue_list:
|
61 |
+
# modify hue channel by adding difference and modulo 180 (modulo because hue parameter only goes up to index 180, shouldn't exceed that )
|
62 |
+
hnew = np.mod(h + i, 180).astype(np.uint8) #<<<<<<<<<<<<<<<< where the iter comes in
|
63 |
+
|
64 |
+
# recombine channels
|
65 |
+
hsv_new = cv2.merge([hnew,s,v])
|
66 |
+
|
67 |
+
# convert back to bgr
|
68 |
+
bgr_new = cv2.cvtColor(hsv_new, cv2.COLOR_HSV2BGR)
|
69 |
+
|
70 |
+
# put alpha back into bgr_new
|
71 |
+
#bgra = cv2.cvtColor(bgr_new, cv2.COLOR_BGR2BGRA)
|
72 |
+
#bgra[:,:,3] = alpha
|
73 |
+
|
74 |
+
# save output AS FILE LABELED BY ITERABLE
|
75 |
+
output_filename = 'output_bgr_new_' + str(i) +'.png' #<<<<<<<<<<<<<<<< where the iter comes in
|
76 |
+
cv2.imwrite(output_filename, bgr_new)
|
77 |
+
|
78 |
+
|
79 |
+
'''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'''
|
80 |
+
|
81 |
+
|
82 |
+
for i in my_hue_list:
|
83 |
+
output_filename = 'output_bgr_new_' + str(i) +'.png' #<<<<<<<<<<<<<<<< where the iter comes in
|
84 |
+
Image(filename='/main_outputs/' + output_filename)
|
85 |
+
|
86 |
+
# filepaths
|
87 |
+
fp_in = "/main_outputs/*.png"
|
88 |
+
fp_out = "/" + original_filename + "_HueShiftGIF_color_step_" + str(color_step) + "_duration_" + str(duration_parameter) + "_loop_" + str(loop_parameter) + ".gif"
|
89 |
+
|
90 |
+
# https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
|
91 |
+
img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
|
92 |
+
img.save(fp=fp_out, format='GIF', append_images=imgs,
|
93 |
+
save_all=True, duration=duration_parameter, loop=loop_parameter)
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
st.image(fp_out)
|