Python Buffer PNG Image

I am working with the Indigo library for a chemistry web project. In short, they wrote a nice Python interface for him, which I use through CGI. There are several output formats, including SVG and PNG. I'm not a Python professional, so I'm a little fixated on the idea of ​​an author about a buffer. The following works:

#!python
from indigo import *
from indigo_renderer import *
from struct import *
print "Content-type: image/svg+xml"
print

indigo = Indigo()
renderer = IndigoRenderer(indigo);

mol1 = indigo.loadMolecule("ONc1cccc1");

indigo.setOption("render-output-format", "svg");
indigo.setOption("render-highlight-color-enabled", "true");

image = renderer.renderToBuffer(mol1);
output = image.tostring()
print output

Thus, the above code splashes out properly formatted SVG XML without any unwanted leading or tail characters. Firefox recognized the type of content and displayed it perfectly.

However, it’s hard for me to understand what I should do for PNG:

I change the code to:

from indigo import *
from indigo_renderer import *
from struct import *

print "Content-type: image/png"
print

indigo = Indigo()
renderer = IndigoRenderer(indigo);

mol1 = indigo.loadMolecule("ONc1cccc1");

indigo.setOption("render-output-format", "png");
indigo.setOption("render-highlight-color-enabled", "true");

image = renderer.renderToBuffer(mol1);
output = image
print output

and I get this (which I did not expect from PNG):

array('c', '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00[ truncated by me ]')

toString(), blob ( ), - . , , ( PHP) - , , , (, , , , ). , , .

+3
2

( , , ). - Windows , , . , , Linux VPS, . , tostring() , , . , , , . , stdout, . . : . :

Win32 Binary Write :

#!python
from indigo import *
from indigo_renderer import *
from array import *
import sys, cgitb, cgi

cgitb.enable()
httpArgs = cgi.FieldStorage()
print "Content-type: image/png"
print

indigo = Indigo()
renderer = IndigoRenderer(indigo)

mol1 = indigo.loadMolecule("ONc1cccc1")

indigo.setOption("render-output-format", "png")
indigo.setOption("render-highlight-color-enabled", "true")

outputStream = renderer.renderToBuffer(mol1)
if sys.platform == "win32":
   import os, msvcrt
   msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
else:
sys.exit()
sys.stdout.write(outputStream.tostring())
0

, PNG . wb.

PHP, , python:

:

PHP

...
output = image
f=open ("someFileName.png", wb)
f.write(output[1])
f.close()

"someFileName.png" PHP.

: python script PHP. .

...
output = image
import sys
sys.exit(output[1])
+1

All Articles