【问题标题】:how can I resize images and save them with python?如何调整图像大小并使用 python 保存它们?
【发布时间】:2020-11-10 18:04:48
【问题描述】:

我有一个以 Tuple 类型保存的 image_size 列表。我想检查小于 2470 像素的高度大小并制作新的列表文件并保存。

Image_size = [(800,1200), (820, 700), (850, 300), (900, 200), (760, 1900), (820, 2000), 
(830, 1300), (900, 400), (300, 600), (190, 200)]
widths, heights = zip(*(Image_size))


i = 0
j = 0

while sum(heights[j:i+2]) < 2470:
    i = i +1
    if sum(heights[j:i+2]) > 2470:
        Image_size[i] = Image_size[j:i+1]

        j = i + 1

但是代码有一些错误,所以我无法得到正确的结果。 我期望的结果值如下。

Image_size1 = [(800,1200), (820, 700), (850, 300), (900, 200)]
Image_size2 = [(760, 1900)]
Image_size3 = [(820, 2000)]
Image_size4 = [(830, 1300), (900, 400), (300, 600)]
Image_size5 = [(190, 200)]

Image_size 的数量应该是自动创建的。

【问题讨论】:

标签: python list variables while-loop sum


【解决方案1】:

我希望我正确理解了您的问题。该脚本会根据2470的高度拆分image_size列表:

image_size = [(800,1200), (820, 700), (850, 300), (900, 200), (760, 1900), (820, 2000), (830, 1300), (900, 400), (300, 600), (190, 200)]

out, l, curr_height = [], [], 0
for w, h in image_size:
    curr_height += h
    if curr_height < 2470:
        l.append((w, h))
    else:
        out.append(l)
        l, curr_height = [(w, h)], h
if l:
    out.append(l)

from pprint import pprint
pprint(out)

打印:

[[(800, 1200), (820, 700), (850, 300), (900, 200)],
 [(760, 1900)],
 [(820, 2000)],
 [(830, 1300), (900, 400), (300, 600)],
 [(190, 200)]]

【讨论】:

    猜你喜欢
    • 2015-11-14
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多