Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Shrink Image to Fit Widget?

693 views
Skip to first unread message

Bryan Green

unread,
Jun 25, 2008, 12:47:59 PM6/25/08
to
Hello, all-- I'm wondering if someone can give me a quick tutorial on
loading images into widgets...

I've been struggling with this for days and I'm on the verge of pulling
my hair out. I've tried all the code snippets I've found, none of which
has worked. I'm convinced that the "-shrink" option of the "image"
command does nothing. Here's an example of code I've tried, from the
"TKPhotoLab" tutorial in the tcl/tk wiki:

proc loadImg {{fn ""}} {
if {$fn==""} {set fn [tk_getOpenFile]}
if {$fn != ""} {
cd [file dirname [file join [pwd] $fn]]
set ::im1 [image create photo -file $fn]
.1 config -image $::im1
set ::im2 [image create photo]
.2 config -image $::im2
$::im2 copy $::im1 -shrink
set ::info "Loaded image 1 from $fn"
}
}

When I use this code, my images aren't shrunk to fit the labels on which
they're placed-- they just get clipped, and only the top-left corner
shows up in the label.

Thanks!
Bryan

Bryan Oakley

unread,
Jun 25, 2008, 1:22:10 PM6/25/08
to

Images in tcl will never shrink (or grow) to fit. Tk images aren't
scaleable. Typically it works the other way around, with the label
growing or shrinking to fit the image. Though, this behavior depends on
how the widget is configured and managed. If the labels are a fixed
size, the cropping behavior is what you get.

As for the effect of the -shrink option -- it doesn't do what you seem
to think it does. All it will do is resize the destination image to fit
what's being copied in at the time of the copy. So, if im1 is smaller
than im2 (impossible in the above case since you didn't specify a size),
im2 will be the same size as im1 when you copy im1 into it.

Bryan Green

unread,
Jun 25, 2008, 1:41:42 PM6/25/08
to
> Images in tcl will never shrink (or grow) to fit. Tk images aren't
> scaleable. Typically it works the other way around, with the label
> growing or shrinking to fit the image. Though, this behavior depends on
> how the widget is configured and managed. If the labels are a fixed
> size, the cropping behavior is what you get.
>
> As for the effect of the -shrink option -- it doesn't do what you seem
> to think it does. All it will do is resize the destination image to fit
> what's being copied in at the time of the copy. So, if im1 is smaller
> than im2 (impossible in the above case since you didn't specify a size),
> im2 will be the same size as im1 when you copy im1 into it.

Interesting...the TKPhotoLab demo (and other examples I've seen) lead one
to believe that one can automatically shrink down images to fit on
widgets. Thanks for clearing this up for me. I'll use ImageMagick to
create temporary copies of images for display in widgets...

Bryan

keithv

unread,
Jun 25, 2008, 3:48:36 PM6/25/08
to

If you're on Windows and only need scaling, I've found that
xphoto (http://wiki.tcl.tk/11924) to be much lighter
weight and simpler to use than ImageMagick.

Keith

sp...@controlq.com

unread,
Jun 25, 2008, 3:56:13 PM6/25/08
to

On Wed, 25 Jun 2008, Bryan Green wrote:

> Date: Wed, 25 Jun 2008 12:41:42 -0500
> From: Bryan Green <bcg...@gmail.com>
> Newsgroups: comp.lang.tcl
> Subject: Re: Shrink Image to Fit Widget?

#
# img_scale not fast, but does not require tclmagick ...
proc img_scale { im mx_wd mx_ht } {

set w [ image width $im ]
set h [ image height $im ]

set iw $mx_wd
set ih $mx_ht

set new [ image create photo -format jpeg -width $iw -height $ih ]
set smp_x [ expr $w / $iw ]
set smp_y [ expr $h / $ih ]

$new copy $im -subsample $smp_x $smp_y -shrink -to 0 0 $iw $ih
return $new
}

HTH,
Rob Sciuk
---- Posted via Pronews.com - Premium Corporate Usenet News Provider ----
http://www.pronews.com offers corporate packages that have access to 100,000+ newsgroups

Bryan Green

unread,
Jun 25, 2008, 5:01:53 PM6/25/08
to
Thanks, I'll give this a go-- I've tried subsample in the past, but I
don't think I was using it correctly. This'll help!

Thanks all!
Bryan

Gerhard Reithofer

unread,
Jun 25, 2008, 8:17:10 PM6/25/08
to
On Wed, 25 Jun 2008, Bryan Green wrote:

> > Images in tcl will never shrink (or grow) to fit. Tk images aren't
> > scaleable. Typically it works the other way around, with the label

...

> > im2 will be the same size as im1 when you copy im1 into it.
>
> Interesting...the TKPhotoLab demo (and other examples I've seen) lead one
> to believe that one can automatically shrink down images to fit on
> widgets. Thanks for clearing this up for me. I'll use ImageMagick to
> create temporary copies of images for display in widgets...

Scaling and subsampling by integer factor is possible.

proc scale_up { c id fct up } {
global the_img
if {$up} {set op "zoom"} {set op "subsample"}
# make the copy
set tmp [image create photo]
$tmp copy $the_img -$op $fct $fct -shrink
image delete $the_img
set the_img $tmp
# display scaled image
$c itemconfigure $id -image $the_img
}

pack [canvas .c]
set the_img [image create photo -file [lindex $argv 0]]
set id [.c create image 0 0 -anchor nw -image $the_img -tags visimg]

# "+" and "-" as scaling keys.
bind . + [list scale_up .c $id 2 1]
bind . - [list scale_up .c $id 2 0]
bind . <Escape> exit

--
Gerhard Reithofer
Tech-EDV Support Forum - http://support.tech-edv.co.at

0 new messages