How to change the pixel values of an Image? [dupli

2019-04-08 04:43发布

This question already has an answer here:

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

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.

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:

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?

3条回答
【Aperson】
2楼-- · 2019-04-08 05:29

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

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()
查看更多
祖国的老花朵
3楼-- · 2019-04-08 05:36

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]

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()
查看更多
Ridiculous、
4楼-- · 2019-04-08 05:38

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

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

查看更多
登录 后发表回答