10

I have python code that needs to do just a couple simple things to photographs: crop, resize, and overlay a watermark. I've used PIL, and the resample/resize results are TERRIBLE. I've used imagemagick, and the interface and commands were designed by packaging a cat in a box, and then repeatedly throwing it down a set of stairs at a keyboard.

I'm looking for something which is not PIL or Imagemagick that I can use with python to do simple, high-quality image transformations. For that matter, it doesn't even have to have python bindings if the command line interface is good.

Oh, and it needs to be relatively platform agnostic, our production servers are linux, but some of our devs develop on windows. It can't require the installation of a bunch of silly gui code to use as a library, either.

ilya n.
  • 18,398
  • 15
  • 71
  • 89
Paul McMillan
  • 19,693
  • 9
  • 57
  • 71
  • 1
    You'll be looking a while. PyMagick is the only thing out there other than PIL. – Matthew Talbert Sep 18 '09 at 00:14
  • 3
    How do people find PIL acceptable? Any resizing operation (even downsampling) produces artifacts and blurriness, even with the ANTIALIAS resampling setting. I've tried for hours to make it produce results that I would accept, but compared to desktop tools like Photoshop or GIMP, PIL's resizing is pathetic. – Gabriel Hurley Sep 18 '09 at 00:19
  • 1
    I use resize with antialias to generate, thumbnails for a document generation app, and they are comparable to what acrobat pdf viewer does – Anurag Uniyal Sep 18 '09 at 04:17

9 Answers9

18

I've used PIL, and the resample/resize results are TERRIBLE.

They shouldn't be, as long as you:

  1. use only Image.ANTIALIAS filtering for downscaling operations
  2. use only Image.BICUBIC filtering for upscaling operations.
  3. remember to convert to 'RGB' colour mode before the resize if you are using a paletted image
  4. don't use .thumbnail(). it's crap
  5. set the quality= level to something appropriate when saving JPEGs (the default is quite low)
bobince
  • 528,062
  • 107
  • 651
  • 834
  • You know, after some further testing, the results *are* better with resize as opposed to thumbnail, even with both using the Image.ANTIALIAS algorithm. If I think of it later I'll post some sample results of various operations. – Gabriel Hurley Sep 18 '09 at 02:38
  • 1
    See http://stackoverflow.com/questions/1386400/pil-image-resizing-algorithm-similar-to-firefoxs — it doesn't quite test like against like but there is clearly something quite wrong with what thumbnail() is doing. – bobince Sep 18 '09 at 11:02
  • Just wanted to reinforce point 5. It's easy to miss, and it's the primary cause of the problems this question was originally complaining about. – Paul McMillan May 30 '11 at 21:20
2

I'm unsure as to why Image.thumbnail is getting such flak. In the present release that I'm running off of it does little more than figure out the desired size and resize the image in place. As long as you're using the proper resample filter and convert to RGB first (as bobince says) thumbnail shouldn't be any different than resize.

Here's the actual source for the thumbnail method:

def thumbnail(self, size, resample=NEAREST):
  # preserve aspect ratio
  x, y = self.size
  if x > size[0]: y = max(y * size[0] / x, 1); x = size[0]
  if y > size[1]: x = max(x * size[1] / y, 1); y = size[1]
  size = x, y

  if size == self.size:
      return

  self.draft(None, size)

  self.load()

  try:
      im = self.resize(size, resample)
  except ValueError:
      if resample != ANTIALIAS:
          raise
      im = self.resize(size, NEAREST) # fallback

  self.im = im.im
  self.mode = im.mode
  self.size = size

  self.readonly = 0
schickm
  • 175
  • 1
  • 6
1

PIL can do good resizing. Make sure your source image is in RGB mode, not palette colors, and try the different algorithm choices.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • I've always used it on plain ol' JPEGs. Image.open('myfile.jpg'), then Image.thumbnail or Image.resize, followed by Image.save... All with the ANTIALIAS setting for downsampling. That should be in RGB mode by default (jpegs are RGB) and with the best algorithm for downsampling, right? If I'm missing something I would love to know what I've got wrong... – Gabriel Hurley Sep 18 '09 at 00:28
  • Without seeing the source code and the source images, it's hard to say what's going on. Lots of people use PIL for this kind of work and are happy with the results. Something is different about your case (do you have especially eagle eyes?), but we haven't figured out what yet. – Ned Batchelder Sep 18 '09 at 01:05
1

While imagemagick seems to be the de facto open-source imaging library, possibly DevIL (cross platform, seems to do simple image operations) or FreeImage.

John Paulett
  • 15,596
  • 4
  • 45
  • 38
1

Have you checked pypi? A cursory search shows some image related tools there, I also discovered python-gd, no clue how useful it might be though.

I've never had any issues with PIL myself, but some kind of variety might be interesting.

tvon
  • 1,513
  • 1
  • 12
  • 16
1

GIMP has a reasonable command-line interface, I think.

ilya n.
  • 18,398
  • 15
  • 71
  • 89
1

Take a look at some of these imaging libraries:

hxxp://pypi.python.org/pypi/collective.croppingimagefield/0.1beta

hxxp://pypi.python.org/pypi/cropresize/0.1.1

hxxp://pypi.python.org/pypi/image_resize/1.0

James
  • 69
  • 2
1

I've used PIL, and the resample/resize results are TERRIBLE.

Resizing in PIL was broken in many ways and PIL is not maintained for a long time. Starting from Pillow 2.7 most of the problems are fixed along with dramatically performance improvement. Make sure you are using latest Pillow.

homm
  • 2,102
  • 1
  • 16
  • 33
0

Last time I compared, this downscaler's output is almost identical to that of GIMP's "cubic" option:

 import Image

 def stretch(im, size, filter=Image.NEAREST):
     im.load()
     im = im._new(im.im.stretch(size, filter))
     return im

IIRC, the differences are visually indistinguishable -- some pixel values +/-1 due to rounding, and they tend to be round the edges. It's not slow either.

cf: http://www.mail-archive.com/image-sig@python.org/msg00248.html

Douglas Bagnall
  • 796
  • 7
  • 7