Spaces:
Runtime error
Runtime error
pedrogengo
commited on
Commit
•
cfcac52
1
Parent(s):
0061c41
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
-
import
|
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!')
|
@@ -22,9 +24,20 @@ if uploaded_file:
|
|
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=
|
|
|
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')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
+
import streamlit as st
|
5 |
|
6 |
|
7 |
uploaded_file = st.file_uploader("Choose an image", type=['png', 'jpg', 'jpeg'])
|
8 |
pixelsize = st.selectbox(
|
9 |
'Pixel grid size',
|
10 |
(256, 128, 64, 32), format_func=lambda x: f'{x}px')
|
11 |
+
colors = st.selectbox("Number of colors", (256, 128, 64, 32, 16, 8))
|
12 |
|
13 |
if uploaded_file:
|
14 |
submit_button = st.button(label='Generate pixel art!')
|
|
|
24 |
|
25 |
new_size = (size[0] * pixelsize // max(size), size[1] * pixelsize // max(size))
|
26 |
|
27 |
+
img = img.convert("RGB").convert("P", palette=Image.ADAPTIVE, colors=colors)
|
28 |
+
img = img.resize(new_size, resample=Image.NEAREST).quantize(colors=256, dither=Image.Dither.FLOYDSTEINBERG)
|
29 |
|
30 |
img = img.resize(size, resample=Image.NEAREST)
|
31 |
col1, col2 = st.columns(2)
|
32 |
col1.image(img_raw)
|
33 |
col2.image(img, channels='RGB')
|
34 |
+
|
35 |
+
output = io.BytesIO()
|
36 |
+
img.convert("RGB").save(output, format='JPEG')
|
37 |
+
|
38 |
+
btn = st.download_button(
|
39 |
+
label="Download image",
|
40 |
+
data=output,
|
41 |
+
file_name="pixel_art.png",
|
42 |
+
mime="image/png"
|
43 |
+
)
|