简体   繁体   English

循环浏览2张图像的所有像素,并将黑色像素替换为白色

[英]Loop through all pixels of 2 images and replace the black pixels with white

I have 2 co-located images, both created in a similar way and both have the size of 7,221 x 119 pixels. 我有2个位于同一位置的图像,都以类似的方式创建,并且都具有7,221 x 119像素的大小。

I want to loop through all the pixels of both images. 我想遍历两个图像的所有像素。 If the pixels are black on the first image and also black on the second image then turn it into white, otherwise, no change. 如果第一个图像上的像素为黑色,第二个图像上的像素也为黑色,则将其变为白色,否则保持不变。

How can I do it with python? 我该如何使用python?

I suggest the use of the Pillow library ( https://python-pillow.org/ ) which is a fork of the PIL library. 我建议使用Pillow库( https://python-pillow.org/ ),它是PIL库的分支。

Here's something from the Pillow docs: http://pillow.readthedocs.io/en/3.1.x/reference/PixelAccess.html 以下是Pillow文档的内容: http : //pillow.readthedocs.io/en/3.1.x/reference/PixelAccess.html

And a couple of Stackoverflow questions that may help you: 还有一些Stackoverflow问题可以帮助您:

Is it possible to change the color of one individual pixel in Python? 是否可以在Python中更改单个像素的颜色?

Changing pixel color Python 更改像素颜色Python

I guess you'd just have to open both images, loop through each pixel of rach image, compare the pixels, compare pixels, then replace if necessary. 我猜您只需要打开两个图像,遍历rach图像的每个像素,比较像素,比较像素,然后在必要时进行替换。

This should hopefully be pretty close to what you're looking for. 希望它应该与您要寻找的非常接近。

from PIL import Image
from PIL import ImageFilter

im            = Image.open('a.png')
imb           = Image.open('b.png')
pix           = im.load()
width, height = im.size
for w in xrange(width):
    for h in xrange(height):
        r,g,b,a = pix[(w,h)]
        rb, gb, bb, ab = pix[(w,h)]
        if not (r+g+b+rb+gb+bb): #all values 0
            pix[w,h] = (255,255,255,255)
im.save('test','BMP')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM