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?
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.
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.
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.
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.
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!
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.
.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.
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With