Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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 如何使用枕头';s Image.load()函数_Python_Image Processing_Python Imaging Library_Image Manipulation - Fatal编程技术网

Python 如何使用枕头';s Image.load()函数

Python 如何使用枕头';s Image.load()函数,python,image-processing,python-imaging-library,image-manipulation,Python,Image Processing,Python Imaging Library,Image Manipulation,我想根据某些像素值创建一个遮罩。例如:B>200的每个像素 Image.load()方法似乎正是我用这些值标识像素所需要的,但我似乎不知道如何获取所有这些像素并从中创建遮罩图像 R,G,B=0,1,2 像素=self.input_image.get_value().load() 宽度,高度=自身。输入图像。获取值()。大小 对于范围(0,高度)内的y: 对于范围内的x(0,宽度): 如果像素[x,y][B]>200: 打印(“%s-%s”的蓝色大于200”%(x,y)) `` 多亏了马克·塞切尔

我想根据某些像素值创建一个遮罩。例如:B>200的每个像素

Image.load()方法似乎正是我用这些值标识像素所需要的,但我似乎不知道如何获取所有这些像素并从中创建遮罩图像

R,G,B=0,1,2
像素=self.input_image.get_value().load()
宽度,高度=自身。输入图像。获取值()。大小
对于范围(0,高度)内的y:
对于范围内的x(0,宽度):
如果像素[x,y][B]>200:
打印(“%s-%s”的蓝色大于200”%(x,y))
``

多亏了马克·塞切尔(Mark Setchell)的回复,我制作了一个与我的图像大小相同的numpy数组,并用零填充,从而解决了这个问题。然后,对于B>200的每个像素,我将数组中的对应值设置为255。最后,我以与输入图像相同的模式将numpy数组转换为PIL图像

            R, G, B = 0, 1, 2

            pixels = self.input_image.get_value().load()
            width, height = self.input_image.get_value().size
            mode = self.input_image.get_value().mode

            mask = np.zeros((height, width))

            for y in range(0, height):
                for x in range(0, width):
                    if pixels[x, y][2] > 200:
                        mask[y][x] = 255

            mask_image = Image.fromarray(mask).convert(mode)

我的意思是让您避免使用
for
循环,而只使用Numpy。因此,从这张图片开始:

如果要使遮罩像素为黑色,请使用:

ni[blues] = 0
Image.fromarray(ni).save('result.png')


您可以针对以下范围进行更复杂的复合测试:

#!/usr/bin/env python3

from PIL import Image
import numpy as np

# Open image
im = Image.open('colorwheel.png')

# Make Numpy array
ni = np.array(im)

# Mask pixels where 100 < Blue < 200
blues = ( ni[:,:,2]>100 ) & (ni[:,:,2]<200)

# Save logical mask as PNG
Image.fromarray((blues*255).astype(np.uint8)).save('result.png')

看看这里。。。谢谢你,那正是我想要的!是的,我完全误解了你在另一个问题上的回答。谢谢你的澄清!这确实比循环快得多。如何扩展以在多个通道中以及在特定值之间更改像素?例如,10>R>50 10>G>100 50>B>200的每个像素
#!/usr/bin/env python3

from PIL import Image
import numpy as np

# Open image
im = Image.open('colorwheel.png')

# Make Numpy array
ni = np.array(im)

# Mask pixels where 100 < Blue < 200
blues = ( ni[:,:,2]>100 ) & (ni[:,:,2]<200)

# Save logical mask as PNG
Image.fromarray((blues*255).astype(np.uint8)).save('result.png')
bluesHi = ni[:,:,2] > 200 
redsLo  = ni[:,:,0] < 50

mask = np.logical_and(bluesHi,redsLo)