Dabs's picture
innitial commit
8975d21
raw history blame
No virus
1.04 kB
import numpy as np
import gradio as gr
def quantize(val, factor):
quantized = (np.round(factor * np.array(val / 255)) * (255 / factor)).astype(int)
# print(val / 255, factor, factor * np.array(val / 255))
return quantized
def sepia(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(sepia, [gr.inputs.Slider(1, 10, 1), "image"], "pil")
iface.launch()