Wie kann ich ein schon geladenes oder mit Image::Magick erzeugtes Bild mit Tk::Photo benutzen?

Inhalt:

Das Problem

In der Dokumentation von Tk::Photo kann man nachlesen, dass man ein neues Bild nicht nur durch Laden aus einer Datei erzeugen kann, sondern auch direkt durch die Bilddaten in einem Skalar:

my $image = $mw->Photo (-data => $imgdata, -format => 'gif');

Das funktioniert erstaunlicherweise aber nicht, wenn $string einfach die binären Bilddaten enthält - es gibt nur die Fehlermeldung Tk::Error: couldn't recognize image data.

Die Lösung

Die Bilddaten müssen für Tk::Photo Base64-kodiert vorliegen.

use MIME::Base64 'encode_base64';

my $image = $mw->Photo (-data => encode_base64($imgdata), -format => 'gif');

Quelle: Posting in der Newsgroup comp.lang.perl.tk

Newsgroups: comp.lang.perl.tk
From: James Gilbert <j...@sanger.ac.uk>
Date: Mon, 04 Jun 2001 16:26:09 +0100
Local: Mon, Jun 4 2001 5:26 pm  
Subject: Re: Photo with -data option

Gary wrote:

> Ok, I'm stumped.  I've been using Photo just fine for -file
> image files.  Now I want speed things up and use internally
> stored data.  I've read in a GIF87 file, compared it byte for byte
> with the original file (to make sure I've read it correctly),
> and passed it to Photo using the -data option.

> I keep getting "couldn't recognize image data at
> /Perl/site/lib/Tk/image.pm line 21"

> I grepped the directories for "recognize" and I can't even find the
> error message in any of the pm files.  I changed image.pm to Writable
> and added print statements around the offending line.  The option
> -data and the image data are arriving there intact.

> Anyone have any idea what I'm doing wrong?  Where is the code
> (handler) which actually interprets the GIFs?

> Thanks in advance.

You can avoid creating the file by using the MIME::Base64
module (get it on CPAN).

  use MIME::Base64 'encode_base64';

  my $photo = $mw->Photo(
      '-format'   => 'gif',
      -data       => encode_base64($gif),
      );

I use this for placing GIFs which I generate with
GD.pm onto a Tk::Canvas.

I discovered that it needed the data base64
encoded only after reading a hint in the
release notes of a version of Tk 4 (under the
heading "Beware of the Leopard")!

I guess that, unlike Perl, Tk isn't 8 bit clean.

        James

Ergänzungen, Kommentare

Kommentare werden am besten in folgender Form vorgenommen, damit sie im Inhaltsverzeichnis angezeigt werden (natürlich ohne das <verbatim>):
---+++ Main.??? - 14 Jul 2003 - Betreff

-- ReneeBaecker - ???
-- HaraldBongartz - 20 Oct 2005

UtilFaqSubForm edit

Titel Wie zeige ich ein Bild aus Image::Magick an ohne eine Datei zu speichern?
Autor ReneeBaecker
Bereich FaqTK
Topic revision: 2005-10-20, HaraldBongartz
 
Bitte die NutzungsBedingungen beachten.
Bei Vorschlägen, Anfragen oder Problemen mit dem PerlCommunityWiki bitten wir um WebBottomBarExample">Rückmeldung.