List Info

Thread: PIL resize with aspect ratio?




PIL resize with aspect ratio?
user name
2006-01-05 20:17:49
Thanks.  How about setting the JPEG compression setting?

Conceptually what I'm trying to do is the following:

Take an image file of arbitrary size and of arbitrary width
and height,
and rescale it to a width of exactly 500 and a height of
whatever,
as long as the aspect ratio is preserved.  Additionally, the
rescaled
image must be < 100kb in size, but I want to preserve as
much
quality as possible.  My thought was that if I could specify
the
JPEG compression ratio as I can with ImageMagick, I could
start
at a high value and iteratively go lower and lower until a
rescaled
image file is produced that is just under 100kb size.

On 1/5/06, Chris Cogdon <chriscogdon.org> wrote:
>
> On Jan 5, 2006, at 11:49, Count László de Almásy wrote:
>
> > Greetings,
> >
> > I'm new to PIL, and am wondering if the following
is possible.
> >
> > I want to resize an image by only specifying the
width, but not the
> > height.  The height should be expanded or
contracted automatically to
> > fit the width value while maintaining the aspect
ratio of the image.
> >
> > Second, I want to be able to specify the JPEG
compression level.
> >
> > If you are familiar with ImageMagick, these would
be accomplished
> > using the following for example:
> >
> > % mogrify -quality 95 -resize 500x big.jpg
> >
> > I can't figure out whether these things are
possible from the PIL
> > documentation or not.  Any ideas?
>
> i.thumbnail ( (128,128), Image.ANTIALIAS )
>
> this will create a thumbnail that is no more than
128x128, but will
> keep the original geometry.
>
>
> --
>     ("`-/")_.-'"``-._        Chris
Cogdon <chriscogdon.org>
>      . . `; -._    )-;-,_`)
>     (v_,)'  _  )`-.  ``-'
>    _.- _..-_/ / ((.'
> ((,.-'   ((,/   fL
>
>
_______________________________________________
Image-SIG maillist  -  Image-SIGpython.org
htt
p://mail.python.org/mailman/listinfo/image-sig
PIL resize with aspect ratio?
user name
2006-01-05 21:24:54
On Jan 5, 2006, at 12:17, Count László de Almásy wrote:

> Thanks.  How about setting the JPEG compression
setting?
>
> Conceptually what I'm trying to do is the following:
>
> Take an image file of arbitrary size and of arbitrary
width and height,
> and rescale it to a width of exactly 500 and a height
of whatever,
> as long as the aspect ratio is preserved. 
Additionally, the rescaled
> image must be < 100kb in size, but I want to
preserve as much
> quality as possible.  My thought was that if I could
specify the
> JPEG compression ratio as I can with ImageMagick, I
could start
> at a high value and iteratively go lower and lower
until a rescaled
> image file is produced that is just under 100kb size.

The thumbnail command just creates the thumbnail image in
memory, but 
doesn't 'compress' it yet. You do the compression when you
save it as a 
particular format, and then you can set the compression.
here's a more 
complete example:

i = Image.open ( path )
i.thumbnail ( (128,128), Image.ANTIALIAS )
i.save ( thumb_path, quality=75 )


-- 
    ("`-/")_.-'"``-._        Chris Cogdon
<chriscogdon.org>
     . . `; -._    )-;-,_`)
    (v_,)'  _  )`-.  ``-'
   _.- _..-_/ / ((.'
((,.-'   ((,/   fL

_______________________________________________
Image-SIG maillist  -  Image-SIGpython.org
htt
p://mail.python.org/mailman/listinfo/image-sig
PIL resize with aspect ratio?
user name
2006-01-05 22:25:10
Thanks so much, I had missed the 'quality' attribute.  BTW,
is
'ANTIALIAS' the best choice for what I want to do?  I
noticed the
docs mention the following:

"The BILINEAR and BICUBIC filters use a fixed input
environment, and
are best used for scale-preserving geometric transforms and
upsamping"

which seems like it suits rescaling while maintaining aspect
ratio as
I am doing.

On 1/5/06, Chris Cogdon <chriscogdon.org> wrote:
>
> On Jan 5, 2006, at 12:17, Count László de Almásy wrote:
>
> > Thanks.  How about setting the JPEG compression
setting?
> >
> > Conceptually what I'm trying to do is the
following:
> >
> > Take an image file of arbitrary size and of
arbitrary width and height,
> > and rescale it to a width of exactly 500 and a
height of whatever,
> > as long as the aspect ratio is preserved. 
Additionally, the rescaled
> > image must be < 100kb in size, but I want to
preserve as much
> > quality as possible.  My thought was that if I
could specify the
> > JPEG compression ratio as I can with ImageMagick,
I could start
> > at a high value and iteratively go lower and lower
until a rescaled
> > image file is produced that is just under 100kb
size.
>
> The thumbnail command just creates the thumbnail image
in memory, but
> doesn't 'compress' it yet. You do the compression when
you save it as a
> particular format, and then you can set the
compression. here's a more
> complete example:
>
> i = Image.open ( path )
> i.thumbnail ( (128,128), Image.ANTIALIAS )
> i.save ( thumb_path, quality=75 )
>
>
> --
>     ("`-/")_.-'"``-._        Chris
Cogdon <chriscogdon.org>
>      . . `; -._    )-;-,_`)
>     (v_,)'  _  )`-.  ``-'
>    _.- _..-_/ / ((.'
> ((,.-'   ((,/   fL
>
>
_______________________________________________
Image-SIG maillist  -  Image-SIGpython.org
htt
p://mail.python.org/mailman/listinfo/image-sig
PIL resize with aspect ratio?
user name
2006-01-05 22:28:31
On Jan 5, 2006, at 14:25, Count László de Almásy wrote:

> Thanks so much, I had missed the 'quality' attribute. 
BTW, is
> 'ANTIALIAS' the best choice for what I want to do?  I
noticed the
> docs mention the following:
>
> "The BILINEAR and BICUBIC filters use a fixed
input environment, and
> are best used for scale-preserving geometric transforms
and upsamping"
>
> which seems like it suits rescaling while maintaining
aspect ratio as
> I am doing.

I've found that ANTIALIAS results in much better thumbnails,
and will 
probably produce better looking results when scaling down.
The others I 
suspect will work well when scaling up.

Best way... try it out and see 


-- 
    ("`-/")_.-'"``-._        Chris Cogdon
<chriscogdon.org>
     . . `; -._    )-;-,_`)
    (v_,)'  _  )`-.  ``-'
   _.- _..-_/ / ((.'
((,.-'   ((,/   fL

_______________________________________________
Image-SIG maillist  -  Image-SIGpython.org
htt
p://mail.python.org/mailman/listinfo/image-sig
PIL resize with aspect ratio?
user name
2006-01-05 22:35:45
I'm actually using resize() instead of thumbnail() in the
code I'm writing,
but I think the idea is the same, right?

I'm color blind, so it's not as easy for me as most to
distinguish such
fine details by eye, but I think I'll use ANTIALIAS.

On 1/5/06, Chris Cogdon <chriscogdon.org> wrote:
>
> On Jan 5, 2006, at 14:25, Count László de Almásy wrote:
>
> > Thanks so much, I had missed the 'quality'
attribute.  BTW, is
> > 'ANTIALIAS' the best choice for what I want to do?
 I noticed the
> > docs mention the following:
> >
> > "The BILINEAR and BICUBIC filters use a fixed
input environment, and
> > are best used for scale-preserving geometric
transforms and upsamping"
> >
> > which seems like it suits rescaling while
maintaining aspect ratio as
> > I am doing.
>
> I've found that ANTIALIAS results in much better
thumbnails, and will
> probably produce better looking results when scaling
down. The others I
> suspect will work well when scaling up.
>
> Best way... try it out and see 
>
>
> --
>     ("`-/")_.-'"``-._        Chris
Cogdon <chriscogdon.org>
>      . . `; -._    )-;-,_`)
>     (v_,)'  _  )`-.  ``-'
>    _.- _..-_/ / ((.'
> ((,.-'   ((,/   fL
>
>
_______________________________________________
Image-SIG maillist  -  Image-SIGpython.org
htt
p://mail.python.org/mailman/listinfo/image-sig
PIL resize with aspect ratio?
user name
2006-01-05 22:55:55
On Jan 5, 2006, at 14:35, Count László de Almásy wrote:

> I'm actually using resize() instead of thumbnail() in
the code I'm 
> writing,
> but I think the idea is the same, right?

yes. resize does much the same thing, but you have to
control the new x 
and y values explicitly... you don't get the 'maintain
geometry' that 
'thumbnail' gives you.


-- 
    ("`-/")_.-'"``-._        Chris Cogdon
<chriscogdon.org>
     . . `; -._    )-;-,_`)
    (v_,)'  _  )`-.  ``-'
   _.- _..-_/ / ((.'
((,.-'   ((,/   fL

_______________________________________________
Image-SIG maillist  -  Image-SIGpython.org
htt
p://mail.python.org/mailman/listinfo/image-sig
[1-6]

about | contact  Other archives ( Real Estate discussion Medical topics )