Image Streaming

Discussion in 'Python' started by Steffen Brodowski, Jul 8, 2003.

  1. 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
    Steffen Brodowski, Jul 8, 2003
    #1
    1. Advertising

  2. Steffen Brodowski

    piter Guest

    Uzytkownik "Steffen Brodowski" <> napisal w
    wiadomosci news:...
    > 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
    >
    > best regards
    >
    > Steffen Brodowksi
    > Germany
    piter, Jul 8, 2003
    #2
    1. Advertising

  3. Steffen Brodowski

    Ian Bicking Guest

    On Tue, 2003-07-08 at 02:26, Steffen Brodowski wrote:
    > 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
    Ian Bicking, Jul 8, 2003
    #3
  4. Steffen Brodowski

    JanC Guest

    Fernando Perez <> schreef:

    > or even gif (the patents expired recently).


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

    --
    JanC

    "Be strict when sending and tolerant when receiving."
    RFC 1958 - Architectural Principles of the Internet - section 3.9
    JanC, Jul 9, 2003
    #4
  5. Fernando Perez wrote:

    > 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>
    Fredrik Lundh, Jul 9, 2003
    #5
  6. Hi Ian,


    > >
    > > 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')



    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
    Steffen Brodowski, Jul 9, 2003
    #6
  7. Steffen Brodowski

    Ian Bicking Guest

    On Wed, 2003-07-09 at 09:22, Steffen Brodowski wrote:
    > > 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)">


    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()
    Ian Bicking, Jul 9, 2003
    #7
  8. JanC wrote:

    > Fernando Perez <> schreef:
    >
    >> or even gif (the patents expired recently).

    >
    > 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.
    Fernando Perez, Jul 9, 2003
    #8
  9. Steffen Brodowski

    JanC Guest

    Fernando Perez <> schreef:

    >>> or even gif (the patents expired recently).

    >>
    >> 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?


    Somewhere next year IIRC (at least in most countries).

    --
    JanC

    "Be strict when sending and tolerant when receiving."
    RFC 1958 - Architectural Principles of the Internet - section 3.9
    JanC, Jul 10, 2003
    #9
  10. 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
    Steffen Brodowski, Jul 11, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen

    Displaying Image after Streaming

    Stephen, Nov 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    446
    Stephen
    Nov 29, 2004
  2. Charles A. Lackman

    Streaming with System.IO and System.Net

    Charles A. Lackman, Dec 8, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    546
    Richard Grimes [MVP]
    Jan 15, 2005
  3. Jerry J
    Replies:
    0
    Views:
    542
    Jerry J
    Jul 18, 2003
  4. Tscheihpih

    Streaming Dynamic Image

    Tscheihpih, Feb 18, 2004, in forum: Java
    Replies:
    1
    Views:
    330
    Tscheihpih
    Feb 18, 2004
  5. Laszlo Zsolt Nagy
    Replies:
    1
    Views:
    1,291
    Kartic
    Jan 26, 2005
Loading...

Share This Page