【问题标题】:Is there a way to open an image as bytes and return it to a jpg file?有没有办法将图像作为字节打开并将其返回到 jpg 文件?
【发布时间】:2022-01-21 13:25:14
【问题描述】:
image = open(filepath, 'rb')
new_image = open("new_image_path.jpg", 'w', encoding="ISO-8859-1")
new_image.write(image.read().decode(encoding="ISO-8859-1"))

如果我在notepad++中打开它,我得到的新图像完全一样,但是新图像已损坏并且由于某种原因无法打开。

【问题讨论】:

  • 需要在wb模式下打开new_image才能写入二进制数据。
  • 非常感谢您,先生。

标签: python byte


【解决方案1】:

您还必须使用'wb',因为它不是文本文件。您不应该解码和编码,因为它不是文本文件。功能代码可以是:

image = open(filepath, 'rb')
new_image = open("new_image_path.jpg", 'wb')
new_image.write(image.read())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2017-07-05
    • 2022-01-24
    • 2021-02-12
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多