473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Image::Magick and Tk::Photo


I am trying to read in a bunch of images and manipulate them using
Image::Magick, then display them using Tk::Photo. I would like to do this
without creating a temp file for each image, since there can be quite a few
of them.

However, I can't make Tk::Photo understand the data returned by
Image::Magick's ImageToBlob() function. Here is the way I had hoped it would
work:

#!perl

use strict;
use warnings;

use Tk;
use Image::Magick;

my $image = Image::Magick->new();
$image->read('c:\Documents and Settings\ericm\My Documents\My
Pictures\earthris.gif');

# Do various Image::Magick manipulations here...
# ...

my $blob = $image->ImageToBlob();

# Set up Tk windows
my $main = MainWindow->new();
my $photo = $main->Photo('img', -format=>'GIF', -data=>$blob );
$main->Label('-image' => 'img', -height=>200, -width=>200)->pack;

MainLoop;

###################################

This code generates the error "couldn't recognize image data at
C:/Perl/site/lib/Tk/Image.pm line 21." in the call to $main->Photo.
If I create a temp file with $image->write() and read it in using the
$main->Photo(-file=>'...') syntax, it works fine.

Any suggestions?

Thanks in advance.

-Eric
Jul 19 '05 #1
2 9511

"Eric McDaniel" <er**@mcdanielhome.com> wrote in message
news:DS*********************@rwcrnsc51.ops.asp.att .net...

I am trying to read in a bunch of images and manipulate them using
Image::Magick, then display them using Tk::Photo. I would like to do this
without creating a temp file for each image, since there can be quite a few
of them.

However, I can't make Tk::Photo understand the data returned by
Image::Magick's ImageToBlob() function. Here is the way I had hoped it would
work:

#!perl

use strict;
use warnings;

use Tk;
use Image::Magick; #-------------------------#
use Mime::Base64;
#-------------------------#

my $image = Image::Magick->new();
$image->read('c:\Documents and Settings\ericm\My Documents\My
Pictures\earthris.gif');

# Do various Image::Magick manipulations here...
# ...

my $blob = $image->ImageToBlob();
# Set up Tk windows
my $main = MainWindow->new();
my $photo = $main->Photo('img', -format=>'GIF', -data=>$blob );
#-------------------------#
my $photo = $main->Photo('img', -format=>'GIF', -data=>encode_base64($blob) );
#-------------------------#
####################### $main->Label('-image' => 'img', -height=>200, -width=>200)->pack;

MainLoop;

###################################

This code generates the error "couldn't recognize image data at
C:/Perl/site/lib/Tk/Image.pm line 21." in the call to $main->Photo.
If I create a temp file with $image->write() and read it in using the
$main->Photo(-file=>'...') syntax, it works fine.

Any suggestions?


You have to convert the data to base64 format for Tk to recognize it.

Jack
Jul 19 '05 #2

"Jack D." <go*********@hotmail.com> writes:
"Eric McDaniel" <er**@mcdanielhome.com> wrote:
I am trying to read in a bunch of images and manipulate them using
Image::Magick, then display them using Tk::Photo. I would like to do this
without creating a temp file for each image, since there can be quite a few
of them.

However, I can't make Tk::Photo understand the data returned by
Image::Magick's ImageToBlob() function.

use Mime::Base64; [...] my $photo = $main->Photo('img', -format=>'GIF', -data=>encode_base64($blob) ); [...] You have to convert the data to base64 format for Tk to recognize it.


Ah man, thanks much for the answer (not to mention the timely question
from Eric). I was just struggling with that issue myself. The
documentation for Tk::Photo leaves much to be desired.

Anyway, now I've got a "baby xv" script working, that re-scales large
images to fit the screen (something noticeably lacking in "ee" and
it's competitors in the free/open software world). It's a little
slow as written, but it least it works:

#!/usr/bin/perl -w
# tk_show_jpeg - do**@kzsu.stanford.edu
# Fri Sep 19 01:10:21 2003

# Simple image viewer, using Perl/Tk and ImageMagick
# This is a baby "xv": an image viewer that re-scales to fit the screen.

use strict;

use Tk;
use Tk::JPEG;
use Image::Magick;
use MIME::Base64;

my ($image_file, $mw, $screen_width, $screen_height);
my ($image_mag, $image_tk, $r, $blob);
my ($tempfile);
my ($img_height, $img_width, $new_height, $new_width, $geom);

$image_file = shift;

$mw = MainWindow->new;
$mw->title($image_file);

# Get screen dimensions
$screen_width = $mw ->screenwidth(); # 1024
$screen_height = $mw ->screenheight(); # 768
$screen_height -= 30; # allow slack for title bars, etc.

# Bind q key to quit
$mw->bind("<Key-q>", [sub{$mw->destroy}, Ev('K')]);

# Read in file into imagemagick

$image_mag = Image::Magick->new;
$r = $image_mag->Read("$image_file");
warn "$r" if "$r";

# Scale down $image_mag if necessary

($img_height, $img_width) = $image_mag->Get('height', 'width');

$new_height = $img_height; # "New" sizes default to the old
$new_width = $img_width;

if ($img_height > $screen_height) {
$new_height = $screen_height;
}
if ($img_width > $screen_width) {
$new_width = $screen_width;
}
$geom = $new_width . 'x' . $new_height;

$r = $image_mag->Scale(geometry=>$geom);
warn "$r" if "$r";

# Transfer image from ImageMagick to Perl/Tk

$blob = ( $image_mag->ImageToBlob(magick=>'jpg') )[0];
$image_tk = $mw->Photo('img', -format=>'jpeg', -data=>encode_base64($blob) );

# (I *gather* that that first argument 'img' is an "imageName"
# mentioned in some places in the docs, but not explained well.)

$mw->Label(-image => $image_tk)->pack(-side=>'left');
$mw->update;
MainLoop;
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Marion | last post by:
Hello. I am using Tkinter and Pmw. I would like to build 2 canvases/frames that are scrollable together horizontally, and independently vertically (so, the vertical scrollbars should not be...
3
by: Terry Carroll | last post by:
I've got a small batch image-processing program (it adds the time a digital photo was taken to the lower right of the image), and as a feature, I wanted to show a thumbnail of each image it was...
3
by: Weapons of Mass Destruction | last post by:
Can anyone tell me why Image::Magick isn't loading? I keep getting the following error: Can't load 'd:/home/d83921/cgi-bin/photo/magick/Magick.dll' for module Image::Magick: load_file:The...
0
by: Shi Mu | last post by:
why the following code report the error: Traceback (most recent call last): File "C:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript exec codeObject...
4
by: Callum Prentice | last post by:
i need a "script" that i can use locally as well as online that will: * create a large (maybe something like 2k x 2k) master image in memory * open a text file and read all the lines from it...
2
by: Aleksandar Cikota | last post by:
Hi all, I have a problem with openning of an image. Here is the Code: from Tkinter import * from PIL import Image, ImageTk from win32com.client import gencache import tkMessageBox
1
by: nish88 | last post by:
hi to all... i'm working on an animation where i have to make the picture background.gif transparent. l'm not able to do it.can you please help me or tell me what codes i should add to make the...
0
by: John Stevens | last post by:
I am using PIL to make images that I need to display in a sequence. The image needs to change when a an event happens from a serial port. I call the following function to display the image, but...
2
by: defn noob | last post by:
from Tkinter import * import os master = Tk() w = Canvas(master, width=800, height=600) print os.path.exists('C:/me/saftarn/desktop/images/blob4.jpg') im = PhotoImage(file =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.