pil pil tobytes()方法后,图像被损坏

发布于 2025-02-01 08:59:38 字数 1210 浏览 4 评论 0原文

我想将图像上传到Google存储桶中,但是我想在上传之前减少图像的大小。当我不调用self._resize_image方法时,图像被成功上传而没有任何问题。但是,当我调用调整大小方法时,它可以正常工作,直到image.tobytes()方法为止。在称为image.tobytes()方法之后,无法再读取图像。

def _resize_image(self, image: bytes, base_with: int = 300) -> bytes:
    stream = BytesIO(image)
    image = Image.open(stream).convert("RGBA")
    width_percentage = base_with / float(image.size[0])
    height_size = int(float(image.size[1]) * float(width_percentage))
    image = image.resize((base_with, height_size), Image.ANTIALIAS)
    # if I do image.show() here the picture is still displayed correctly.
    return image.tobytes()  # after this line the picture is getting uploaded, but can't be read by Google anymore.

def upload_image_to_bucket(self, image: bytes, bucket_folder: str, compress: bool = True) -> str:
    if compress:
        # if I don't call this method the picture get's uploaded correctly.
        image = self._resize_image(image=image)
    file_name = f"{UUIDService().create_uuid(length=40)}.jpeg"
    bucket = self._client.storage.bucket()
    blob = bucket.blob(f"{bucket_folder}/{file_name}")
    blob.upload_from_string(data=image, content_type="image/jpeg")
    return file_name

I want to upload an image to a Google Bucket, however I want to reduce the size of the image before uploading. When I don't call the self._resize_image method the image gets successfully uploaded without any problems. However, when I call the resize method it works until the image.tobytes() method. After the image.tobytes() method is called the image can't be read anymore.

def _resize_image(self, image: bytes, base_with: int = 300) -> bytes:
    stream = BytesIO(image)
    image = Image.open(stream).convert("RGBA")
    width_percentage = base_with / float(image.size[0])
    height_size = int(float(image.size[1]) * float(width_percentage))
    image = image.resize((base_with, height_size), Image.ANTIALIAS)
    # if I do image.show() here the picture is still displayed correctly.
    return image.tobytes()  # after this line the picture is getting uploaded, but can't be read by Google anymore.

def upload_image_to_bucket(self, image: bytes, bucket_folder: str, compress: bool = True) -> str:
    if compress:
        # if I don't call this method the picture get's uploaded correctly.
        image = self._resize_image(image=image)
    file_name = f"{UUIDService().create_uuid(length=40)}.jpeg"
    bucket = self._client.storage.bucket()
    blob = bucket.blob(f"{bucket_folder}/{file_name}")
    blob.upload_from_string(data=image, content_type="image/jpeg")
    return file_name

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆光飞翔i 2025-02-08 08:59:38

来自 pillow tobytes

此方法从内部存储中返回原始图像数据。对于压缩图像数据(例如PNG,JPEG),请使用Save(),带有内存数据的字节参数。

因此,tobytes()方法返回枕头的内部表示图像,大概将使用frombytes()恢复。如果要将映像保存为JPEG,请使用 保存()方法如文档所建议:

output = BytesIO()
image.save(output, format="jpeg")
... # do something with `output`

From Pillow's documentation on tobytes:

This method returns the raw image data from the internal storage. For compressed image data (e.g. PNG, JPEG) use save(), with a BytesIO parameter for in-memory data.

So the tobytes() method returns Pillow's internal representation of the image, presumably to be restored with frombytes(). If you want to save the image as JPEG, use the save() method as suggested by the documentation:

output = BytesIO()
image.save(output, format="jpeg")
... # do something with `output`
余厌 2025-02-08 08:59:38

这是因为tobytes()函数给出了未压缩的字节。您将使用PIL的保存功能保存到缓冲区中,然后上传。

output = io.BytesIO()
img.save(output, format='JPEG')

This is because the tobytes() function gives raw uncompressed bytes. You would have use PIL's save function to save into a buffer and then upload that.

output = io.BytesIO()
img.save(output, format='JPEG')
笛声青案梦长安 2025-02-08 08:59:38

它可能是图像。尝试使该领域空白。

It may be the Image.ANTIALIAS function; try leaving that field blank.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文