Python 如何更改图像的像素值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38198379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:30:09  来源:igfitidea点击:

How to change the pixel values of an Image?

pythonimage-processingerror-handlingpython-imaging-librarypixel

提问by Manshi Sanghai

I am working on an Image Processing Project and I am a beginner at Python and using PIL. Any help would be appreciated.

我正在从事一个图像处理项目,我是 Python 和使用 PIL 的初学者。任何帮助,将不胜感激。

So, what I am doing is, I have an image of space with stars and noise. What I want to do is keep only the brighter pixels and filter out the dull ones. For now, this is my basic step at trying to remove the noise. After studying the image data, I found that values of 205 are quite possibly the ones I want to keep the threshold at.

所以,我正在做的是,我有一个带有星星和噪音的空间图像。我想要做的是只保留较亮的像素并过滤掉暗淡的像素。现在,这是我尝试消除噪音的基本步骤。在研究了图像数据后,我发现 205 的值很可能是我想要保持阈值的值。

So what I am doing in the code is, open the image and change the pixel values containing 205 to black. Here is the code for the same:

所以我在代码中所做的是,打开图像并将包含 205 的像素值更改为黑色。这是相同的代码:

from PIL import Image
im = Image.open('nuvfits1.png')
pixelMap = im.load()

img = Image.new( im.mode, im.size)
pixelsNew = im.load()
for i in range(img.size[0]):
    for j in range(img.size[1]):
        if 205 in pixelMap[i,j]:
           pixelMap[i,j] = (0,0,0,255)
        pixelsNew[i,j] = pixelMap[i,j]
im.close()
img.show()       
img.save("out.tif") 
img.close()

The problem is, that the resultant image is just a plain white screen. What have I done wrong?

问题是,生成的图像只是一个纯白色的屏幕。我做错了什么?

回答by essbee

The if block should be followed by an else block, so that "normal" pixels that do not meet your criteria retain their original values.

if 块后面应该跟一个 else 块,以便不符合您的标准的“正常”像素保留其原始值。

from PIL import Image
im = Image.open('leaf.jpg')
pixelMap = im.load()

img = Image.new( im.mode, im.size)
pixelsNew = img.load()
for i in range(img.size[0]):
    for j in range(img.size[1]):
        if 205 in pixelMap[i,j]:
            pixelMap[i,j] = (0,0,0,255)
        else:
            pixelsNew[i,j] = pixelMap[i,j]
img.show()

The above code gave me the following results:

上面的代码给了我以下结果:

Input image

输入图像

Input

输入

Output image

输出图像

Output

输出

回答by sidd607

You have made a silly mistake. In Line 6 You have written pixelsNew = im.load() instead of pixelsNew = img.load()This should work now.

你犯了一个愚蠢的错误。在第 6 行你已经写了 pixelsNew = im.load() 而不是pixelsNew = img.load()This 现在应该可以工作了。

from PIL import Image
im = Image.open('nuvfits1.png')
pixelMap = im.load()

img = Image.new( im.mode, im.size)
pixelsNew = img.load()
for i in range(img.size[0]):
    for j in range(img.size[1]):
        if 205 in pixelMap[i,j]:
           pixelMap[i,j] = (0,0,0,255)
        pixelsNew[i,j] = pixelMap[i,j]
im.close()
img.show()       
img.save("out.tif") 
img.close()

回答by Anjali

You basically need a new image with the noise removed, which is pixelsNew. So whenever you find such a case in pixelMap if 205 in pixelMap[i,j]then set that corresponding value as 0 in pixelsNew pixelNew[i,j] = (0, 0, 0, 255). OTHERWISE just copy the pixel value from pixelMap pixelsNew[i,j] = pixelMap[i,j]

您基本上需要一个去除噪声的新图像,即pixelsNew。因此,每当您在 pixelMap 中发现这种情况时,请在 pixelNew 中if 205 in pixelMap[i,j]将该对应值设置为 0 pixelNew[i,j] = (0, 0, 0, 255)。否则只需从 pixelMap 复制像素值pixelsNew[i,j] = pixelMap[i,j]

from PIL import Image
im = Image.open('nuvfits1.png')
pixelMap = im.load()

img = Image.new( im.mode, im.size)
pixelsNew = img.load()
for i in range(img.size[0]):
    for j in range(img.size[1]):
        if 205 in pixelMap[i,j]:
            pixelsNew[i,j] = (0,0,0,255)
        else:
            pixelsNew[i,j] = pixelMap[i,j]
im.close()
img.show()       
img.save("out.tif") 
img.close()