Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python PIL image how to save image to a buffer so can be used later?

I have a png file which should be convert to jpg and save to gridfs , I use python's PIL lib to load the file and do the converting job, the problem is I want to store the converted image to a MongoDB Gridfs, in the saving procedure, I can't just use the im.save() method. so I use a StringIO to hold the temp file but it don't work.

here is the code snippet:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PIL import Image
from pymongo import MongoClient
import gridfs
from StringIO import StringIO


im = Image.open("test.png").convert("RGB")

#this is what I tried, define a 
#fake_file with StringIO that stored the image temporarily.
fake_file = StringIO()
im.save(fake_file,"jpeg")

fs = gridfs.GridFS(MongoClient("localhost").stroage)

with fs.new_file(filename="test.png") as fp:
    # this will not work
    fp.write(fake_file.read())


# vim:ai:et:sts=4:sw=4:

I am very verdant in python's IO mechanism, How to make this work?

like image 852
armnotstrong Avatar asked Jan 23 '15 03:01

armnotstrong


People also ask

How do I save an image in Python PIL?

save() Saves this image under the given filename. If no format is specified, the format to use is determined from the filename extension, if possible. Keyword options can be used to provide additional instructions to the writer.

How do I copy a PIL image?

Python PIL | copy() method PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL. Image. copy() method copies the image to another image object, this method is useful when we need to copy the image but also retain the original.

What is image Fromarray?

fromarray - Function. 1.1.1 Construct a CASA image from a numerical (integer or float) array. This function converts a numerical (integer or float) numpy array of any size and dimensionality into a CASA image. It will create both float and complex valued images.

What is the use of PiL image from buffer?

PIL.Image.frombuffer () Creates an image memory referencing pixel data in a byte buffer. 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.

What is the use of PIL in Python?

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images. Attention geek!

How to save an image in PIL?

Saving an image can be achieved by using .save () method with PIL library’s Image module in Python. To open an image in PIL you need to first import the Image module from the PIL library in Python. PIL imaging library is pretty smart.

How do I save an image in Python?

.save() method from PIL’s Image module After you worked on an image, do some image processing or image manipulation you will likely want to save this new version of your image. Saving an image can be achieved by using .save() method with PIL library’s Image module in Python.


1 Answers

Use the getvalue method instead of read:

with fs.new_file(filename="test.png") as fp:
    fp.write(fake_file.getvalue())

Alternatively, you could use read if you first seek(0) to read from the beginning of the StringIO.

with fs.new_file(filename="test.png") as fp:
    fake_file.seek(0)
    fp.write(fake_file.read())
like image 84
unutbu Avatar answered Sep 19 '22 14:09

unutbu