miracle2k / pyutils

My personal collection of Python utility routines.

Clone this repository (size: 159.5 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/miracle2k/pyutils/
commit 109: bb01b78e82b9
parent 108: 7392d5f46626
branch: pyutils
Fixed the cropping thumbnail algorithm. Not only had it a bunch of NameErrors before, the general approach was completely flawed in the first place.
Michael Elsdörfer / miracle2k
3 months ago

Changed (Δ556 bytes):

raw changeset »

pyutils/thumbnail.py (27 lines added, 17 lines removed)

Up to file-list pyutils/thumbnail.py:

@@ -93,25 +93,35 @@ def fit(image, new_width, new_height):
93
93
@_common
94
94
def crop(image, new_width, new_height):
95
95
    """Resize the source image to fit the requested thumbnail size,
96
    but crop when necessary to keep propertions the same.
96
    but crop when necessary to keep proportions the same.
97
98
    This can both scale down and enlarge images, due in case of the
99
    latter, cropping is probably not the right algorithm to do in the
100
    first place: You are first throwing away information, when you
101
    don't have enough of it in the first place.
97
102
    """
103
104
    # 1) Modify the bounding box of the original image to match the
105
    # requested thumb proportions.
98
106
    image_width, image_height = image.size
99
    thumb_ratio = new_width / float(new_height)
100
    image_ratio = image_width / float(image_height)
101
    if thumb_ratio < image_ratio:  # width needs to shrink
102
        top = 0
103
        bottom = image_height
104
        thumb_width = int(org_width * crop_ratio)
105
        left = (image_width - thumb_width) // 2
106
        right = left + thumb_width
107
    else:                          # height needs to shrink
108
        left = 0
109
        right = image_width
110
        thumb_height = int(image_width * thumb_ratio)
111
        top = (new_height - thumb_height) // 2
112
        bottom = top + thumb_height
113
    return image.crop((left, top, right, bottom)).\
114
        resize((new_width, new_height), Image.ANTIALIAS)
107
    target_width, target_height = \
108
        _ensure_proportions((new_width, new_height), (image_width, image_height),)
109
110
    # 2) Use the adjusted image size to crop the original, so that we
111
    # have a piece of the original that we can resize to the requested
112
    # thumbnail size without skewing the image.
113
    # Note: We calculate both the horizontal and vertical axis, but only
114
    # one of them will be cropped, due to ``_ensure_proportions`` only
115
    # changing either the width or the height.
116
    top = (image_height - target_height) // 2
117
    left = (image_width - target_width) // 2
118
    bottom = image_height - top
119
    right = image_width - left
120
    image = image.crop((left, top, right, bottom,))
121
122
    # 3) Resize to target size; we can be sure we are not skewing due
123
    # to the cropping we've done.
124
    return image.resize((new_width, new_height), Image.ANTIALIAS)
115
125
116
126
117
127
@_common