librelist archives

« back to archive

[flask] Using GridFS for images and resizing

[flask] Using GridFS for images and resizing

From:
Delilah Parks
Date:
2012-07-07 @ 11:09
I am using mongodb ... specifically gridfs for images. I am now stuck on
how to dynamically resize images and still use them within gridfs.
Has any one dode this or know where I can find an example. I have searched
every where but I can't seem to wrap my head around the concept.

Any advise or help would be appreciated. Thanks all!
Delilah

UPDATE: Forgot to change the subject on reply. My apologies.

Re: [flask] Using GridFS for images and resizing

From:
dorian i
Date:
2012-07-09 @ 20:53
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.

Re: [flask] Using GridFS for images and resizing

From:
Delilah Parks
Date:
2012-07-12 @ 11:00
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 <dorian.hoxha@gmail.com> 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.
>
>
>

Re: [flask] Using GridFS for images and resizing

From:
dorian i
Date:
2012-07-12 @ 12:35
3.2: PIL is http://www.pythonware.com/products/pil/ . The documentation is
here: http://www.pythonware.com/library/pil/handbook/index.htm
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
should work)
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

def get_resized_image(id,width,height):
#try to get the image from cache
    try:
        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
    return resized_image


#when you delete an original image
def original_delete(id):
    original = GridFS.get(id)
    for image in original.gridfs:
        GridFS.delete(image) #this deletes all the derivatives
    original.delete()

Hope this is clear.

On Thu, Jul 12, 2012 at 1:00 PM, Delilah Parks <delilahtp@gmail.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 <dorian.hoxha@gmail.com> 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.
>>
>>
>>
>