File size: 4,022 Bytes
a85e64f
5853376
 
 
a85e64f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5853376
a85e64f
b83acf4
7cb0e99
b83acf4
5853376
 
fa34b93
5853376
 
 
a2db704
d52395d
 
5853376
 
a2db704
5853376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d321f27
3ac8916
5853376
 
 
 
 
 
 
 
 
d321f27
 
5853376
 
 
 
 
 
 
3ac8916
 
5853376
 
9c6a4cd
 
 
9984749
 
 
 
6f7b676
 
 
9984749
5a0272f
9984749
5a0272f
 
9984749
5a0272f
d321f27
 
 
 
5853376
8f712b0
 
5a0272f
 
 
3ac8916
d52c181
5a0272f
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import streamlit as st
import cv2
import numpy as np
from PIL import Image

color_step = st.slider('color_step', value=10, min_value=1, max_value=179, step=1)

#duration of each frame of the gif in milliseconds 
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)

#Loop parameter = number of times gif loops. 0 = loops infinitely. 
loop_parameter = st.slider('Loop parameter aka number of times gif loops', value=0, min_value=0, max_value=10, step=1)


if color_step == 0:
  my_hue_list = [0]
else:
  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]



user_image_object = st.file_uploader("upload your image", type=['png', 'jpg'], accept_multiple_files=False)

if user_image_object is not None:
  st.image(user_image_object )
  
  
  user_image_name = "input_image.png"
  
  #re-encode for streamlit interface
  #streamlit uploader encodes as a pillow img so we want to save to open in cv2 (converting directly is a pain)
  input_image = Image.open( user_image_object )
  input_image.save(user_image_name )

  # load image with alpha channel
  img = cv2.imread( user_image_name , cv2.IMREAD_UNCHANGED)
  
  # extract alpha channel  
  #alpha = img[:,:,3]
  
  # extract bgr channels
  bgr = img[:,:,0:3]
  
  # convert to HSV
  hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
  #h = hsv[:,:,0]
  #s = hsv[:,:,1]
  #v = hsv[:,:,2]
  h,s,v = cv2.split(hsv)
  
  
  if color_step == 0:
    my_hue_list = [0]
  else:
    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]
  #180 at end means highest it can go is 179 (same as hue param )
  #including 0 makes original image part of the outputs/gif 
  
  #H,S,V = Hue , Saturation, Value (ie color value) parameters
  #Hue has range [0,179] , Saturation [0,255] , Value [0,255]
  
  img_array = []
  output_filename_array = []
  for i in my_hue_list:
    # modify hue channel by adding difference and modulo 180 (modulo because hue parameter only goes up to index 180, shouldn't exceed that )
    hnew = np.mod(h + i, 180).astype(np.uint8)   #<<<<<<<<<<<<<<<< where the iter comes in 
  
    # recombine channels
    hsv_new = cv2.merge([hnew,s,v])
  
    # convert back to bgr
    bgr_new = cv2.cvtColor(hsv_new, cv2.COLOR_HSV2BGR)
    
    img_array.append(bgr_new )
  
    # put alpha back into bgr_new
    #bgra = cv2.cvtColor(bgr_new, cv2.COLOR_BGR2BGRA)
    #bgra[:,:,3] = alpha
  
    # save output AS FILE LABELED BY ITERABLE 
    output_filename = 'output_bgr_new_' + str(i) +'.png'        #<<<<<<<<<<<<<<<< where the iter comes in 
    output_filename_array.append(output_filename)
    cv2.imwrite(output_filename, bgr_new)
  
  
  height, width, layers = bgr_new.shape
  size = (width,height)
  
  st.write("len(img_array) = ", len(img_array) )
  
  '''Show some demos: '''
  
  if len(img_array) > 7:
    for ii in [1, 4, 7]:
      st.image( img_array[ii] )
  
  '''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'''
  
  '''HuggingFaces Spaces can create a video vile ephemerally but doesn't actually save one that we can access. 
  So to show the video/gif we save it as a file then open that file to show it in streamlit''' 
  
  ''' mp4 method
  out = cv2.VideoWriter('outputvideo.mp4',cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
  for i in range(len(img_array)):
      out.write(img_array[i])
  out.release()
  
  video_file = open('outputvideo.mp4', 'rb')
  video_bytes = video_file.read()
  st.video(video_bytes )
  '''
  
  img, *imgs = [Image.open(f) for f in output_filename_array]
  img.save(fp="output_gif.gif", format='GIF', append_images=imgs,
         save_all=True, duration=duration_parameter, loop=loop_parameter)
         
  st.image("output_gif.gif")