TkPhotoImage.copy always gives "too many colors" Error


#1

I’m working to copy and eventually manipulate images within Ruby, but
the Tk extension seems to be broken. My program (minus some crud) looks
like this:

require ‘tk’
require ‘tk/image’
require ‘tkextlib/tkimg’

Settings

image_file_suffix = ‘.jpg’
final_filename = “final.jpg”

Find all the separate images

Dir.chdir( to_dir )

image_filenames = []
regex = Regexp.compile("^[0-9]+.*\#{image_file_suffix}$")
Dir.foreach(".") {|filename| image_filenames << filename if filename =~
regex }

photos = []
pixel_height = 0
pixel_width = 0
image_filenames.each{ |image_fn|
image = TkPhotoImage.new( :file => image_fn )
photos << image
pixel_width += photo.width
pixel_height = photo.height > pixel_height ? photo.height :
pixel_height
}

compound_image = TkPhotoImage.new( :height => pixel_height, :width =>
pixel_width )
compound_image.blank

curr_x = 0
photo = photos[0]

All give C:/ruby/ruby1_86_26/lib/ruby/1.8/tk.rb:2272:in `__invoke’:

too many colors (RuntimeError)
#compound_image.copy( photo, :from => [0,0,photo.width, photo.height] )
#data = photo.data( :from => [0,0,photo.width, photo.height] )
#compound_image.copy( photo )
compound_image.put( photo.data )

compound_image.write final_filename

So I’ve tried the 4 different commands near the end and everything
fails. In particular I added a puts at the tk.rb method mentioned and
get the following out using any .copy() call:

INVOKE: [“i00002”, “copy”, “i00000”]
INVOKE: [“i00002”, “write”, “final.jpg”]
INVOKE: [“rename”, “INTERP_FINALIZE_HOOK”, “”]

And it is always after the last one when the error gets thrown. The
.put(.data()) call dumps all the data to the console (and takes a looong
time), but otherwise the last 2 commands and the error are always the
same. The file actually created is only 6 bytes - not a good result
when trying to copy a 35K file over… I’ve tried it as a simple way to
copy a couple different images with the same results. Google only finds
me many versions of a code file with INTERP_FINALIZE_HOOK in it -
nothing useful nor explanatory except possibly a couple links at the
very end in Japanese.

Any ideas appreciated!

-Chris
WinXP SP3?, Ruby 1.8.6, ActiveTcl 8.4.17.0…


#2

From: “C. Dagnon” removed_email_address@domain.invalid
Subject: TkPhotoImage.copy always gives “too many colors” Error
Date: Mon, 18 May 2009 08:21:57 +0900
Message-ID: removed_email_address@domain.invalid

too many colors (RuntimeError)

This error message is generated in a GIF write function of Tcl/Tk
(“CommonWriteGIF” function in stardard Tcl/Tk and tkImg extension),
when color map size of an image is larger than 256.
So, it doesn’t depend on Ruby/Tk.

I’m sorry but I have no solution.
The only what I can say is “Could you try to use ‘format’ option?”.


#3

Yes, I am certainly willing to try :format, however I only have PERL
examples and adding

, :format => ‘jpeg’

to the end of both TkPhotoImage.new() calls ends up with the same error.
I tried :jpeg and :jpg also, though for :jpg it says that format is not
supported. I also tried all the permutations of adding :format between
the 2 .new() calls and get the same error each time.

I added a puts in tk/image.rb’s write(), but that just shows that it is
going to the correct filename with no options specified. I could try
higher up in the stack (4 more methods) but they are all there just to
call the Tk underbelly, right?

Locally I see that /ruby/1.8/tkextlib/tkimg/jpeg.rb exists. I realize
that this is the first time I’ve tried saving an image from Ruby/Tk,
however given the focus of your comment should I assume that my code at
least looks correct? So is the conclusion that the ActiveTcl
implementation (the only one available for Windows, I’ve heard, a
commercially dependant binary) has errors?

Thanks,

-Chris


#4

From: “C. Dagnon” removed_email_address@domain.invalid
Subject: Re: TkPhotoImage.copy always gives “too many colors” Error
Date: Mon, 18 May 2009 21:59:33 +0900
Message-ID: removed_email_address@domain.invalid

however given the focus of your comment should I assume that my code at
least looks correct? So is the conclusion that the ActiveTcl
implementation (the only one available for Windows, I’ve heard, a
commercially dependant binary) has errors?

Your code fixed your typo (e.g. “image” <-> “photo” in a each block
of “image_filenames”) works properly on my linux box and windows box.
The error may depend on your image files.
If so, and if the image files are not broken, it maybe a problem on
the ActiveTcl.


#5

Hmmm,

I tried a separate JPEG from a camera which displays correctly with
other programs as a straight copy and got the same errors. Of course
the original images were ones I cut out of a JPEG using GIMP, so I don’t
think it is the original format which is the problem. I’d be happy to
try other images if you have suggestions - color tests or what not, or
suggestions on what makes a good image file…

I also tried it on a Mac OS X 10.4.11 and again had the same GIF “too
many colors” error. That would mean it is some problem in the Ruby-Tk
wrapper, correct?

I wonder if they ever tested writing a JPEG file. But again I’m unsure
that I’m using the API correctly here since I can’t find examples from
Google of anyone doing it. Any alternate code is appreciated.

But perhaps it is onto another UI technology then :frowning:

-Chris


#6

Thanks a lot, Hidetoshi!

For anyone still following this thread the solution is:

composite.write( ‘filename.jpg’, :format => :jpeg )

I had somehow assumed that we were dealing with objects, internal state
and memory, but we’re not. Even though I created the composite image
with :format, Ruby/Tk only sends the immediate command to tk, so write()
also needs the format parameter. I was additionally confused because
the Perl/Tk book I have as a reference also has a table stating which
image formats do not need format specified in the calls, and JPEG is one
of those which doesn’t - there at least.

Next question is: how do I give Hidetoshi NAGAI a bump? Do we have any
star ratings here? 8)

Cryptic non-documented APIs (along with the code) seem to be Ruby’s
trademark :frowning: RDoc rarely counts, unfortunately.

-Chris


#7

From: “C. Dagnon” removed_email_address@domain.invalid
Subject: Re: TkPhotoImage.copy always gives “too many colors” Error
Date: Tue, 19 May 2009 08:25:11 +0900
Message-ID: removed_email_address@domain.invalid

I tried a separate JPEG from a camera which displays correctly with
other programs as a straight copy and got the same errors.

Could you send me the image files which make errors?
I cannot investigate the trouble, because I cannot re-generate it.
Although I’m not an expert enough to check image formats,
I need samples to make the same error at least.

I also tried it on a Mac OS X 10.4.11 and again had the same GIF “too
many colors” error. That would mean it is some problem in the Ruby-Tk
wrapper, correct?

I think that the method which make the error calls a Tcl/Tk function
only. And the error message is made at the Tcl/Tk function.
If you can write a Tcl/Tk script, please execute the same operation
for the images. Of course, your “wish” command uses the same Tcl/Tk
libraries (libtcl.so and libtk.so) with Ruby’s tcltklib.so.
If it makes the same error, your trouble depends on your Tcl/Tk and/or
your environment. But if not, the trouble possibly depends on Ruby/Tk.

Is your window system’s color depth larger than 8bit ?