tags:

views:

130

answers:

1

Hi all,

How can I send a .png file using python cgi to a flex application?

Thanks in advance...

+1  A: 

The Python/CGI side of your question can be as simple as something like this, if you just need to send an existing image:

import sys

# Send the Content-Type header to let the client know what you're sending
sys.stdout.write('Content-Type: image/png\r\n\r\n')

# Send the actual image data
with open('path/to/image.png', 'rb') as f:
    sys.stdout.write(f.read())

If, on the other hand, you're dynamically creating images with, say, PIL, you can do something along these lines:

import sys
import Image, ImageDraw # or whatever

sys.stdout.write('Content-Type: image/png\r\n\r\n')

# Dynamically create an image
image = Image.new('RGB', (100, 100))
# ... etc ...

# Send the image to the client
image.save(sys.stdout, 'PNG')
Will McCutchen