繁体   English   中英

如何调整图像大小并使用 python 保存它们?

[英]how can I resize images and save them with python?

我有一个以 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 的数量应自动创建。

我希望我理解你的问题是正确的。 此脚本将根据高度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)]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM