I have a folder that has a bunch of images. I'm trying to write a python script that will loop through each image and return the width/height and then append it to my dictionary. A simplified version of my dictionary looks like this:
in_metadata = {123: {labels:[1,2]}, 234: {labels:[2,3]}}
and what I want is this:
in_metadata = {123: {'labels':[1,2], 'bbox':[320,240,640,480]}, 234: {'labels':[2,3], 'bbox':[320,206,640,412]}, ...}
where the bbox = [center x, center y, w, h]
When the code goes into the first iteration of the loop, I get:
stdout = '640,480'
which is what I expect. However, the second time through the loop I get:
stdout = '640,480640,412'
The first width and height values aren't being flushed. Here is my code:
command = ['identify', '-format', '%[fx:w],%[fx:h]']
for img_id, img_dict in in_metadata.iteritems():
if 'bbox' in img_dict:
continue
command.append(srcdir + 'images/' + str(img_id) + '.jpg')
p = Popen(command, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
if len(stderr) != 0:
continue
w, h = map(int, stdout.split(','))
img_dict['bbox'] = [int(w / 2), int(h / 2), w, h]
stdout.flush()
I've all matter of craziness trying to get this to work (p.wait, stdout.flush, etc.) but the buffer does not want to seem to empty. I know it's something simple, what am I missing?
Thanks.
I'm using python 2.7.12 on Ubuntu 16.04