Using os.popen3() to get binary data

  • Thread starter Christoph Krammer
  • Start date
C

Christoph Krammer

Hello everybody,

I need to get the different frames from a GIF image in my python
script and want to use the giftopnm program from netpbm to get the
frames and directly convert them to pnm files. I tried to use the
following code:

for image in images:
if (image[0:3] == 'GIF'):
(si, so, se) = os.popen3('giftopnm -image=all', 'b')
si.write(image)
frame = so.readlines()

But with this code the script just hangs. When I interrupt the script,
I get the following error message:
Traceback (most recent call last):
File "/home/tiger/stock-spam/scripts/all_in_one.py", line 46, in ?
frames = so.readlines()
KeyboardInterrupt
close failed: [Errno 32] Broken pipe

Can somebody tell me, which command I have to use that the pipe will
be closed when the giftopnm returns? This program just prints the
converted images to stdout and terminates.

Thanks in advance,
Christoph
 
?

=?ISO-8859-1?Q?Thomas_Kr=FCger?=

Christoph said:
for image in images:
if (image[0:3] == 'GIF'):
(si, so, se) = os.popen3('giftopnm -image=all', 'b')
si.write(image)
frame = so.readlines()

But with this code the script just hangs. When I interrupt the script,
I get the following error message:
Traceback (most recent call last):
File "/home/tiger/stock-spam/scripts/all_in_one.py", line 46, in ?
frames = so.readlines()
KeyboardInterrupt
close failed: [Errno 32] Broken pipe

Just a try: use read() instead of readlines()!

Thomas
 
C

Christoph Krammer

Just got the solution...

After sending the image data with "si.write(image)", I have to close
the pipe to tell the program to convert the image with "si.close()".
Now everything works fine.

Christoph
 
D

Dennis Lee Bieber

Hello everybody,

I need to get the different frames from a GIF image in my python
script and want to use the giftopnm program from netpbm to get the
frames and directly convert them to pnm files. I tried to use the
following code:

for image in images:
if (image[0:3] == 'GIF'):

What type of data /is/ "image" that the FIRST three characters
identify the type? If it's the name of the file (I don't know what
giftopnm requires for input) I'd have expected the last three to
identify..
(si, so, se) = os.popen3('giftopnm -image=all', 'b')
si.write(image)

Again, if this is a file name, maybe you need to write a new-line at
the end of it. If it's a binary stream (the actual image file contents)
you might have to somehow signal an EOF (tricky, as any /character/ EOF
would be taken as more data)

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
M

MRAB

Hello everybody,
I need to get the different frames from a GIF image in my python
script and want to use the giftopnm program from netpbm to get the
frames and directly convert them to pnm files. I tried to use the
following code:
for image in images:
if (image[0:3] == 'GIF'):

What type of data /is/ "image" that the FIRST three characters
identify the type? If it's the name of the file (I don't know what
giftopnm requires for input) I'd have expected the last three to
identify..
[snip]
FYI, the first 3 bytes of a GIF image are the ASCII characters "GIF".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top