1

I have a image_size list saved in Tuple type. I would like to check the size of heights less than 2470 pixel and make new list file and save them.

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

But the code has some errors so I cannot get the right result. The result value that I expect is as follows.

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)]

the number of Image_size should be created automatically.

문정아
  • 141
  • 11
  • Does this answer your question? [How to resize an image with OpenCV2.0 and Python2.6](https://stackoverflow.com/questions/4195453/how-to-resize-an-image-with-opencv2-0-and-python2-6) – Tom Wojcik Jul 21 '20 at 11:07

1 Answers1

1

I hope I understood your question right. This script will split the image_size list according the height 2470:

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)

Prints:

[[(800, 1200), (820, 700), (850, 300), (900, 200)],
 [(760, 1900)],
 [(820, 2000)],
 [(830, 1300), (900, 400), (300, 600)],
 [(190, 200)]]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91