librelist archives

« back to archive

PIL to create thumbnails automatically using tag

PIL to create thumbnails automatically using tag

From:
Alex
Date:
2011-01-26 @ 09:59
Hello,

I'm a bit new to Flask and Python, what could be the best way to
automatically create create links to resized images that are saved in flask
app based on image url provided?

For example {{ some_func('http://somesite.com/image.jpg', '140', '90',
'70%')

Thanks,
Alex

Re: [flask] PIL to create thumbnails automatically using tag

From:
Julien
Date:
2011-01-26 @ 12:37
You can create something like this

import Image, os, sys

@app.route("/thumbs")
def thumbs():

     # not good (type of values) but something like this
     file = request.args.get('file', None)
     width = request.args.get('width', 120)
     height = request.args.get('height', 90)
     quality = request.args.get('height', 70)
     crop = request.args.get('crop', False)

     # image
     img = Image.open(file)
     newpath = ""
     out = file(newpath, "w")
     resize(img, (width, height), crop, out, quality)

     return send_file(out)



def resize(img, box, fit, out, quality=75):
     '''Downsample the image.

     script example :
 >>> import Image, os, sys
 >>> for filename in sys.argv[1:]:
 >>>     img = Image.open(filename).resize( (200,200) )
 >>>     out = file(os.path.splitext(filename)[0]+"_thumb.jpg", "w")
 >>>     try:
 >>>         img.save(out, "JPEG")
 >>>     finally:
 >>>         out.close()

     @param img: Image -  an Image-object
     @param box: tuple(x, y) - the bounding box of the result image
     @param fix: boolean - crop the image to fill the box
     @param out: file-like-object - save the image into the output stream
     '''
     #preresize image with factor 2, 4, 8 and fast algorithm
     factor = 1
     while img.size[0] / factor > 2 * box[0] and img.size[1] * 2 / 
factor > 2 * box[1]:
         factor *= 2
     if factor > 1:
         img.thumbnail((img.size[0] / factor, img.size[1] / factor), 
Image.NEAREST)

     #calculate the cropping box and get the cropped part
     if fit:
         x1 = y1 = 0
         x2, y2 = img.size
         wRatio = 1.0 * x2 / box[0]
         hRatio = 1.0 * y2 / box[1]
         if hRatio > wRatio:
             y1 = y2 / 2-box[1] * wRatio / 2
             y2 = y2 / 2 + box[1] * wRatio / 2
         else:
             x1 = x2 / 2-box[0] * hRatio / 2
             x2 = x2 / 2 + box[0] * hRatio / 2
         img = img.crop((x1, y1, x2, y2))

     #Resize the image with best quality algorithm ANTI-ALIAS
     img.thumbnail(box, Image.ANTIALIAS)

     #save it into a file-like object
     img.save(out, "JPEG", quality=75)
#resize

and call it from your template:

<a href="{{ imagepath }}">
<img src="{{ url_for('thumbs', file=imagepath, width=120, height=90) }}" 
alt="" />
</a>

You have to adapt this, it's just a big approximation.

Julien C.

On 26/01/2011 10:59, Alex wrote:
> Hello,
>
> I'm a bit new to Flask and Python, what could be the best way to 
> automatically create create links to resized images that are saved in 
> flask app based on image url provided?
>
> For example {{ some_func('http://somesite.com/image.jpg', '140', '90', 
> '70%')
>
> Thanks,
> Alex

Re: [flask] PIL to create thumbnails automatically using tag

From:
Alex
Date:
2011-01-26 @ 17:10
OK, thanks.

Now I see the point. I will also need to add caching (saving of thumb to a
disk and getting it from the disk next time).

Probably I'll do MD5 over image url - save it on disk and next time do MD5
over the image name url again and check if this hashed image name is already
on the disk.

-Alex

On Wed, Jan 26, 2011 at 7:37 AM, Julien <jc63@free.fr> wrote:

> You can create something like this
>
> import Image, os, sys
>
> @app.route("/thumbs")
> def thumbs():
>
>     # not good (type of values) but something like this
>     file = request.args.get('file', None)
>     width = request.args.get('width', 120)
>     height = request.args.get('height', 90)
>     quality = request.args.get('height', 70)
>     crop = request.args.get('crop', False)
>
>     # image
>     img = Image.open(file)
>     newpath = ""
>     out = file(newpath, "w")
>     resize(img, (width, height), crop, out, quality)
>
>     return send_file(out)
>
>
>
> def resize(img, box, fit, out, quality=75):
>     '''Downsample the image.
>
>     script example :
>  >>> import Image, os, sys
>  >>> for filename in sys.argv[1:]:
>  >>>     img = Image.open(filename).resize( (200,200) )
>  >>>     out = file(os.path.splitext(filename)[0]+"_thumb.jpg", "w")
>  >>>     try:
>  >>>         img.save(out, "JPEG")
>  >>>     finally:
>  >>>         out.close()
>
>     @param img: Image -  an Image-object
>     @param box: tuple(x, y) - the bounding box of the result image
>     @param fix: boolean - crop the image to fill the box
>     @param out: file-like-object - save the image into the output stream
>     '''
>     #preresize image with factor 2, 4, 8 and fast algorithm
>     factor = 1
>     while img.size[0] / factor > 2 * box[0] and img.size[1] * 2 /
> factor > 2 * box[1]:
>         factor *= 2
>     if factor > 1:
>         img.thumbnail((img.size[0] / factor, img.size[1] / factor),
> Image.NEAREST)
>
>     #calculate the cropping box and get the cropped part
>     if fit:
>         x1 = y1 = 0
>         x2, y2 = img.size
>         wRatio = 1.0 * x2 / box[0]
>         hRatio = 1.0 * y2 / box[1]
>         if hRatio > wRatio:
>             y1 = y2 / 2-box[1] * wRatio / 2
>             y2 = y2 / 2 + box[1] * wRatio / 2
>         else:
>             x1 = x2 / 2-box[0] * hRatio / 2
>             x2 = x2 / 2 + box[0] * hRatio / 2
>         img = img.crop((x1, y1, x2, y2))
>
>     #Resize the image with best quality algorithm ANTI-ALIAS
>     img.thumbnail(box, Image.ANTIALIAS)
>
>     #save it into a file-like object
>     img.save(out, "JPEG", quality=75)
> #resize
>
> and call it from your template:
>
> <a href="{{ imagepath }}">
> <img src="{{ url_for('thumbs', file=imagepath, width=120, height=90) }}"
> alt="" />
> </a>
>
> You have to adapt this, it's just a big approximation.
>
> Julien C.
>
> On 26/01/2011 10:59, Alex wrote:
> > Hello,
> >
> > I'm a bit new to Flask and Python, what could be the best way to
> > automatically create create links to resized images that are saved in
> > flask app based on image url provided?
> >
> > For example {{ some_func('http://somesite.com/image.jpg', '140', '90',
> > '70%')
> >
> > Thanks,
> > Alex
>
>