最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python的Pillow如何从二进制数据中读取图像数据

Python crifan 1908浏览 0评论
折腾:
【未解决】Python中实现二进制数据的图片的压缩
期间,对于Pillow读取图片数据,是从文件中。但是此处需要去搞清楚,Pillow如何从二进制数据中读取进来。
Reference — Pillow (PIL Fork) 5.3.0 documentation
Image Module — Pillow (PIL Fork) 5.3.0 documentation
PIL Package (autodoc of remaining modules) — Pillow (PIL Fork) 5.3.0 documentation
python  pillow load image from binary data
python – Get binary image data from PIL.Image? – Stack Overflow
Python Script to convert Image into Byte array – Stack Overflow
Python PIL bytes to Image – Stack Overflow
“>>> from PIL import Image
>>> import cStringIO as StringIO
>>> stream = StringIO.StringIO(b_data)
>>> img = Image.open(stream)
>>> img.size
(320, 240)”
python – Open PIL image from byte file – Stack Overflow
“from PIL import Image
import io
image_data = … # byte values of the image
image = Image.open(io.BytesIO(image_data))”
【总结】
最后用代码:
import io
from PIL import Image

fileBytes = fileObj.read()
log.debug("len(fileBytes)=%s", len(fileBytes))

if fileType == MongoFileType.IMAGE.value:
    imageStream = io.BytesIO(fileBytes)
    imageFile = Image.open(imageStream)
    log.debug("imageFile=%s", imageFile)
    log.debug("imageFile.size=%s", imageFile.size)
可以实现从 二进制数据中读取进来Pillow的Image:
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=3543x3543 at 0x10D5DC1D0>
<_io.BytesIO object at 0x10b984d00>

转载请注明:在路上 » 【已解决】Python的Pillow如何从二进制数据中读取图像数据

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.207 seconds, using 22.13MB memory