Image Streaming

  • Thread starter Steffen Brodowski
  • Start date
S

Steffen Brodowski

Hello everyone,

since one week, I'm programming with python. Its a realy interesting
tool. I wrote a script for generating barcodes in jpg-format.
Everything is ok, but my function "CreateBarcode" should write the jpg
data into an outstream. All barcodes will be generate online, without
saving the jpgs on harddisk.

Can you give me a tip witch objects I need and how to put the jpg into
an outgoing stream?

import Image, ImageDraw
def CreateBarcode(SourceString,Linewidth,WriteText):
blablabla
...
NewImage = Image.new("L",NewSize,Backcolor)
ImgDraw = ImageDraw.Draw(NewImage)
....

#How to put the image into an stream?

best regards

Steffen Brodowksi
Germany
 
P

piter

Uzytkownik "Steffen Brodowski said:
Hello everyone,

since one week, I'm programming with python. Its a realy interesting
tool. I wrote a script for generating barcodes in jpg-format.
Everything is ok, but my function "CreateBarcode" should write the jpg
data into an outstream. All barcodes will be generate online, without
saving the jpgs on harddisk.

Can you give me a tip witch objects I need and how to put the jpg into
an outgoing stream?

import Image, ImageDraw
def CreateBarcode(SourceString,Linewidth,WriteText):
blablabla
...
NewImage = Image.new("L",NewSize,Backcolor)
ImgDraw = ImageDraw.Draw(NewImage)
....

#How to put the image into an stream?

Image.save
save(outfile, options)
save(outfile, format, options)
Saves the image under the given filename. If format is omitted, the format
is determined from the filename extension, if possible. This method returns
None.
Keyword options can be used to provide additional instructions to the
writer. If a writer doesn't recognise an option, it is silently ignored. The
available options are described later in this handbook.
You can use a file object instead of a filename. In this case, you must
always specify the format. The file object must implement the seek, tell,
and write methods, and be opened in binary mode.

hth
Piter
 
I

Ian Bicking

Hello everyone,

since one week, I'm programming with python. Its a realy interesting
tool. I wrote a script for generating barcodes in jpg-format.
Everything is ok, but my function "CreateBarcode" should write the jpg
data into an outstream. All barcodes will be generate online, without
saving the jpgs on harddisk.

Can you give me a tip witch objects I need and how to put the jpg into
an outgoing stream?

import Image, ImageDraw
def CreateBarcode(SourceString,Linewidth,WriteText):
blablabla
...
NewImage = Image.new("L",NewSize,Backcolor)
ImgDraw = ImageDraw.Draw(NewImage)
....

#How to put the image into an stream?

have that function return the image object. Then, assuming you are
doing this in CGI (easily adapted if not), do something like (untested):

import sys
image = CreateBarCode(...)
print 'Content-type: image/jpeg\n'
image.save(sys.stdout, 'JPEG')

Ian
 
F

Fredrik Lundh

Fernando said:
For barcodes, use png, tiff or even gif (the patents expired recently).

note that PIL's GIF generator uses run-length encoding, so the
Unisys LZW patents won't matter here.

</F>
 
S

Steffen Brodowski

Hi Ian,

have that function return the image object. Then, assuming you are
doing this in CGI (easily adapted if not), do something like (untested):

import sys
image = CreateBarCode(...)
print 'Content-type: image/jpeg\n'
image.save(sys.stdout, 'JPEG')


I think its more difficult.

The function CreateBarcode has to return the image directly.
Additional you have to know, that I have to implement it into Zope. So
I use the script as an "external method". Modulname=Barcode,
functionname=CreateBarcode.

I'm using the following line in Zope DTML
<dtml-var "barcode(SourceString='123456789',Linewidth=1,WriteText=0)">
or
<img src="<dtml-var "barcode128(SourceString='123456789',Linewidth=1,WriteText=0)">">

to generate the barcode and for showing it on a html-site.

But is doesn't run.

Do you have any ideas?

Greetings

Steffen Brodowski
 
I

Ian Bicking

I think its more difficult.

The function CreateBarcode has to return the image directly.
Additional you have to know, that I have to implement it into Zope. So
I use the script as an "external method". Modulname=Barcode,
functionname=CreateBarcode.

I'm using the following line in Zope DTML
<dtml-var "barcode(SourceString='123456789',Linewidth=1,WriteText=0)">

So then you don't want to stream it. You might do something like:

from cStringIO import StringIO
def CreateBarcode(...):
# create image object
output = StringIO()
image.save(output, 'GIF')
return output.getvalue()
 
F

Fernando Perez

JanC said:
Only in the US, not in (some countries in) Europe & Japan.

Ah, you're right. But I think they also expire soon, don't they? Well, anyway,
png is better :)

Best,

f.
 
S

Steffen Brodowski

Hi Ian,
from cStringIO import StringIO
def CreateBarcode(...):
# create image object
output = StringIO()
image.save(output, 'GIF')
return output.getvalue()

Yes, it works! Thank you!!

Steffen Brodowski
 

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top