无法在 Fabric 笔记本中打开 .png 文件

问题描述 投票:0回答:1

我正在遵循教程 - https://learn.microsoft.com/en-gb/azure/ai-services/openai/how-to/assistant#supported-file-types

我在

notebook
中创建了一个
Fabric

我使用以下命令在

cell

中下载了软件包
%pip install openai
%pip install openai --upgrade
%pip install pillow

在最后一步中,当我运行代码打开

sinewave.png
文件时,

from PIL import Image

# Display the image in the default image viewer
image = Image.open("sinewave.png")
image.show()

我收到以下错误:

    ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~/cluster-env/trident_env/lib/python3.10/site-packages/PIL/JpegImagePlugin.py:639, in _save(im, fp, filename)
    638 try:
--> 639     rawmode = RAWMODE[im.mode]
    640 except KeyError as e:

KeyError: 'RGBA'

The above exception was the direct cause of the following exception:

OSError                                   Traceback (most recent call last)
File ~/cluster-env/trident_env/lib/python3.10/site-packages/PIL/Image.py:643, in Image._repr_image(self, image_format, **kwargs)
    642 try:
--> 643     self.save(b, image_format, **kwargs)
    644 except Exception as e:

File ~/cluster-env/trident_env/lib/python3.10/site-packages/PIL/Image.py:2413, in Image.save(self, fp, format, **params)
   2412 try:
-> 2413     save_handler(self, fp, filename)
   2414 except Exception:

File ~/cluster-env/trident_env/lib/python3.10/site-packages/PIL/JpegImagePlugin.py:642, in _save(im, fp, filename)
    641     msg = f"cannot write mode {im.mode} as JPEG"
--> 642     raise OSError(msg) from e
    644 info = im.encoderinfo

OSError: cannot write mode RGBA as JPEG

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
File ~/cluster-env/trident_env/lib/python3.10/site-packages/IPython/core/formatters.py:344, in BaseFormatter.__call__(self, obj)
    342     method = get_real_method(obj, self.print_method)
    343     if method is not None:
--> 344         return method()
    345     return None
    346 else:

File ~/cluster-env/trident_env/lib/python3.10/site-packages/PIL/Image.py:661, in Image._repr_jpeg_(self)
    656 def _repr_jpeg_(self):
    657     """iPython display hook support for JPEG format.
    658 
    659     :returns: JPEG version of the image as bytes
    660     """
--> 661     return self._repr_image("JPEG")

File ~/cluster-env/trident_env/lib/python3.10/site-packages/PIL/Image.py:646, in Image._repr_image(self, image_format, **kwargs)
    644 except Exception as e:
    645     msg = f"Could not save to {image_format} for display"
--> 646     raise ValueError(msg) from e
    647 return b.getvalue()

ValueError: Could not save to JPEG for display
microsoft-fabric
1个回答
0
投票

Copilot
帮助找到了答案。

It seems you’re encountering an error related to saving an image with the mode “RGBA” as a JPEG. Let’s address this issue! 😊

The problem arises because JPEG images do not support transparency, and the “RGBA” mode includes an alpha channel for transparency. To resolve this, you can follow one of the solutions below:

from PIL import Image

# Load the image (assuming it's in RGBA mode)
im = Image.open("audacious.png")

# Convert to RGB mode
rgb_im = im.convert("RGB")

# Save as a JPEG
rgb_im.save("audacious.jpg")
© www.soinside.com 2019 - 2024. All rights reserved.