Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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 - Fatal编程技术网

逐像素编辑图像-Python

逐像素编辑图像-Python,python,image,Python,Image,我有两个图像,一个覆盖和一个背景。 我想创建一个新的图像,通过编辑覆盖图像和操纵它只显示背景图像中有蓝色的像素。 我不想添加背景,它只是用于选择像素。 其余部分应该是透明的。 有什么提示或想法吗?PS:我用油漆编辑了结果图像,所以它并不完美 图1是背景图像 图2是覆盖图 图3是我想要执行的检查。(找出哪些像素的背景为蓝色,并使其余像素透明) 图4是编辑后的结果图像 我根据自己的想法重命名了您的图像,因此我将其作为image.png: 这就是mask.png: 然后我做了我认为你想要的如下。我

我有两个图像,一个覆盖和一个背景。 我想创建一个新的图像,通过编辑覆盖图像和操纵它只显示背景图像中有蓝色的像素。 我不想添加背景,它只是用于选择像素。 其余部分应该是透明的。 有什么提示或想法吗?PS:我用油漆编辑了结果图像,所以它并不完美

图1是背景图像

图2是覆盖图

图3是我想要执行的检查。(找出哪些像素的背景为蓝色,并使其余像素透明)

图4是编辑后的结果图像


我根据自己的想法重命名了您的图像,因此我将其作为
image.png

这就是
mask.png

然后我做了我认为你想要的如下。我写得相当详细,因此您可以看到整个过程中的所有步骤:

#!/usr/local/bin/python3
from PIL import Image
import numpy as np

# Open input images
image = Image.open("image.png")
mask  = Image.open("mask.png")

# Get dimensions
h,w=image.size

# Resize mask to match image, taking care not to introduce new colours (Image.NEAREST)
mask = mask.resize((h,w), Image.NEAREST)    
mask.save('mask_resized.png')

# Convert both images to numpy equivalents
npimage = np.array(image)
npmask  = np.array(mask)

# Make image transparent where mask is not blue
# Blue pixels in mask seem to show up as RGB(163 204 255)
npimage[:,:,3] = np.where((npmask[:,:,0]<170) & (npmask[:,:,1]<210) & (npmask[:,:,2]>250),255,0).astype(np.uint8)

# Identify grey pixels in image, i.e. R=G=B, and make transparent also
RequalsG=np.where(npimage[:,:,0]==npimage[:,:,1],1,0)
RequalsB=np.where(npimage[:,:,0]==npimage[:,:,2],1,0)
grey=(RequalsG*RequalsB).astype(np.uint8)
npimage[:,:,3] *= 1-grey

# Convert numpy image to PIL image and save
PILrgba=Image.fromarray(npimage)
PILrgba.save("result.png")
正在修改第四个通道,即图像的alpha/透明度通道

npimage[:,:,3] = ...