简体   繁体   中英

can't save JPG image with alpha

width, height = resized_image.size
dummyImg = np.zeros([height, width, 4], dtype=np.uint8)
for x in range(width):
    for y in range(height):
        color = seg_map[y,x]
        (r,g,b) = resized_image.getpixel((x,y))
        if color == 0:
            dummyImg[y,x] = [255,255,255,255]
        else:
            dummyImg[y,x] = [r,g,b,255]
img = Image.fromarray(dummyImg)
outputFilePath = '15d09a689ca0d4 Mod.jpg'
img.save(outputFilePath)

Hello, I am getting this error while trying to save image as "JPG", I included alpha channels in dummyimg .

Traceback (most recent call last):
  File "/home/timmy/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 620, in _save
    rawmode = RAWMODE[im.mode]
KeyError: 'RGBA'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/timmy/Desktop/image-background-removal-master/impr.py", line 79, in <module>
    img.save(outputFilePath)
  File "/home/timmy/.local/lib/python3.6/site-packages/PIL/Image.py", line 2007, in save
    save_handler(self, fp, filename)
  File "/home/timmy/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 622, in _save
    raise IOError("cannot write mode %s as JPEG" % im.mode)
OSError: cannot write mode RGBA as JPEG

I checked GitHub issue cannot write mode RGBA as JPEG (4.2.0) , they said it is solved and is now doable

JPEG file format cannot handle transparency. To save an image with color and transparency you need to use another format (eg PNG).

You can save to JPEG only if you drop the alpha channel and make all pixels opaque.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM