gpt4 book ai didi

python - 使用 Python 图像库缩放

转载 作者:太空狗 更新时间:2023-10-29 21:47:53 25 4
gpt4 key购买 nike

我正在用 Python 编写一个显示图像的简单应用程序。我需要通过缩放图像来实现放大和缩小。
我认为 Image.transform 方法可以做到这一点,但我不确定如何使用它,因为它需要一个 affine 矩阵或类似的东西:P
这是文档中的引述:

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

Applies an 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.

最佳答案

使用 EXTENT 而不是 AFFINE 方法会更好。你只需要计算两件事:你想看到输入的哪一部分,以及它应该有多大。例如,如果你想看到整个图像缩小到一半大小(即缩小 2),你将传递数据 (0, 0, im.size[0], im.size[1]) 和尺寸(im.size[0]/2, im.size[1]/2)。

关于python - 使用 Python 图像库缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3368740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com