Movatterモバイル変換
[0]ホーム
Newbie has question that's not exactly Python...
Dave LeBlancwhisper at oz.net
Tue Apr 3 21:01:27 EDT 2001
I did an experiment where I tried to load a gif file into IE directlyfrom disk (File:Open). Instead of displaying it inline, it brought upthe helper app associated with gif files.I believe that you'll have to add just a bit of html around your imagebefore a browser is going to accept it. I don't believe that"content-type image/gif" is enough! According to the html standard, atleast <html> is needed to start things off. This seems to be a fairly standard minimal template for an html file:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><title>Untitled</title></head><body><-- Image here --></body></html>(Change the "HTML 4.0 Transitional" to 3.2 if that suits you: "HTML3.2".)I have seen html sources (View:source) that got away with just:<html></html>Dave LeBlancOn Tue, 03 Apr 2001 21:08:31 GMT, "Gary Walker" <borealis3 at home.com>wrote:>Chris,>>Your answer *looks* like it might work, but, unfortunately, it doesn't. Let>me quickly say that I realize that you were typing it off the top of your>head, and so one really shouldn't expect it to run. But to me, it looks>rather complete... but I think something's missing...>>Here's some actual code of mine that doesn't work:>>************************************>#!/usr/bin/python>>import StringIO, cgi, os, sys, string, Image>>im = Image.open('backgroundimage.gif')># here I'll be drawing all over my image...>># Now I want to send it to a browser...>sfp = StringIO.StringIO()>im.save(sfp,'gif')>>print 'Content-Type: image/gif\r\n\r\n'>sys.stdout.write(sfp.getvalue())>************************************>>Do you (or anyone else please??) see what's wrong here? It seems like it>oughta work.>>As you can see, I've incorporated most of Chris's solution (see below), but>I'm not a savvy enough Python programmer to know what's missing. I'd love to>know the "right" way of doing this, so I can get on with the next piece of>my program...>>Can anyone suggest a working solution?>>Thanks!!>>Gary Walker>>Chris Gonnerman wrote in message ...>>You could use the Python Imaging Library. I'm no expert on it; I use>>gdmodule,>>but PIL is more "standard." You would do something like this in a cgi:>>>> import sys>> from PIL import Image>> from StringIO import StringIO>>>> img = Image.open("background.gif") # or Image.new(...) for a blank>>image>> # ... do some drawing on the image ...>> sfp = StringIO()>> img.save(sfp, "gif") # possibly with keyword options>> sys.stdout.write(sfp.getvalue())>>>>The StringIO is required as the save() method of the Image object requires>>seek() and tell() methods, so we can't just say>>>> img.save(sys.stdout, "gif") # BIG NO NO>>>>because sys.stdout may be attached to a pipe or socket.>>>>As I said, I'm no expert on PIL, so your mileage may vary. There may be an>>easier way to do this... anyone else care to comment?>>>>>
More information about the Python-listmailing list
[8]ページ先頭