JamesKim commited on
Commit
45f4f71
β€’
1 Parent(s): 1a14cee

image upload

Browse files
Files changed (3) hide show
  1. app.py +57 -0
  2. images/monkey_PNG18720.png +0 -0
  3. requirements.txt +3 -1
app.py CHANGED
@@ -3,7 +3,45 @@ This is the main file for the streamlit app.
3
  Author: Sungjin Kim
4
  Date: 2023-05-19
5
  """
 
 
6
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  def main():
9
  """
@@ -11,6 +49,25 @@ def main():
11
  """
12
  st.set_page_config(page_title="Avatar Generation")
13
  st.header("Artistic Avatar Generation πŸ’–")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  x = st.slider('Slect a value')
15
  st.write(x, 'squared is', x * x)
16
 
 
3
  Author: Sungjin Kim
4
  Date: 2023-05-19
5
  """
6
+ import os
7
+ from typing import Union
8
  import streamlit as st
9
+ import requests
10
+ from PIL import Image
11
+
12
+ def download_image(image_url:str,
13
+ fname:str=None,
14
+ default_fold:str="images/") -> Union[str, None]:
15
+ """
16
+ download image from url
17
+ """
18
+ try:
19
+ image = requests.get(image_url, timeout=5)
20
+ if fname:
21
+ name_ext = fname
22
+ else:
23
+ name_ext = image_url.split("/")[-1]
24
+ name_ext = default_fold + name_ext
25
+ with open(name_ext, "wb") as f:
26
+ # with open(name, "wb") as f:
27
+ f.write(image.content)
28
+ return name_ext
29
+ except Exception as e_name:
30
+ print("An error occured", e_name)
31
+ return None
32
+
33
+ def save_rgba_image(uploaded_file_name:str, uploaded_file_buffer:object,
34
+ Default_fold="images/"):
35
+ """
36
+ # Save the uploaded file to a specific location
37
+ """
38
+ file_path = os.path.join(Default_fold[:-1], uploaded_file_name)
39
+ with open(file_path, "wb") as file:
40
+ file.write(uploaded_file_buffer)
41
+ image_file = Image.open(file_path)
42
+ image_file_rgba = image_file.convert("RGBA")
43
+ image_file_rgba.save(file_path)
44
+ return file_path
45
 
46
  def main():
47
  """
 
49
  """
50
  st.set_page_config(page_title="Avatar Generation")
51
  st.header("Artistic Avatar Generation πŸ’–")
52
+
53
+ mode = st.radio("Choose your option", ("Upload photo by file", "Upload photo by url"))
54
+ photo_filename = None
55
+
56
+ if mode == "Upload photo by file":
57
+ uploaded_file = st.file_uploader("Upload your photo", type=["png"])
58
+ if uploaded_file is not None:
59
+ st.image(uploaded_file, width=256, caption="Your photo")
60
+ # Save the uploaded file to a specific location
61
+ photo_filename = save_rgba_image(uploaded_file.name, uploaded_file.getbuffer())
62
+ else:
63
+ photo_url = st.text_input("Enter your photo url:",
64
+ value="https://pngimg.com/uploads/monkey/monkey_PNG18720.png")
65
+ if photo_url:
66
+ st.image(photo_url, width=256, caption="Your photo")
67
+ photo_filename = download_image(photo_url) # e.g., "images/monkey_PNG18720.png"
68
+
69
+ st.write("Your photo filename:", photo_filename)
70
+
71
  x = st.slider('Slect a value')
72
  st.write(x, 'squared is', x * x)
73
 
images/monkey_PNG18720.png ADDED
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- streamlit
 
 
 
1
+ streamlit
2
+ requests
3
+ Pillow