Spaces:
Runtime error
Runtime error
File size: 1,169 Bytes
8975d21 7c4b8dc 8975d21 7c4b8dc 8975d21 |
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 |
import numpy as np
import gradio as gr
def quantize(val, factor):
quantized = (np.round(factor * np.array(val / 255)) * (255 / factor)).astype(int)
return quantized
def fsd(factor, input_img):
img_arr = np.asarray(input_img)
new_img = np.copy(img_arr)
for y in range(img_arr.shape[1] - 1):
for x in range(img_arr.shape[0] - 1):
old_pixel = new_img[x, y].copy()
new_pixel = quantize(old_pixel, factor)
new_img[x, y] = new_pixel
quant_error = old_pixel - new_pixel
new_img[x + 1][y ] = new_img[x + 1][y ] + quant_error * 7 / 16
new_img[x - 1][y + 1] = new_img[x - 1][y + 1] + quant_error * 3 / 16
new_img[x ][y + 1] = new_img[x ][y + 1] + quant_error * 5 / 16
new_img[x + 1][y + 1] = new_img[x + 1][y + 1] + quant_error * 1 / 16
return new_img
iface = gr.Interface(fsd,
[gr.inputs.Slider(1, 10, 1),
"image"],
"pil",
title="Floyd–Steinberg dithering",
description="Floyd–Steinberg dithering algorithm")
iface.launch() |