无法用alph保存JPG图像

2024-04-26 12:49:19 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

您好,我在尝试将图像保存为“JPG”时遇到此错误,我在dummyimg中包含了alpha通道。你知道吗

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

我检查了GitHub的问题cannot write mode RGBA as JPEG (4.2.0) ,他们说它已经解决了,现在是可行的


Tags: inpyimagehomeimgmodesavelocal