Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: integer argument expected, got float when converting an image to greyscale

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")
like image 823
唐笙凱 Avatar asked Apr 10 '15 18:04

唐笙凱


1 Answers

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(...
like image 80
Zizouz212 Avatar answered Nov 02 '22 15:11

Zizouz212