'Python pillow: read image content from a list of byte chunks and save to disk
I am coming into a scenario that I need to use python pillow to read image from a given list of byte chunks (e.g., an iterator with type Iterator[bytes]
), then save it to a location.
I hope I could come up with a method like this:
def save_image(chunks: Iterator[bytes]):
pil_image: Image = Image.open(chunks) # Here is where I am getting blocked
# ... some image operations
pil_image.save(SOME_PATH, format=pil_image.format)
But it seems that I didn't find any documents or other questions that can resolve my question, to allow python pillow to read images from a list of byte chunks. Any help would be really appreciated, thanks!
Solution 1:[1]
You will need to accumulate the chunks into a BytesIO
and pass the accumulated buffer as the sole parameter to Image.open()
.
Same concept as here.
Untested, but it will presumably look something like this:
from io import BytesIO
from PIL import Image
buf = BytesIO()
for chunk in chunks:
buf.write(chunk)
im = Image.open(buf)
If you are dealing with extremely large or numerous chunks, you might want to accumulate on disk rather than in memory:
with open('image.bin', 'wb') as fd:
for chunk in chunks:
fd.write(chunk)
im = Image.open('image.bin')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |