diff --git a/sigal/image.py b/sigal/image.py index 7e170d6..606f514 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -48,12 +48,25 @@ class Image: self.img.save(filename, quality=quality) def resize(self, size): - "resize image" + """Resize the image + + - check if the image format is portrait or landscape and adjust `size`. + - compute the width and height ratio, and keep the min to resize the + image inside the `size` box without distorting it. + """ if self.img.size[0] > self.img.size[1]: - self.img = self.img.resize(size, PILImage.ANTIALIAS) + newsize = size else: - self.img = self.img.resize([size[1], size[0]], PILImage.ANTIALIAS) + newsize = (size[1], size[0]) + + wratio = newsize[0] / float(self.img.size[0]) + hratio = newsize[1] / float(self.img.size[1]) + ratio = min(wratio, hratio) + newsize = (int(ratio*self.img.size[0]), int(ratio*self.img.size[1])) + + if ratio < 1: + self.img = self.img.resize(newsize, PILImage.ANTIALIAS) def add_copyright(self, text): "add copyright to image" diff --git a/tests/sample/dir1/test1.jpg b/tests/sample/dir1/test1.jpg index 38854eb..0b60190 100644 Binary files a/tests/sample/dir1/test1.jpg and b/tests/sample/dir1/test1.jpg differ