【问题标题】:How to convert a string tensor to a PIL Image?如何将字符串张量转换为 PIL 图像?
【发布时间】:2022-06-16 16:02:39
【问题描述】:

我有一个字符串张量对象,它是通过在图像张量(Documentation for encode_jpeg) 上调用tf.io.encode_jpeg 生成的。

如何将此字符串 Tensor 转换为 PIL Image?

我尝试调用Image.fromarray(encoded_tensor.numpy()),但返回的是AttributeError: 'bytes' object has no attribute '__array_interface__'

【问题讨论】:

  • 你能发布用于转换的完整代码吗?

标签: python tensorflow python-imaging-library


【解决方案1】:

您似乎有一个内存中(而不是磁盘上)JPEG 编码的图像。您可以通过打印缓冲区的开头来检查这是否正确:

print(encoded_tensor[:10])

并查看它是否以 JPEG 幻数 ff d8 ff 开头。

如果是这样,您需要将其包装在 BytesIO 中并将其打开到 PIL Image 中:

from io import BytesIO
from PIL import Image

im = Image.open(BytesIO(encoded_tensor))

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 1970-01-01
    • 2023-03-04
    • 2018-08-04
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 2018-04-27
    • 2018-12-14
    相关资源
    最近更新 更多