Photo does not recognize format of Archive::Zip contents

R

Richard Trahan

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.
 
A

Anno Siegel

Richard Trahan said:
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
 
A

Anno Siegel

Richard Trahan said:
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
 
R

Richard Trahan

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.
 
A

A. Sinan Unur

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

Anno Siegel

Richard Trahan said:
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
 
R

Richard Trahan

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.
 
A

Anno Siegel

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.

So long then, and have a pleasant Usenet experience.

Anno
 
D

David H. Adler

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
 
D

Damian James

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
 
T

Tim

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top