File size: 3,484 Bytes
a85e64f
5853376
 
 
a85e64f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5853376
a85e64f
b83acf4
7cb0e99
b83acf4
5853376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
  user_image_object.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 
  print(my_hue_list)
  
  #H,S,V = Hue , Saturation, Value (ie color value) parameters
  #Hue has range [0,179] , Saturation [0,255] , Value [0,255]
  
  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)
  
    # 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 
    cv2.imwrite(output_filename, bgr_new)
  
  
  '''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'''
  
  
  for i in my_hue_list:
    output_filename = 'output_bgr_new_' + str(i) +'.png'        #<<<<<<<<<<<<<<<< where the iter comes in 
    Image(filename='/main_outputs/' + output_filename) 
  
  # filepaths
  fp_in = "/main_outputs/*.png"
  fp_out = "/" + original_filename + "_HueShiftGIF_color_step_" + str(color_step) + "_duration_" + str(duration_parameter) + "_loop_" + str(loop_parameter) + ".gif"
  
  # https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
  img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
  img.save(fp=fp_out, format='GIF', append_images=imgs,
           save_all=True, duration=duration_parameter, loop=loop_parameter)
  
  
  
  st.image(fp_out)