473,765 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:\Docum ents and Settings\ericm\ My Documents\My
Pictures\earthr is.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 9556

"Eric McDaniel" <er**@mcdanielh ome.com> wrote in message
news:DS******** *************@r wcrnsc51.ops.as p.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:\Docum ents and Settings\ericm\ My Documents\My
Pictures\earthr is.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_ba se64($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*********@ho tmail.com> writes:
"Eric McDaniel" <er**@mcdanielh ome.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_ba se64($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.stanf ord.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_f ile);

# 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_f ile");
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(ma gick=>'jpg') )[0];
$image_tk = $mw->Photo('img', -format=>'jpeg', -data=>encode_ba se64($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
1456
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 moved by the horizontal one). So I built a Pmw.ScrolledFrame() containing the 2 canvases, horizontally scrollable. Now I am trying to build 2 independent vertical scrollbars,
3
8777
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 being processed. I can't get the image to update, though. I trimmed it down to a basic app indicating the problem and the code is at the end of this message. It should display the three listed sample images, one after another. The thing is,...
3
4040
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 specified module could not be found at C:/Perl/lib/DynaLoader.pm line 206. I'm trying to get the module to work on a remote Win32 and IIS server that I do NOT have local access to.
0
1478
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 in __main__.__dict__ File "C:\Python23\Examples\AppB\text.py", line 24, in ? text.image_create(END, image=photo) File "C:\Python23\lib\lib-tk\Tkinter.py", line 2882, in image_create return self.tk.call( TclError: image "pyimage4" doesn't exist
4
9584
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 (maybe 1000 lines max) * each line is composed of an x, y, name and a png image filename * for each line, open the png image and position it in the master image at the location given by x & y * save off the master image to a png at the end
2
4685
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
11369
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 picture transparent. thank in advance import Tkinter from Tkconstants import * def window(tk):
0
916
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 then the application is waiting for an event. I need to return to the main code which is sending commands to a serial port and waiting for data to return then displaying the next chart. def display(R,G,B): img =...
2
10133
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 = 'C:/users/saftarn/desktop/images/blob4.jpg') #im = file = 'C:/users/me/desktop/images/blob4.jpg'
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.