Python自动缩放算法?

Python auto-scaling algorithm?

我制作的简单图像查看器有问题。你看,我想让用户选择的图片根据图片的宽度和高度放大或缩小。因此,一张 1920 x 1080 的图片将缩小到可管理的尺寸,但一张 40 x 40 的图片看起来足够大,可以看得很清楚。

这个比例因子我更喜欢在单个变量中并且不使用 IF 语句,因为执行 IF pic_h > 100: 等等......你明白了。

这是我所在的位置:

for x in range(int(pic_w)):
    xline = target.readline()
    for y in range(int(pic_h)):
        pixel_data = xline[y:y+1]

        sf = <THIS IS THE BIT I NEED>
        y1b = (y*2)+0
        x1b = (x*2)+0
        y2b = (y*2)+2
        x2b = (x*2)+2

        y1w = (y*2)+0
        x1w = (x*2)+0
        y2w = (y*2)+2
        x2w = (x*2)+2

        if pixel_data == "1":
            pixel = canvas.create_rectangle(y1b, x1b, y2b, x2b, fill="black")
        elif pixel_data == "0":
            pixel = canvas.create_rectangle(y1b, x1b, y2b, x2b, fill="white")
        else:
            blah blah blah ............

变量pic_h和pic_w是图片的高度和宽度。

width, height = maximum size in pixels
x, y = number of pixels

maxSize = min(width, height)
width = = (maxSize//x)*x
height = (maxSize//y)*y

xScale = (width/x)
yScale = (height/y)

for row in xrange(y):
  for column in xrange(x):
    canvas.create_rectangle((column * xScale, row * yScale, (column * xScale)+xScale, (row * yScale)+yScale), fill=data)