How to change the pixel values of an Image? [duplicate]
Asked Answered
O

3

14

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?

Otiliaotina answered 5/7, 2016 at 8:14 Comment(0)
G
14

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

Giesser answered 18/7, 2016 at 9:21 Comment(0)
C
4

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()
Catch answered 5/7, 2016 at 8:53 Comment(1)
Thank you for the suggestion. I did that, but now I get the same image again, not an edited image.Otiliaotina
R
3

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()
Rodenhouse answered 7/7, 2017 at 6:56 Comment(2)
You should explain your code in brief..Knowhow
Done! Please check now.Rodenhouse

© 2022 - 2024 — McMap. All rights reserved.