GDavila commited on
Commit
5853376
1 Parent(s): f28222e

Update app.py

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