Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何对黑白图片进行半色调处理?_Python_Image_Numpy - Fatal编程技术网

Python 如何对黑白图片进行半色调处理?

Python 如何对黑白图片进行半色调处理?,python,image,numpy,Python,Image,Numpy,我想把一张图片分成同样大的正方形,测量平均灰度等级,然后用一个斑点(也称为半色调)代替它。这个代码给了我一个图片,但它看起来不正确。有什么想法吗 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 rang

我想把一张图片分成同样大的正方形,测量平均灰度等级,然后用一个斑点(也称为半色调)代替它。这个代码给了我一个图片,但它看起来不正确。有什么想法吗

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()

这是你想要的东西。这本质上是对相关问题公认答案中代码的简化:

以下是将其用作输入图像的结果:

产生的半色调效果:


你昨天问了同样的问题。它被搁置是有原因的。你为什么不继续写你的问题,而不是再发一次@皮因斯基。我很确定这不是他在mindOP里所做的,如果你需要帮助,你需要学会如何正确地问。带有“修复此问题”的代码转储不是正确的问题。提供输入和输出的样本。解释为什么输出与您想要的不同。解释你做了什么来解决这个问题,即使它不起作用。表现出努力。一个好的问题通常应该提供足够的信息,让某人只需从这个页面复制粘贴就可以在本地重现您的问题。@Emiki。我在帮助你。我已经给了你关于如何发布一个不会在几分钟内被关闭的问题的明确指示。如果你在这方面多做点工作,你可以把它变成一个非常体面的问题。我访问你的问题是因为我认为这个主题很有趣,这个问题很有潜力。你对“看起来不对劲”的量化是什么?@MadPhysicast:很高兴你这么说。在更高分辨率的图像上效果更好——我只是使用了一个相对低分辨率的图像进行测试和开发(并上传到这里)。
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()