Spaces:
Runtime error
Runtime error
Commit ·
36dd0e0
1
Parent(s): 731958e
ad app
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
uploaded_file = st.file_uploader("Choose an image", type=['png', 'jpg', 'jpeg'])
|
| 7 |
+
pixelsize = st.selectbox(
|
| 8 |
+
'Pixel grid size',
|
| 9 |
+
(256, 128, 64, 32), format_func=lambda x: f'{x}px')
|
| 10 |
+
|
| 11 |
+
if uploaded_file:
|
| 12 |
+
submit_button = st.button(label='Generate pixel art!')
|
| 13 |
+
if uploaded_file and submit_button:
|
| 14 |
+
img_raw = Image.open(uploaded_file)
|
| 15 |
+
hsv_image = img_raw.convert("HSV")
|
| 16 |
+
hue, saturation, value = hsv_image.split()
|
| 17 |
+
threshold = 180
|
| 18 |
+
saturation = saturation.point(lambda x: 255 if x > threshold else x)
|
| 19 |
+
hsv_image = Image.merge("HSV", (hue, saturation, value))
|
| 20 |
+
img = hsv_image.convert("RGB")
|
| 21 |
+
size = img.size
|
| 22 |
+
|
| 23 |
+
new_size = (size[0] * pixelsize // max(size), size[1] * pixelsize // max(size))
|
| 24 |
+
|
| 25 |
+
img = img.convert("RGB").convert("P", palette=Image.ADAPTIVE, colors=128).resize(new_size, resample=Image.NEAREST).quantize(colors=256, kmeans=0, dither=Image.Dither.FLOYDSTEINBERG)
|
| 26 |
+
|
| 27 |
+
img = img.resize(size, resample=Image.NEAREST)
|
| 28 |
+
col1, col2 = st.columns(2)
|
| 29 |
+
col1.image(img_raw)
|
| 30 |
+
col2.image(img, channels='RGB')
|