I am using the following code to convert a color image to a grayscale image. Why does it throw a TypeError
?
#!/usr/bin/python
from PIL import Image
im = Image.open("Penguins.jpg")
pixel = im.load()
width, height = im.size
for x in range(width):
for y in range(height):
R,G,B = pixel[x,y]
pixel[x,y] = ((0.299*R+0.587*G+0.114*B),(0.299*R+0.587*G+0.114*B),(0.299*R+0.587*G+0.114*B))
im.save("Penguins_new.jpg")
The argument that you are passing to pixel[x, y]
needs to be an int
, not a float
. Try casting it as an integer.
pixel[x, y] = ((int(0.299*R) + int(...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With