User
User

Reputation: 24749

Converting jpg to greyscale

I'm trying to convert an image to greyscale as part of a set of instructions I'm following. However, it won't let me save after making it greyscale.

Error:

    img2.save("img.jpg")
  File "/Library/Python/2.7/site-packages/PIL/Image.py", line 1698, in save
    save_handler(self, fp, filename)
  File "/Library/Python/2.7/site-packages/PIL/JpegImagePlugin.py", line 586, in _save
    raise IOError("cannot write mode %s as JPEG" % im.mode)
IOError: cannot write mode LA as JPEG

Code:

img = Image.open(fname)
img2 = img2.convert('LA')
img2.save("img.jpg")

Upvotes: 3

Views: 6947

Answers (2)

awiebe
awiebe

Reputation: 3846

LA is L (8-bit pixels, black and white) with ALPHA. JPEG images do not support the alpha(transparency) channel, choose GIF or PNG instead.

Or try

img2 = img.convert('L')

For 8 bit black and white only

Upvotes: 10

Tobi S.
Tobi S.

Reputation: 183

Try this:

img2 = img.convert('LA').convert('RGB')

Upvotes: 4

Related Questions