【问题标题】:PIL fromstring errorPIL fromstring 错误
【发布时间】:2012-01-12 19:26:11
【问题描述】:

我有一张 png 图片,我需要将它保存为字符串,然后用 PIL 再次打开它。我正在尝试这样做:

output = StringIO.StringIO()
old_image.save(output, format="PNG")
contents = output.getvalue()
output.close()

new_image = Image.fromstring(contents, "RGBA", old_image.size)

但它给了我一个错误:TypeError: 'argument 1 must be string without null bytes, not str'

如何解决这个问题?

【问题讨论】:

    标签: python python-imaging-library


    【解决方案1】:

    你的论点颠倒了:

    Image.fromstring(mode, size, data, decoder_name='raw', *args)
    

    所以

    Image.fromstring("RGBA", old_image.size, contents)
    

    但请注意,直接从StringIO 对象中读取要容易得多:

    output = StringIO.StringIO()
    old_image.save(output, format="PNG")
    
    output.seek(0)
    new_image = Image.open(output)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多