Re: [flask] Using GridFS for images and resizing
- dorian i
- 2012-07-12 @ 12:35
3.2: PIL is http://www.pythonware.com/products/pil/ . The documentation is
You can search on stackoverflow and im sure there are hundreds of tutorials
all over the web with pil
3.3-3.5. The image that you transformed with PIL store it just like the
original image in gridfs. The special here is the "key" or id that you will
query the image. (note that i haven't worked with gridfs but the pseudocode
Suppose that you want to scale the image to an x width and y height. If you
store the original image with _id:"1223344" (maybe you have objectid).
The transformed image will have an _id of : _id:"1223344" + width + height
#try to get the image from cache
resized_image = GridFS.get(id + width + height) #maybe you should
make str(width) if you store the key as string
except DocumentNotFound: #you havent resized the image previously
image = GridFS.get(id)#get the original image #get original image
resized_image = PIL.transform(image,width,height) #resize or
transform the image
GridFS.save(resized_image,id+width+height)# save the transformed
image with the new id
# What happens when you delete the original image? You don't want
to have left over the transformed images. So you save the _id(s) of the
transformed images in an array on the original image.
original_image.derivatives.push = id+width+height #so you push the
id on the derivative key
#when you delete an original image
original = GridFS.get(id)
for image in original.gridfs:
GridFS.delete(image) #this deletes all the derivatives
Hope this is clear.
On Thu, Jul 12, 2012 at 1:00 PM, Delilah Parks <email@example.com> wrote:
> Thanks for the feedback Dorian. Do you or anyone else have any code
> examples for 3.2 - 3.5?
> On Mon, Jul 9, 2012 at 4:53 PM, dorian i <firstname.lastname@example.org> wrote:
>> I don't understand the problem. Shouldn't it be like this:
>> 1.Store original image in gridfs.
>> 2.Original image requested? Just query and return it.
>> 3.Resized image requested?:
>> 3.0. Check if you have stored the transformed image in cache. (you
>> usually don't want to resize the image in every request)
>> If you want the cache to expire you can use the new TTL (in the new
>> version) (http://docs.mongodb.org/manual/tutorial/expire-data/)(i dont
>> think you need this)
>> Store the id of the resized image that you saved in an array in the
>> original image document so when you delete the original you have some extra
>> code to also delete all the resized copies of that image.
>> 3.1. Get the original image.
>> 3.2. Transform the image using pil or whatever.
>> 3.3. Store the resized image in the cache.
>> 3.4. Return the transformed image.
>> 3.5. If you replace the original image delete the cache documents also.