[Image-SIG] Trying to achive better quality resizing images

K Schutte schutte@fel.tno.nl
Tue, 05 Mar 2002 09:05:04 +0100


Dear Paulo,

The key is that the reduction in size does introduce aliasing in your
output. The remedy against aliasing is to first low-pass filter the image
before doing a reduction.

The standard filter ImageFilter.SMOOTH in PIL does a fairly good job on
low-pass filtering for a reduction in size of about a factor two. So
the following sequence:

$ python
Python 1.5.2 (#1, Jul  5 2001, 03:02:19)  [GCC 2.96 20000731 (Red Hat Linux 7.1
2 on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import Image
>>> im = Image.open('CentroOriginal.jpg')
>>> import ImageFilter
>>> print im.size
(1971, 1367)
>>> out = im.filter(ImageFilter.SMOOTH)
>>> out3 = out.resize((1971/2,1367/2))
>>> out4 = out3.filter(ImageFilter.SMOOTH)
>>> out5 = out4.resize((500, 346))
>>> out5.show()
>>> 

gave me a fairly decent resized image. I use ImageFilter.SMOOTH two times,
as it works for a reduction in size for a factor two, and you seem to want
to have a reduction in size of a factor 4 = 2 * 2.

Note that the OPTIMAL aliasing reduction is dependent on your application.
- For having the ability to reconstruct back towards a larger image you
  want to have frequency domain cutoff at Nyquist rate -- this is not in
  PIL, and will give not so nice visual results.
- For speed you might want to have a uniform filter. This is not available
  in a fast version in PIL.
- For nice visual results you want to have a Gaussian filter. A generic
  Gaussian filter is not available in PIL.
- As a fairly standard trade of you might want to have a small convolution
  approaching a Gaussian. This is what ImageFilter.SMOOTH approaches for
  a factor-two reduction in size.

Good luck,

Klamer

Paulo Eduardo Neves wrote:
> 
> I've noticed that when I resize my photograph jpg images with PIL I get
> a low quality out put.  See this:
> 
> The original image (big - 515K):
> http://www.samba-choro.com.br/s-c/imagens/teste/
> 
> Now resizing it to have a width of 500px with the same aspect ratio:
> http://www.samba-choro.com.br/s-c/imagens/teste/PILnearest.jpg
> http://www.samba-choro.com.br/s-c/imagens/teste/PILbilinear.jpg
> http://www.samba-choro.com.br/s-c/imagens/teste/PILbicubic.jpg
> I'm just opening the original image, and calling resize.
> 
> Now the same resize using ImageMagick:
> http://www.samba-choro.com.br/s-c/imagens/teste/CentroMagick.jpg
> 
> ImageMagick has, by far, the best output.
> 
> I've found an old thread about it in this list:
> http://aspn.activestate.com/ASPN/Mail/Message/583399
> 
> Fredrik Lundh suggests:
> http://aspn.activestate.com/ASPN/Mail/Message/583404
> 
> I've tried it and achieved:
> http://www.samba-choro.com.br/s-c/imagens/teste/iteracaofinal.jpg
> it also doesn't have a good enough quality.
> 
> Am I missing  something?
> 
> regards,
> --
> Paulo Eduardo Neves
> Agenda do Samba & Choro, o boteco virtual do samba e choro
> http://www.samba-choro.com.br
> 
> _______________________________________________
> Image-SIG maillist  -  Image-SIG@python.org
> http://mail.python.org/mailman/listinfo/image-sig

-- 
Klamer Schutte, E-mail: Schutte@fel.tno.nl
Electro-Optical Systems, TNO Physics and Electronics Laboratory
Tel: +31-70-3740469 -- Fax: +31-70-3740654 -- Mobile: +31-6-51316671