Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
like image 729
User Avatar asked Nov 30 '25 11:11

User


1 Answers

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

like image 74
awiebe Avatar answered Dec 02 '25 04:12

awiebe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!