最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Python如何从二进制数据中生成Pillow的Image

Python crifan 407浏览 0评论
折腾:
【未解决】Python中如何更新印象笔记中note笔记中的附件图片
期间,此处已经得到了note的单个resource是image,且有data是二进制数据:
想要:
从这个二级制数据中,生成Pillow的Image,用于后续resize。
pillow image from binary
Image Module — Pillow (PIL Fork) 3.1.2 documentation
PIL.Image与Base64 String的互相转换 – 简书
Python PIL | Image.frombuffer() method – GeeksforGeeks
pillow binary to image
python – Working with binary PNG images in PIL/pillow – Stack Overflow
python – Turning binary string into an image with PIL – Stack Overflow
Image.new
看到了:
PIL.Image.frombytes(mode, size, data, decoder_name=’raw’, *args)
PIL.Image.frombuffer(mode, size, data, decoder_name=’raw’, *args)
去试试PIL.Image.frombytes
Image file formats — Pillow (PIL Fork) 5.1.0 documentation
Python PIL | Image.open() method – GeeksforGeeks
Pillow (PIL Fork) Documentation
PIL.Image.frombytes(mode, size, data, decoder_name=’raw’, *args) Creates a copy of an image memory from pixel data in a buffer.
In its simplest form, this function takes three arguments (mode, size, and unpacked pixel data).
You can also use any pixel decoder supported by PIL. For more information on available decoders, see the
section Writing Your Own File Decoder.
Note that this function decodes pixel data only, not entire images. If you have an entire image in a string, wrap
it in a BytesIO object, and use open() to load it. Parameters
• mode – The image mode. See: Modes.
• size – The image size.
• data – A byte buffer containing raw data for the given mode. • decoder_name – What decoder to use.
• args – Additional parameters for the given decoder.
Returns An Image object. PIL.Image.fromstring(*args, **kw)
PIL.Image.frombuffer(mode, size, data, decoder_name=’raw’, *args) Creates an image memory referencing pixel data in a byte buffer.
This function is similar to frombytes(), but uses data in the byte buffer, where possible. This means that changes to the original buffer object are reflected in this image). Not all modes can share memory; supported modes include “L”, “RGBX”, “RGBA”, and “CMYK”.
Note that this function decodes pixel data only, not entire images. If you have an entire image file in a string, wrap it in a BytesIO object, and use open() to load it.
In the current version, the default parameters used for the “raw” decoder differs from that used for frombytes(). This is a bug, and will probably be fixed in a future release. The current release issues a
from PIL import Image
import numpy as np
im = Image.open('hopper.jpg') a = np.asarray(im)

48
Chapter3. Reference
Pillow (PIL Fork) Documentation, Release 6.2.1

warning if you do this; to disable the warning, you should provide the full set of parameters. See below for details.
Parameters
* mode – The image mode. See: Modes.
* size – The image size.
* data – A bytes or other buffer object containing raw data for the given mode.
* decoder_name – What decoder to use.
* args – Additional parameters for the given decoder. For the default encoder (“raw”), it’s recommended that you provide the full set of parameters:
frombuffer(mode, size, data, "raw", mode, 0, 1) Returns An Image object.
New in version 1.1.4.
参考:
/Users/crifan/.local/share/virtualenvs/EvernoteToWordpress-PC3x4gk8/lib/python3.7/site-packages/PIL/Image.py
def frombytes(mode, size, data, decoder_name="raw", *args):
用代码:
            imgData = eachResource.data
            imgDataBody = imgData.body
            imgDataSize = imgData.size
            logging.info("imgDataSize=%s", imgDataSize)

            curImg = Image.frombytes("RGBA", imgDataSize, imgDataBody)
结果报错:
    raise ValueError("Size must be a tuple")
ValueError: Size must be a tuple
所以此处size不是大小,而是宽度和高度
此处不清楚宽度和高度,所以放弃此办法。
算了,感觉还是想办法用Image.open吧
对于此处,直接从二进制中生成类似file的对象,应该就能用Image.open打开了
所以问题转换为:如何从二进制数据生成file对象
然后发现之前就解决过类似问题
crifanLib/crifanMultimedia.py
    elif isinstance(inputImage, bytes):
        inputImageLen = len(inputImage)
        openableImage = io.BytesIO(inputImage)

    imageFile = Image.open(openableImage) # <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=3543x3543 at 0x1065F7A20>
所以此处继续用
            fileTypeImageData = io.BytesIO(imgDataBody)
            # curImg = Image.frombytes("RGBA", imgDataSize, imgDataBody)
            curImg = Image.open(fileTypeImageData)
是可以的
20191214 10:47:52 EvernoteToWordpress.py:353  INFO    curMime=image/png
20191214 10:47:52 EvernoteToWordpress.py:355  INFO    matchImage=<re.Match object; span=(0, 6), match='image/'>
20191214 10:47:52 EvernoteToWordpress.py:365  INFO    imgDataSize=69889
20191214 10:48:47 EvernoteToWordpress.py:370  INFO    curImg=<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1080x1920 at 0x102423B70>
【总结】
此处是Python的bytes的图片的二进制数据,想要生成Pillow的Image
最后是:
之前就已经实现过的
crifanLibPython/crifanMultimedia.py at master · crifan/crifanLibPython · GitHub
elif isinstance(inputImage, bytes):
  openableImage = io.BytesIO(inputImage)
  imageFile = Image.open(openableImage)
变量类型是:
# <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=3543x3543 at 0x1065F7A20>
# <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1080x1920 at 0x1026D7278>
即可实现:从二进制数据生成Image。
后记:

转载请注明:在路上 » 【已解决】Python如何从二进制数据中生成Pillow的Image

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.183 seconds, using 23.35MB memory