c语言抖动算法,弗洛伊德-斯坦伯格抖动算法

import numpy as np

from PIL import Image

from requests import get

from bokeh.plotting import figure, show, output_notebook

from bokeh.layouts import layout

from bokeh.palettes import gray

output_notebook()

def image_dither(path, black='#000000', white='#ffffff'):

image_rgb = read_image(path)

image_gray = grayscale(image_rgb)

image_bw = floyd_steinberg(image_gray)

show(layout([[

plot(image_gray, palette=gray(256)),

plot(image_bw, palette=[black, white])

]]))

def floyd_steinberg(image):

image = image.copy()

distribution = np.array([7, 3, 5, 1], dtype=float) / 16

u = np.array([0, 1, 1, 1])

v = np.array([1, -1, 0, 1])

for y in range(image.shape[0] - 1):

for x in range(image.shape[1] - 1):

value = np.round(image[y, x])

error = image[y, x] - value

image[y, x] = value

image[y + u, x + v] += error * distribution

image[:, -1] = 1

image[-1, :] = 1

return image

def grayscale(image):

height, width, _ = image.shape

image = np.array(image, dtype=np.float32) / 255

image = image[:, :, 0] * .21 + \

image[:, :, 1] * .72 + \

image[:, :, 2] * .07

return image.reshape(height, width)

def read_image(path, size=400):

if path.startswith('https://'):

image = Image.open(get(path, stream=True).raw)

else:

image = Image.open(path)

width, height = image.size

width, height = size, int(size * height / width)

image = image.resize((width, height), Image.ANTIALIAS)

data = image.getdata()

assert data.bands in [3, 4], 'RGB or RGBA image is required'

raw = np.array(data, dtype=np.uint8)

return raw.reshape(height, width, data.bands)

def plot(image, palette):

y, x = image.shape

plot = figure(x_range=(0, x), y_range=(0, y),

plot_width=x, plot_height=y)

plot.axis.visible = False

plot.toolbar_location = None

plot.min_border = 0

plot.image([np.flipud(image)], x=0, y=0, dw=x, dh=y,

palette=palette)

return plot

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值