TkPhotoImage.copy always gives "too many colors" Error

Discussion in 'Ruby' started by C. Dagnon, May 18, 2009.

  1. C. Dagnon

    C. Dagnon Guest

    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...
    --
    Posted via http://www.ruby-forum.com/.
    C. Dagnon, May 18, 2009
    #1
    1. Advertising

  2. From: "C. Dagnon" <>
    Subject: TkPhotoImage.copy always gives "too many colors" Error
    Date: Mon, 18 May 2009 08:21:57 +0900
    Message-ID: <>
    > 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?".
    --
    Hidetoshi NAGAI ()
    Hidetoshi NAGAI, May 18, 2009
    #2
    1. Advertising

  3. C. Dagnon

    C. Dagnon Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
    C. Dagnon, May 18, 2009
    #3
  4. From: "C. Dagnon" <>
    Subject: Re: TkPhotoImage.copy always gives "too many colors" Error
    Date: Mon, 18 May 2009 21:59:33 +0900
    Message-ID: <>
    > 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.
    --
    Hidetoshi NAGAI ()
    Hidetoshi NAGAI, May 18, 2009
    #4
  5. C. Dagnon

    C. Dagnon Guest

    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 :(

    -Chris
    --
    Posted via http://www.ruby-forum.com/.
    C. Dagnon, May 19, 2009
    #5
  6. From: "C. Dagnon" <>
    Subject: Re: TkPhotoImage.copy always gives "too many colors" Error
    Date: Tue, 19 May 2009 08:25:11 +0900
    Message-ID: <>
    > 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 ?
    --
    Hidetoshi NAGAI ()
    Hidetoshi NAGAI, May 19, 2009
    #6
  7. C. Dagnon

    C. Dagnon Guest

    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 :( RDoc rarely counts, unfortunately.

    -Chris
    --
    Posted via http://www.ruby-forum.com/.
    C. Dagnon, May 19, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew
    Replies:
    3
    Views:
    445
    Ken Cox [Microsoft MVP]
    Sep 3, 2003
  2. Ara.T.Howard

    TkPhotoImage :data=>buf

    Ara.T.Howard, Feb 21, 2004, in forum: Ruby
    Replies:
    2
    Views:
    160
    Ara.T.Howard
    Feb 24, 2004
  3. email55555 email55555

    [Tk] TkPhotoImage question

    email55555 email55555, Dec 26, 2004, in forum: Ruby
    Replies:
    1
    Views:
    129
    Hidetoshi NAGAI
    Dec 26, 2004
  4. Replies:
    2
    Views:
    101
  5. Ed Redman

    using TkPhotoImage

    Ed Redman, Oct 20, 2007, in forum: Ruby
    Replies:
    1
    Views:
    125
    Hidetoshi NAGAI
    Oct 21, 2007
Loading...

Share This Page