Photo does not recognize format of Archive::Zip contents

Discussion in 'Perl Misc' started by Richard Trahan, Jun 8, 2005.

  1. Consider the following sub. On entry, $tl is a Toplevel, $rmn is an
    index into an array of Archive::Zip object member names; comments
    explain the others.

    sub display_image
    {
    my ($tl,$rmn) = @_; # Toplevel, requested member number
    my $mnref = $tl->{'member_names'}; # ref to member names array
    my $zip = $tl->{'zip'}; # Archive::Zip object

    my $membername = $mnref->[$rmn];
    my $member = $zip->memberNamed($membername);
    $tl->{'current_member_number'} = $rmn; # reset current member
    my $fh;
    my $fn = "C:/windows/temp/temp.jpg";
    open $fh,">",$fn;
    $member->extractToFileHandle($fh); # this works ok
    close $fh;
    # my $contents = $zip->contents($member); # (binary) jpeg data
    # open $fh,">","C:/temp/temp2.jpg";
    # binmode $fh;
    # print $fh $contents; # this works ok
    # close $fh;
    my $image = $tl->Photo(
    '-format' => 'jpeg',
    # -data => $contents, # this doesn't work
    -file => $fn, # this works ok
    );
    my $button = $tl->{'button'}; # button where image is displayed
    $button->configure(-image => $image); # set image
    $tl->configure(-title => $tl->{'file_name'} .
    ":" . $tl->{'sequence'} . " - $membername");
    }

    Commented sections show that I can successfully extract the "contents"
    of a Zip::Archive member (all of which are jpeg files), and write it to
    a file (it displays perfectly in a browser, PhotoEditor, etc.). I can
    also "extract" that member to another file and specify that file name to
    the Photo object, whereupon it displays just fine. But when I take the
    "contents", which is supposed to be pure jpeg binary data, and feed it
    to the Photo object, the system complains that it doesn't recognize the
    file format.

    Any help, please.
    Richard Trahan, Jun 8, 2005
    #1
    1. Advertising

  2. Richard Trahan

    Anno Siegel Guest

    Richard Trahan <> wrote in comp.lang.perl.misc:
    > Consider the following sub. On entry, $tl is a Toplevel, $rmn is an


    A toplevel what? Please name the modules you are using. Leaving it
    up to the reader to guess from one or two function names is impolite.

    > index into an array of Archive::Zip object member names; comments
    > explain the others.


    Is the fact that the images come from a zip archive essential? I
    doubt it.

    > sub display_image
    > {
    > my ($tl,$rmn) = @_; # Toplevel, requested member number
    > my $mnref = $tl->{'member_names'}; # ref to member names array
    > my $zip = $tl->{'zip'}; # Archive::Zip object
    >
    > my $membername = $mnref->[$rmn];
    > my $member = $zip->memberNamed($membername);
    > $tl->{'current_member_number'} = $rmn; # reset current member
    > my $fh;
    > my $fn = "C:/windows/temp/temp.jpg";
    > open $fh,">",$fn;
    > $member->extractToFileHandle($fh); # this works ok
    > close $fh;
    > # my $contents = $zip->contents($member); # (binary) jpeg data


    That line shouldn't be commented out, you're using $contents later.
    Please copy and paste code instead of re-typing it. Trivial errors
    like this are distracting.

    > # open $fh,">","C:/temp/temp2.jpg";
    > # binmode $fh;
    > # print $fh $contents; # this works ok
    > # close $fh;
    > my $image = $tl->Photo(
    > '-format' => 'jpeg',
    > # -data => $contents, # this doesn't work
    > -file => $fn, # this works ok
    > );
    > my $button = $tl->{'button'}; # button where image is displayed
    > $button->configure(-image => $image); # set image
    > $tl->configure(-title => $tl->{'file_name'} .
    > ":" . $tl->{'sequence'} . " - $membername");
    > }
    >
    > Commented sections show that I can successfully extract the "contents"
    > of a Zip::Archive member (all of which are jpeg files), and write it to
    > a file (it displays perfectly in a browser, PhotoEditor, etc.). I can
    > also "extract" that member to another file and specify that file name to
    > the Photo object, whereupon it displays just fine. But when I take the
    > "contents", which is supposed to be pure jpeg binary data, and feed it
    > to the Photo object, the system complains that it doesn't recognize the
    > file format.


    Read "perldoc Tk::photo". Under the heading "IMAGE FORMATS" you'll
    find:

    The photo image code is structured to allow handlers for
    additional image file formats to be added easily. The
    photo image code maintains a list of these handlers.
    Handlers are added to the list by registering them with a
    call to Tk_CreatePhotoImageFormat. The standard Tk
    distribution comes with handlers for PPM/PGM and GIF
    formats, which are automatically registered on
    initialization.

    Looks like you may have to find a way to add a jpeg handler. I don't
    know why it works with file input, nor why a jpeg handler *is* mentioned
    elsewhere in the documentation (except that documentation isn't perfect).
    At least this looks like something to explore.

    Anno
    Anno Siegel, Jun 8, 2005
    #2
    1. Advertising

  3. Richard Trahan

    Anno Siegel Guest

    Richard Trahan <> wrote in comp.lang.perl.misc:
    > Consider the following sub. On entry, $tl is a Toplevel, $rmn is an


    A toplevel what? Please name the modules you are using. Leaving it
    up to the reader to guess from one or two function names is impolite.

    > index into an array of Archive::Zip object member names; comments
    > explain the others.


    Is the fact that the images come from a zip archive essential? I
    doubt it.

    > sub display_image
    > {
    > my ($tl,$rmn) = @_; # Toplevel, requested member number
    > my $mnref = $tl->{'member_names'}; # ref to member names array
    > my $zip = $tl->{'zip'}; # Archive::Zip object
    >
    > my $membername = $mnref->[$rmn];
    > my $member = $zip->memberNamed($membername);
    > $tl->{'current_member_number'} = $rmn; # reset current member
    > my $fh;
    > my $fn = "C:/windows/temp/temp.jpg";
    > open $fh,">",$fn;
    > $member->extractToFileHandle($fh); # this works ok
    > close $fh;
    > # my $contents = $zip->contents($member); # (binary) jpeg data
    > # open $fh,">","C:/temp/temp2.jpg";
    > # binmode $fh;
    > # print $fh $contents; # this works ok
    > # close $fh;
    > my $image = $tl->Photo(
    > '-format' => 'jpeg',
    > # -data => $contents, # this doesn't work
    > -file => $fn, # this works ok
    > );
    > my $button = $tl->{'button'}; # button where image is displayed
    > $button->configure(-image => $image); # set image
    > $tl->configure(-title => $tl->{'file_name'} .
    > ":" . $tl->{'sequence'} . " - $membername");
    > }
    >
    > Commented sections show that I can successfully extract the "contents"
    > of a Zip::Archive member (all of which are jpeg files), and write it to
    > a file (it displays perfectly in a browser, PhotoEditor, etc.). I can
    > also "extract" that member to another file and specify that file name to
    > the Photo object, whereupon it displays just fine. But when I take the
    > "contents", which is supposed to be pure jpeg binary data, and feed it
    > to the Photo object, the system complains that it doesn't recognize the
    > file format.


    Read "perldoc Tk::photo". Under the heading "IMAGE FORMATS" you'll
    find:

    The photo image code is structured to allow handlers for
    additional image file formats to be added easily. The
    photo image code maintains a list of these handlers.
    Handlers are added to the list by registering them with a
    call to Tk_CreatePhotoImageFormat. The standard Tk
    distribution comes with handlers for PPM/PGM and GIF
    formats, which are automatically registered on
    initialization.

    Looks like you may have to find a way to add a jpeg handler. I don't
    know why it works with file input, nor why a jpeg handler *is* mentioned
    elsewhere in the documentation (except that documentation isn't perfect).
    At least this looks like something to explore.

    Anno
    Anno Siegel, Jun 8, 2005
    #3
  4. Thank you for your reply.

    There are no errors in my sample; $contents is not used later unless all
    lines containing $contents are uncommented.

    Yes, it is essential that the files come from a zip archive.

    Yes, I have read Tk::photo; JPEG is listed in the Description as a
    supported format. The fact that I can read a jpeg file with the "-file"
    option illustrates that. It is when "-file" is replaced by "-data =>
    contents_from_zip_archive" that it breaks.

    A Toplevel would be immediately familiar to anyone conversant with Tk.
    Since you opted to point me to Tk::photo, I assume you are a Tk person,
    and you should have known what Toplevel is.

    Yes, I do know about comp.lang.perl.tk. I chose to post here, because
    the members are more knowledgeable, for the most part.

    It is impolite to criticize when you don't know what you're talking about.
    Richard Trahan, Jun 8, 2005
    #4
  5. Richard Trahan <> wrote in
    news:2oCpe.3444$:

    > Thank you for your reply.


    [ Thank whom for what reply? Please quote some context when you reply. ]

    > It is impolite to criticize when you don't know what you're talking
    > about.


    A verdict more applicable to you than to Anno, for sure.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jun 8, 2005
    #5
  6. Richard Trahan

    Anno Siegel Guest

    Richard Trahan <> wrote in comp.lang.perl.misc:
    >
    > Thank you for your reply.


    Whose reply? Please quote some context when you reply.

    > There are no errors in my sample; $contents is not used later unless all
    > lines containing $contents are uncommented.


    Yes. I noticed too late that you chose to give example code that doesn't
    demonstrate the error. I corrected that part of my posting, but you seem
    to have replied to the uncorrected version. See how quoting *the post that
    you saw* can make a difference?

    > Yes, it is essential that the files come from a zip archive.


    Essential for the error? Are you saying, if you read the string from
    a .jpeg file instead of a zip archive you don't have the problem?
    That would be an interesting hint, but neither your code nor your
    comments indicate that.

    > Yes, I have read Tk::photo; JPEG is listed in the Description as a
    > supported format.


    It is listed in one place, but not in the section "IMAGE FORMATS"
    (in the version I checked). Since you reported a problem with
    one of the missing image formats I thought I'd point out the fact.

    > The fact that I can read a jpeg file with the "-file"
    > option illustrates that. It is when "-file" is replaced by "-data =>
    > contents_from_zip_archive" that it breaks.


    Yes, I understand that. Does it also break when you read the data from
    a file and not a zip archive? I'd expect it does, but your insistence
    that Archive::Zip is somehow involved makes me ask.

    > A Toplevel would be immediately familiar to anyone conversant with Tk.


    Oh. No other module can use the identifier "Toplevel"?

    > Since you opted to point me to Tk::photo, I assume you are a Tk person,
    > and you should have known what Toplevel is.


    I'm not a "Tk person". I have used the module occasionally and have
    it around. I decided to take a look at the relevant documentation,
    found a discrepancy and pointed it out.

    > Yes, I do know about comp.lang.perl.tk. I chose to post here, because
    > the members are more knowledgeable, for the most part.


    Don't mix posters up. I didn't point you to c.l.p.tk.

    Since you chose to post here it would have been polite to word your
    posting for the general audience you can expect here and not for people
    who live in a Tk world.

    Anno
    Anno Siegel, Jun 8, 2005
    #6
  7. Thank you for your reply.
    >
    > [ Thank whom for what reply? Please quote some context when you reply. ]

    No, I won't. The thanks apply to the parent posting, which you can see
    if you group your postings by thread.
    Richard Trahan, Jun 8, 2005
    #7
  8. Richard Trahan

    Anno Siegel Guest

    Richard Trahan <> wrote in comp.lang.perl.misc:
    > Thank you for your reply.
    > >
    > > [ Thank whom for what reply? Please quote some context when you reply. ]

    > No, I won't. The thanks apply to the parent posting, which you can see
    > if you group your postings by thread.


    So long then, and have a pleasant Usenet experience.

    Anno
    Anno Siegel, Jun 8, 2005
    #8
  9. On 2005-06-08, Richard Trahan <> wrote:
    > Thank you for your reply.
    >>
    >> [ Thank whom for what reply? Please quote some context when you reply. ]

    > No, I won't. The thanks apply to the parent posting, which you can see
    > if you group your postings by thread.


    so you feel that everyone should arrange their newsreading the way you
    want to and further assume that all posts come in in the same order for
    everyone.

    I've seen one post indicating that you won't be getting any quality help
    from here out, and I think that there will likely be more.

    Have the appropriate amount of fun.

    dha

    --
    David H. Adler - <> - http://www.panix.com/~dha/
    _Day of Wrath_ is probably Dreyer's most popular film, which already
    indicates something of the problems it poses.
    - David Bordwell, The Films of Carl-Theodor Dreyer
    David H. Adler, Jun 8, 2005
    #9
  10. Richard Trahan

    Damian James Guest

    On Wed, 08 Jun 2005 12:50:50 -0400, Richard Trahan said:
    > Thank you for your reply.
    >>
    >> [ Thank whom for what reply? Please quote some context when you reply. ]

    > No, I won't. The thanks apply to the parent posting, which you can see
    > if you group your postings by thread.


    Who said "[ Thank whom for what reply? Please quote some context when you
    reply. ]"? I should wait till my news server has a copy of all previous
    posts in the thread before reading this?

    *sigh*

    You're perhaps not aware of the distributed nature of usenet. You might
    ponder what the implications are of there not existing a single central
    news server from which changes are promulgated and posts served. Instead,
    the lag interval between posting and various people seeing the post
    is quite undeterminable. Some people will see this reply before the post
    of yours that I have replied to. Some news servers might not even carry
    the post that someone is referring to (I know my leafnode configuration
    drops anything with more than 3 crossposts, because in the groups I read,
    I have never seen a non-spam post that matches that).

    You may perhaps be using some sort of web-based interface (Not google, I
    would not have seen your post at all, nor any replies to it, if that were
    the case). Even in that case the vagaries of usenet apply, you may indeed
    not see all posts in a thread. And even on google, you won't see all posts
    by all people at all times, even if the post made it to the server, since
    there exists the X-No-Archive header, and many people use it routinely.

    All in all, denizens of groups like this one have established a convention
    that says: quote what you are replying to, there's no other reliable way
    for others to know what that is. You are either bringing a convention from
    another environment, of you are making up your own. Either way, no-one is
    saying that YOU MUST follow the conventions of this group while you are
    here, but all the same not doing so shows disrespect, even contempt, of
    the folks who post here regularly. Do you really want to be doing that?

    Hope this helps your understanding of the situation here a little.

    --damian

    --
    @:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
    $:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
    $,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/} __END__
    Just another Perl Hacker,
    Damian James, Jun 9, 2005
    #10
  11. Richard Trahan

    Tim Guest

    Richard,

    The answer to your question is that the "-data" option in the Photo
    method requires that the picture be base64 encoded. Add the following
    to your code:

    use MIME::Base64 qw(encode_base64);

    and change
    # -data => $contents, # this doesn't work
    to
    # -data => encode_base64($contents), # this doesn't work

    And I think you will find that this now works. It would have been nice
    if the other posters had bothered to answer the question insted of
    getting into arguments about net-etiquette.


    Richard Trahan <> wrote in
    news:cEype.70207$:

    > Consider the following sub. On entry, $tl is a Toplevel, $rmn is an
    > index into an array of Archive::Zip object member names; comments
    > explain the others.
    >
    > sub display_image
    > {
    > my ($tl,$rmn) = @_; # Toplevel, requested member number
    > my $mnref = $tl->{'member_names'}; # ref to member names array
    > my $zip = $tl->{'zip'}; # Archive::Zip object
    >
    > my $membername = $mnref->[$rmn];
    > my $member = $zip->memberNamed($membername);
    > $tl->{'current_member_number'} = $rmn; # reset current member
    > my $fh;
    > my $fn = "C:/windows/temp/temp.jpg";
    > open $fh,">",$fn;
    > $member->extractToFileHandle($fh); # this works ok
    > close $fh;
    > # my $contents = $zip->contents($member); # (binary) jpeg data
    > # open $fh,">","C:/temp/temp2.jpg";
    > # binmode $fh;
    > # print $fh $contents; # this works ok
    > # close $fh;
    > my $image = $tl->Photo(
    > '-format' => 'jpeg',
    > # -data => $contents, # this doesn't work
    > -file => $fn, # this works ok
    > );
    > my $button = $tl->{'button'}; # button where image is displayed
    > $button->configure(-image => $image); # set image
    > $tl->configure(-title => $tl->{'file_name'} .
    > ":" . $tl->{'sequence'} . " - $membername");
    > }
    >
    > Commented sections show that I can successfully extract the "contents"
    > of a Zip::Archive member (all of which are jpeg files), and write it
    > to a file (it displays perfectly in a browser, PhotoEditor, etc.). I
    > can also "extract" that member to another file and specify that file
    > name to the Photo object, whereupon it displays just fine. But when I
    > take the "contents", which is supposed to be pure jpeg binary data,
    > and feed it to the Photo object, the system complains that it doesn't
    > recognize the file format.
    >
    > Any help, please.
    >
    Tim, Jul 4, 2005
    #11
    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. Graham Wood
    Replies:
    3
    Views:
    518
    A. Sinan Unur
    Jan 11, 2004
  2. Twig
    Replies:
    1
    Views:
    121
    A. Sinan Unur
    Jan 27, 2006
  3. MoshiachNow
    Replies:
    2
    Views:
    257
    Ilya Zakharevich
    Oct 4, 2006
  4. MoshiachNow

    Archive::Zip - zip file has "invalid" format

    MoshiachNow, Oct 5, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    153
  5. Bo Yang
    Replies:
    9
    Views:
    289
    -berlin.de
    Nov 20, 2006
Loading...

Share This Page