[英]Loop through all pixels of 2 images and replace the black pixels with white
我有2個位於同一位置的圖像,都以類似的方式創建,並且都具有7,221 x 119像素的大小。
我想遍歷兩個圖像的所有像素。 如果第一個圖像上的像素為黑色,第二個圖像上的像素也為黑色,則將其變為白色,否則保持不變。
我該如何使用python?
我建議使用Pillow庫( https://python-pillow.org/ ),它是PIL庫的分支。
以下是Pillow文檔的內容: http : //pillow.readthedocs.io/en/3.1.x/reference/PixelAccess.html
還有一些Stackoverflow問題可以幫助您:
我猜您只需要打開兩個圖像,遍歷rach圖像的每個像素,比較像素,比較像素,然后在必要時進行替換。
希望它應該與您要尋找的非常接近。
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.