[英]How to read all images from subfolder, resize them and save them with their original name in 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.