Pb script cgi-python
Le
fabrice

Bonjour,
un problème avec le code suivant :
#! c:/Python27/python.exe
# -*- coding:utf8 -*-
from PIL import Image,ImageDraw
import cStringIO
# creation d'une image 300x300 (fond bleu)
img = Image.new('RGB',(300,300),"#0000FF")
#img.save('photo.png','PNG') # ca marche (fichier 911 octets, format
PNG valide)
#img.show()
# sortie de l'image au format PNG
f = cStringIO.StringIO()
img.save(f,'PNG')
print "Content-Type: image/png"
print f.getvalue() # probleme
dans mon navigateur : "L'image localhost/cgi-bin/monimage.py ne peut
être affichée car elle contient des erreurs"
la taille du fichier généré fait 915 octets au lieu de 911, et je
constate que les octets 0x0A sont considérés comme des sauts de ligne
car ils sont remplacés par 0xOD 0x0A
Bien sûr, ça marche sous linux (mais tout de même un octet
supplémentaire 0xOA en fin de fichier).
Pourriez-vous m'aider à résoudre ce problème ?
Cordialement,
FS
un problème avec le code suivant :
#! c:/Python27/python.exe
# -*- coding:utf8 -*-
from PIL import Image,ImageDraw
import cStringIO
# creation d'une image 300x300 (fond bleu)
img = Image.new('RGB',(300,300),"#0000FF")
#img.save('photo.png','PNG') # ca marche (fichier 911 octets, format
PNG valide)
#img.show()
# sortie de l'image au format PNG
f = cStringIO.StringIO()
img.save(f,'PNG')
print "Content-Type: image/png"
print f.getvalue() # probleme
dans mon navigateur : "L'image localhost/cgi-bin/monimage.py ne peut
être affichée car elle contient des erreurs"
la taille du fichier généré fait 915 octets au lieu de 911, et je
constate que les octets 0x0A sont considérés comme des sauts de ligne
car ils sont remplacés par 0xOD 0x0A
Bien sûr, ça marche sous linux (mais tout de même un octet
supplémentaire 0xOA en fin de fichier).
Pourriez-vous m'aider à résoudre ce problème ?
Cordialement,
FS
Essaye avec StringIO à la place de cStringIO.
Nicolas
Déjà testé :
ça ne marche pas.
j'ai essayé avec ça, mais exactement le même problème :
img.save("camenbert.png","PNG")
print file(r"camenbert.png","rb").read()
Essaye avec sys.stdout.write au lieu de print
--
même problème !
#! c:/Python27/python.exe
##! /usr/bin/python
# -*- coding: utf-8 -*-
# windows : OK
# linux : OK
from PIL import Image,ImageDraw
import cStringIO
import sys
# Write binary output to stdout (windows OS only)
if sys.platform == "win32":
import os, msvcrt
# sinon n (0x0A) est transformé en rn (0xOD 0x0A)
msvcrt.setmode(1,os.O_BINARY)
print "Content-type: image/pngn"
# création d'une image 300x300 (fond blanc)
img = Image.new('RGB',(300,300),"#FFFFFF")
draw = ImageDraw.Draw(img)
# dessine un arc partiel et le remplit
draw.pieslice((50,50,250,250),0,60,fill="blue")
draw.pieslice((50,50,250,250),60,150,fill="gray")
draw.pieslice((50,50,250,250),150,360,fill="red")
f = cStringIO.StringIO()
img.save(f,"PNG")
sys.stdout.write(f.getvalue())
Cordialement,
FS
Code fonctionnel chez moi :
def make_image(filename, size):
image = Image.open (filename)
image.thumbnail(size, 2)
data = image.tostring("jpeg", "RGB")
return data
Nicolas