如何半色调黑白照片? [英] How to halftone a black and white picture?
本文介绍了如何半色调黑白照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将图片划分为相同的大正方形,并测量平均灰度等级并将其替换为blob,即半色调。这段代码给了我一张照片,但看起来并不正确。什么想法可能是错的?
im = scipy.misc.imread(uggla.tif)
def halftoning(im ):
im = im.astype('float64')
width,height = im.shape
halftone_pic = np.zeros((width,height))
for x in范围(宽度):
范围内的y(高度):
floating_matrix = im [x:x + 1,y:y + 1]
sum = np.sum(floating_matrix)
mean = np.mean(sum)
round =(mean> 128)* 255
halftone_pic [x,y] = round
fig,ax = plt.subplots(1 ,2)
ax [0] .imshow(im,cmap =gray)
ax [1] .imshow(halftone_pic,cmap =gray)
plt.show()
解决方案
这里有一些代码可以满足您的需求。我从一个答案中修改了一些代码到某个
生成半色调结果:
< a href =https://i.stack.imgur.com/Ejwjz.png =nofollow noreferrer>
I want to divide a picture in equally big squares and measure the average gray scale level and replace it with a blob, aka halftoning. This code gives me a picture but it doesn't look right. Any ideas what could be wrong?
im = scipy.misc.imread("uggla.tif")
def halftoning(im):
im = im.astype('float64')
width,height = im.shape
halftone_pic = np.zeros((width, height))
for x in range(width):
for y in range(height):
floating_matrix = im[x:x + 1, y:y + 1]
sum = np.sum(floating_matrix)
mean = np.mean(sum)
round = (mean > 128) * 255
halftone_pic[x,y] = round
fig, ax = plt.subplots(1,2)
ax[0].imshow(im, cmap="gray")
ax[1].imshow(halftone_pic, cmap="gray")
plt.show()
解决方案
Here's some code that does what you want. I adapted some code from one answer to a somewhat related question:
from PIL import Image, ImageDraw, ImageStat
# Adaption of answer https://stackoverflow.com/a/10575940/355230
def halftone(img, sample, scale, angle=45):
''' Returns a halftone image created from the given input image "img".
"sample" (in pixels), determines the sample box size from the original
image. The maximum output dot diameter is given by "sample" * "scale"
(which is also the number of possible dot sizes). So "sample" == 1 will
preserve the original image resolution, but "scale" must be > 1 to allow
variations in dot size.
'''
img_grey = img.convert('L') # Convert to greyscale.
channel = img_grey.split()[0] # Get grey pixels.
channel = channel.rotate(angle, expand=1)
size = channel.size[0]*scale, channel.size[1]*scale
bitmap = Image.new('1', size)
draw = ImageDraw.Draw(bitmap)
for x in range(0, channel.size[0], sample):
for y in range(0, channel.size[1], sample):
box = channel.crop((x, y, x+sample, y+sample))
mean = ImageStat.Stat(box).mean[0]
diameter = (mean/255) ** 0.5
edge = 0.5 * (1-diameter)
x_pos, y_pos = (x+edge) * scale, (y+edge) * scale
box_edge = sample * diameter * scale
draw.ellipse((x_pos, y_pos, x_pos+box_edge, y_pos+box_edge),
fill=255)
bitmap = bitmap.rotate(-angle, expand=1)
width_half, height_half = bitmap.size
xx = (width_half - img.size[0]*scale) / 2
yy = (height_half - img.size[1]*scale) / 2
bitmap = bitmap.crop((xx, yy, xx + img.size[0]*scale,
yy + img.size[1]*scale))
return Image.merge('1', [bitmap])
# Sample usage
img = Image.open('uggla.tif')
img_ht = halftone(img, 8, 1)
img_ht.show()
Here's the results from using this as the input image:
Halftoned result produced:
这篇关于如何半色调黑白照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文