1

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

wmaddox
  • 141
  • 2
  • 10

2 Answers2

1

Each iteration, your command is being appended to. I suspect that you don't really want that. Consider this simplified version of your code:

labels = 'LABELS'
srcdir = 'SRCDIR/'
in_metadata = {123: {labels:[1,2]}, 234: {labels:[2,3]}}
command = ['identify', '-format', '%[fx:w],%[fx:h]']

for img_id, img_dict in in_metadata.iteritems():
    command.append(srcdir + 'images/' + str(img_id) + '.jpg')
    print command

Output:

['identify', '-format', '%[fx:w],%[fx:h]', 'SRCDIR/images/234.jpg']
['identify', '-format', '%[fx:w],%[fx:h]', 'SRCDIR/images/234.jpg', 'SRCDIR/images/123.jpg']

You probably want something more like this:

base_command = ['identify', '-format', '%[fx:w],%[fx:h]']

for img_id, img_dict in in_metadata.iteritems():
    command = base_command + [srcdir + 'images/' + str(img_id) + '.jpg']
    ...
FMc
  • 41,963
  • 13
  • 79
  • 132
  • I'm such a bonehead. I've used subprocess quite a few times in the past. Never have i had so much difficulty. Thanks for pointing out the obvious. – wmaddox Dec 13 '16 at 18:49
0

If you are running this from bash , can you try setting PYTHONUNBUFFERED before running your script?

export PYTHONUNBUFFERED=true
Illusionist
  • 5,204
  • 11
  • 46
  • 76