开发者

Zooming With Python Image Library

开发者 https://www.devze.com 2023-01-10 00:38 出处:网络
I\'m writing a simple application in Python which displays images.I need to implement Zoom In and Zoom Out by scaling the image.

I'm writing a simple application in Python which displays images.I need to implement Zoom In and Zoom Out by scaling the image.

I think the Image.transform method will be able to do this, but I'm not sure how to use it, since it's asking for an affine matrix or something like that :P

Here's the quote from the docs:

im.transform(size, AFFINE, data, filter) => image

Applies an开发者_Go百科 affine transform to the image, and places the result in a new image with the given size.

Data is a 6-tuple (a, b, c, d, e, f) which contain the first two rows from an affine transform matrix. For each pixel (x, y) in the output image, the new value is taken from a position (a x + b y + c, d x + e y + f) in the input image, rounded to nearest pixel.

This function can be used to scale, translate, rotate, and shear the original image.


You would be much better off using the EXTENT rather than the AFFINE method. You only need to calculate two things: what part of the input you want to see, and how large it should be. For example, if you want to see the whole image scaled down to half size (i.e. zooming out by 2), you'd pass the data (0, 0, im.size[0], im.size[1]) and the size (im.size[0]/2, im.size[1]/2).


An affine transformation applies and linear transform followed by a translation. But you should only need to resize a portion of the image using the resize method. There's some sample code in the following SO answer:

  • Image viewer
0

精彩评论

暂无评论...
验证码 换一张
取 消