File size: 1,546 Bytes
cfcac52
36dd0e0
 
cfcac52
36dd0e0
 
 
 
 
 
cfcac52
36dd0e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfcac52
 
36dd0e0
 
 
 
 
cfcac52
 
 
 
 
 
 
 
 
 
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
import io
import numpy as np
from PIL import Image
import streamlit as st


uploaded_file = st.file_uploader("Choose an image", type=['png', 'jpg', 'jpeg'])
pixelsize = st.selectbox(
    'Pixel grid size',
    (256, 128, 64, 32), format_func=lambda x: f'{x}px')
colors = st.selectbox("Number of colors", (256, 128, 64, 32, 16, 8))

if uploaded_file:
    submit_button = st.button(label='Generate pixel art!')
    if uploaded_file and submit_button:
        img_raw = Image.open(uploaded_file)
        hsv_image = img_raw.convert("HSV")
        hue, saturation, value = hsv_image.split()
        threshold = 180
        saturation = saturation.point(lambda x: 255 if x > threshold else x)
        hsv_image = Image.merge("HSV", (hue, saturation, value))
        img = hsv_image.convert("RGB")
        size = img.size

        new_size = (size[0] * pixelsize // max(size), size[1] * pixelsize // max(size))

        img = img.convert("RGB").convert("P", palette=Image.ADAPTIVE, colors=colors)
        img = img.resize(new_size, resample=Image.NEAREST).quantize(colors=256, dither=Image.Dither.FLOYDSTEINBERG)

        img = img.resize(size, resample=Image.NEAREST)
        col1, col2 = st.columns(2)
        col1.image(img_raw)
        col2.image(img, channels='RGB')

        output = io.BytesIO()
        img.convert("RGB").save(output, format='JPEG')

        btn = st.download_button(
                label="Download image",
                data=output,
                file_name="pixel_art.png",
                mime="image/png"
            )