Spaces:
Runtime error
Runtime error
File size: 4,446 Bytes
a85e64f 5853376 84bc6b5 a85e64f 5853376 a85e64f b83acf4 7cb0e99 b83acf4 5853376 fa34b93 5853376 a2db704 d52395d 5853376 a2db704 5853376 d321f27 3ac8916 5853376 d321f27 5853376 3ac8916 5853376 9c6a4cd 0c997e3 9984749 3f1c768 9984749 28e8ad7 9984749 0b9d3b0 5a0272f 28e8ad7 3ac8916 d52c181 5a0272f 28e8ad7 5d9a9d5 |
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 118 119 120 |
import streamlit as st
import cv2
import numpy as np
from PIL import Image
import base64
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("This algorithm creates a GIF from images by creating hue shifted aka different color images from your input images bases on the parameters you chose in the slider above. The current set of parameters yields this many images: \n len(img_array) = ", len(img_array) , " \n So you GIF will be a composite of this many images changing color.")
'''Show some demos: '''
#Uncomment this if statement to show some sample images of the gif
#if len(img_array) > 7:
# for ii in [1, 4, 7]:
# st.image( img_array[ii] )
#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
st.text("Generating GIF, may take a minute. You should see it appear on screen. ")
#Create GIF
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)
#Show gif using this script to show gifs in streamlit https://discuss.streamlit.io/t/how-to-show-local-gif-image/3408/2
"""### gif from local file"""
file_ = open("output_gif.gif", "rb")
contents = file_.read()
data_url = base64.b64encode(contents).decode("utf-8")
file_.close()
st.markdown(
f'<img src="data:image/gif;base64,{data_url}" alt="output gif">',
unsafe_allow_html=True,
) |