[Image-SIG] ImageOps.fit(): centering parameter (PIL 1.1.7)

Olaf Dietrich olaf at dtrx.de
Wed Aug 4 16:14:02 CEST 2010


Olaf Dietrich (2010-07-30T09:42:47+0200):
> Does the centering parameter of ImageOps.fit() really
> work as intended (i.e. as described in the documentation)?

Meanwhile, I've had a closer look at ImageOps.fit() and start to
understand what's going on:

  # Returns a sized and cropped version of the image, cropped to the
  # requested aspect ratio and size.
  def fit(image, size, method=Image.NEAREST, bleed=0.0, centering=(0.5, 0.5)):


1) Cropping is always performed if the requested size ("size") has
   another aspect ratio than the original image.size. In particular,
   cropping is then performed even though the bleed-parameter
   might be zero (the default).

2) If cropping is performed because of differing aspect ratios, then
   it can be controlled by the centering-parameter.

3) If additional cropping is requested by setting the bleed-parameter
   to a value >0 (and <0.5), then this additional border is always
   removed symmetrically from the original image - this cannot
   be influenced by the centering parameter (this is what I originally
   expected the centering-parameter to do).
   I guess this behavior might be sensible as well, so I do not
   ask for any changes here.


4) (an independent observation:) There is a a wrong decrement by 1
   when calculating the liveArea (or the liveSize).


So, I suggest to improve the documentation and to correct the
definition of liveArea, i.e., change

# @param size The requested output size in pixels, given as a
#     (width, height) tuple.

to e.g.

# @param size The requested output size in pixels, given as a
#     (width, height) tuple.  If the aspect ratio of size differs
#     from the original one, the image data is cropped appropriately.


Change

# @param centering Control the cropping position.  Use (0.5, 0.5) for
#     center cropping (e.g. if cropping the width, take 50% off of the
...

to e.g.

# @param centering Control the cropping position.  This parameter is
#     relevant only if a changed aspect ratio is requested by the chosen
#     size. Any additionally removed border pixels (if bleed is > 0)
#     are not influenced by the centering parameter, but are always
#     removed symmetrically from the image.  Use (0.5, 0.5) for
#     center cropping (e.g. if cropping the width, take 50% off of the
...


And finally correct:

    liveArea = (
        bleedPixels[0], bleedPixels[1], image.size[0] - bleedPixels[0] - 1,
        image.size[1] - bleedPixels[1] - 1
        )
to

    liveArea = (
        bleedPixels[0], bleedPixels[1], image.size[0] - bleedPixels[0],
        image.size[1] - bleedPixels[1]
        )


Unfortunately, I was really looking for a function that does
asymmetric cropping of any border pixels as well, so I will
have to implement my own version of fit() ... 


Olaf


More information about the Image-SIG mailing list