缩放图像以保持位置Pi

2024-05-13 00:22:51 发布

您现在位置:Python中文网/ 问答频道 /正文

伙计们,我试着缩放一个图像,既缩小它又扩展它,但保持它在图像中的位置。让我解释一下。如下图所示: Image,我需要缩放它,但保持位置不变。在

我用下面的代码来做,但它没有保留位置。有什么想法吗?在

from PIL import Image, ImageChops, ImageOps,ImageStat, ImageFilter
def scaling_thumbline(image1):
    scale = Image.new("1", image1.size, "white");
    image1.show()
    x = image1.size[0]
    y = image1.size[1]
    print (x,y)
    image2 = image1.resize((x/2,y/2),Image.ANTIALIAS);
    image2.show()
    print image2.size
    for x in range(0,image2.size[0]):
        for y in range(0,image2.size[1]):
            scale.putpixel((x,y),image2.getpixel((x,y)));

   scale.show()
   print scale.size



image1 = Image.open("Problems/Basic Problems C/Basic Problem C-02/A.png").convert("1");
scaling_thumbline(image1)

我发现, Image,我需要缩放它,但保持位置不变。请注意,图像不是位于中间,而是移动到了顶部。在

有什么想法吗?帮忙吗?在


Tags: in图像imageforsizebasicshowrange
1条回答
网友
1楼 · 发布于 2024-05-13 00:22:51

我认为resize函数就是您要找的。我在macosxyosemite上使用python2.7.10测试了下面的代码,得到了正确的结果。在

>>> from PIL import Image
>>> a = Image.open('test.png')
>>> size = a.size
>>> newSize = tuple([dimension/10 for dimension in size])
>>> b = a.resize(newSize)
>>> a.show()
>>> b.show()

相关问题 更多 >