Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is my take on doing a padded fit for an image:</p> <pre><code>#!/usr/bin/env python from PIL import Image, ImageChops F_IN = "/path/to/image_in.jpg" F_OUT = "/path/to/image_out.jpg" size = (80,80) image = Image.open(F_IN) image.thumbnail(size, Image.ANTIALIAS) image_size = image.size thumb = image.crop( (0, 0, size[0], size[1]) ) offset_x = max( (size[0] - image_size[0]) / 2, 0 ) offset_y = max( (size[1] - image_size[1]) / 2, 0 ) thumb = ImageChops.offset(thumb, offset_x, offset_y) thumb.save(F_OUT) </code></pre> <p>It first uses the thumbnail operation to bring the image down to within your original bounds and preserving the aspect. Then it crops it back out to actually fill the size of your bounds (since unless the original image was square, it will be smaller now), and we find the proper offset to center the image. The image is offset to the center, so you end up with black padding but no image cropping. </p> <p>Unless you can make a really sensible guess at a proper center crop without losing possible important image data on the edges, a padded fit approach will work better.</p> <p><strong>Update</strong></p> <p>Here is a version that can do either center crop or pad fit.</p> <pre><code>#!/usr/bin/env python from PIL import Image, ImageChops, ImageOps def makeThumb(f_in, f_out, size=(80,80), pad=False): image = Image.open(f_in) image.thumbnail(size, Image.ANTIALIAS) image_size = image.size if pad: thumb = image.crop( (0, 0, size[0], size[1]) ) offset_x = max( (size[0] - image_size[0]) / 2, 0 ) offset_y = max( (size[1] - image_size[1]) / 2, 0 ) thumb = ImageChops.offset(thumb, offset_x, offset_y) else: thumb = ImageOps.fit(image, size, Image.ANTIALIAS, (0.5, 0.5)) thumb.save(f_out) source = "/path/to/source/image.JPG" makeThumb(source, "/path/to/source/image_padded.JPG", pad=True) makeThumb(source, "/path/to/source/image_centerCropped.JPG", pad=False) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload