1

I am trying to create a code for converting the RBG values of specific pixels in a picture. Here is the code I have thus far:

So I have gotten as far as inputting new RGB values for the new color of the pixel, but I am stumped as how to actually input those for the pixel. Thanks, any help is appreciated!

  • Use [`Image.putpixel()`](http://pillow.readthedocs.io/en/4.2.x/reference/Image.html#PIL.Image.Image.putpixel). – martineau Aug 14 '17 at 21:57
  • @martineau any ideas with how to format variables for the image.putpixel? I am encountering trouble with this. The code I added to the bottom of the function looks like this: """original.putpixel((coordinates) , (new_RGB))"""" Which should work, since the first argument would be the two cartesian coordinates and the second argument would be the r,g,b values, not sure why it isn't working though. – physicslifter Aug 15 '17 at 01:34
  • It depends on the format of the opened image. If it's RGB, then the color value should be a sequence of three _integers_, like `(10,2,4)` not `"10,2,4"`. Black and white images would only need a single integer value (not a sequence with one thing in it). That means you can't just use the string the `input()` function returns. You'll need to convert the string into the needed format before calling `putpixel()`. The same is true of the coordinates, except they're always going to need to be two integer values representing x and y position regardless of the image format. – martineau Aug 15 '17 at 02:31
  • P.S. You could use something like `new_RGB = tuple(int(x) for x in input().split(','))` similar to what's shown in @Dawit Abate's answer below assuming the three numbers are separated by commas. The same sort of thing should also work for the two coordinate values.Actually his answer should almost work, except his `new_RGB` is a `list` not the `tuple` required. Besides the user input conversion, this [example code](http://pillow.readthedocs.io/en/4.2.x/reference/PixelAccess.html#example) shows essentially the same approach (which would be better than using `putpixel()` for a lot of pixels). – martineau Aug 15 '17 at 02:52
  • 1
    Thanks a lot, I finally got it figured out, you've been a huge help!!! – physicslifter Aug 15 '17 at 06:53
  • physicslifter: That's good to hear...and as a bonus you probably learned several useful things in the process. `;-)` – martineau Aug 15 '17 at 12:53

1 Answers1

1

This is what i came up with.

from PIL import Image, ImageFilter


print("enter image file:")
myimage = input()

try:
    original = Image.open(myimage)
    im = original.load()
except:
    print('Invalid file')
    # print(myimage)
    # print("The size of the Image is: ")

print(original.format, original.size, original.mode)

# pixel_values = list(original.getdata())

'''
for y in range(0, 512):
    row = ""
for x in range(0, 512):
    row = ""
'''

print("Enter coordinates of desired pixel in x,y form")
coordinates = [int(x) for x in input().split(',')]
x, y = coordinates
R, G, B = im[x, y]
print("R,G,B values corresponding with this pixel are:")
print(R, G, B)

print("enter new R,G,B values")
new_RGB = [int(x) for x in input().split(',')]

r, g, b = new_RGB

im[x, y] = (r, g, b)

original.save(myimage)
Dawit Abate
  • 462
  • 5
  • 10
  • I tried this code, and one problem is that it produces an error if the x,y coordinates are lower values (for example I used 15,18 and it produced an error. But you think the [x,y]=(r,g,b) is the way to go as opposed to the image.putpixel? Because I have been struggling with the latter. I will play around with the former a bit and see if I can get it to work. – physicslifter Aug 15 '17 at 01:32
  • It is working for me with (15,18) can you post the error code? – Dawit Abate Aug 15 '17 at 14:52