pil pil tobytes()方法后,图像被损坏
我想将图像上传到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 pillow
tobytes
:
因此,
tobytes()
方法返回枕头的内部表示图像,大概将使用frombytes()
恢复。如果要将映像保存为JPEG,请使用保存()
方法如文档所建议:From Pillow's documentation on
tobytes
:So the
tobytes()
method returns Pillow's internal representation of the image, presumably to be restored withfrombytes()
. If you want to save the image as JPEG, use thesave()
method as suggested by the documentation:这是因为tobytes()函数给出了未压缩的字节。您将使用PIL的保存功能保存到缓冲区中,然后上传。
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.
它可能是图像。尝试使该领域空白。
It may be the Image.ANTIALIAS function; try leaving that field blank.