Cannot Use Images in Ruby

Hi there! This is Todd again. :smiley:

I have a problem with displaying images in Ruby. It just doesn’t work,
no matter what I do. I’ve tried different methods, but still no results.

Here is one particular method…

require ‘tk’

$resultsVar = TkVariable.new
root = TkRoot.new
root.title = “Window”

image = TkPhotoImage.new
image.file = “TEST.png”

label = TkLabel.new(root)
label.image = image
label.place(‘height’ => image.height,
‘width’ => image.width,
‘x’ => 10, ‘y’ => 10)
Tk.mainloop

When I try to run this particular file, I keep getting “Couldn’t open
TEST.png; No such file or directory”.

I’ve tried inserting the entire file path and I’ve tried just using it
as shown in the above example. No results. I just keep getting error
messages. I don’t know what file path it’s looking for or what directory
I’m supposed to be in. I’m very lost there.

I’ve also tried this method…

#!/usr/bin/env ruby

require ‘tk’
require “tkextlib/tkimg/jpeg”
require “open-uri”

photo = open(“IMAGE URL GOES HERE”, “rb”) {|io| io.read}

TkRoot.new {title “Fox” }

TkLabel.new {
image TkPhotoImage.new( :data => Tk::BinaryString( photo ) )
width 300
pack
}

TkLabel.new {
font TkFont.new( ‘verdana 24 bold’ )
text “The Great Fox Was Here!”
pack
}

TkButton.new {
text ‘Quit’
command ‘exit’
pack
}

Tk.mainloop

It will not load anything for this. It keeps telling me “TkPackage
can’t find package img::jpeg”. I’ve tried a few different file types,
but none work, including GIF and PNG. I’ve checked the code over and
over. It’s all precisely like it was in the book. I only changed the
text and tried to use other images from the internet. I also uploaded an
image to my own website, and that didn’t work either.

So I have two problems here (Can’t use images from my computer or the
internet) and no solutions. Any help? It would be much
appreciated.

By the way, I figured out how to get TK working, so it’s not that.

  • Todd

Hi Todd,

I haven’t used tk ruby, but looking at the documentation:

http://ruby-doc.org/stdlib-2.0.0/libdoc/tk/rdoc/TkPhotoImage.html#method-c-new

It looks like you might want to try TkPhotoImage#read to use an image
file. Otherwise, the constructor seems to take a hash of options, like:

image = TkPhotoImage.new(:file => ‘TEST.png’)

Thank you for the advice!

I finally managed to fix one of my problems, and that was with the first
method. That is, using files from the computer rather than the internet.

What I realized is that, for some reason, Tk doesn’t know how to use
files other than GIF. So, in a way, I’m still having a problem, because
I could only figure out how to use GIF’s, and Tk is supposed to be
capable of using more file types. I was even able to find the relevant
files for other file types, which makes me even more confused.

So this is what I got working…

require ‘tk’

$resultsVar = TkVariable.new
root = TkRoot.new
root.title = “Window”

image = TkPhotoImage.new
image.file = “C:/FILE PATH GOES HERE/image.gif”

label = TkLabel.new(root)
label.image = image
label.place(‘height’ => image.height,
‘width’ => image.width,
‘x’ => 10, ‘y’ => 10)
Tk.mainloop

But, like I said, it only works with GIF.

However, I still haven’t solved my internet image file problem yet.

Well this is weird, I think other image formats should work too.

But the most important thing is to get any widget to display. Do buttons
and text work? :smiley: If so, you can start with error handling.

For images from the www, this should be simple too - I think you can use
the open-uri library to download binary data too; store this stream and
then open it. (If you know it is a .gif then store it locally in a .gif
file).

But first I would start with buttons + normal text, and then, try to see
if .gif files really work and THEN after you have done this, try the
third intermittent step and download the remote binary dataset (this may
be a bit difficult for a newcomer but if you tackled the earlier two
steps already, the last one will not be too hard - there are lots of
stackoverflow questions for this, if you are systematic, you can solve
it too eventually, you’ll see that in the next 4 weeks I am sure)

Todd F. wrote in post #1184192:

Hi there! This is Todd again. :smiley:

I have a problem with displaying images in Ruby.

gem install Ruiby
ruiby “image(‘test.png’); button(‘ok’) { ruiby_exit }”

Thank you for your advice.

Yes, I can make buttons and normal text. My only problem with that is
that I only know how to adjust the window size by adjusting the empty
area around the text (I can adjust the actual text size). It sounds
really confusing, but that is what’s been happening for me. I need to
figure out how to adjust the window size by simply adjusting the window
size.

I’m still very new, so, yeah, I don’t entirely understand what you’re
saying, but I can still try it anyway. I learn through changing code
over and over again rather than just reading instructions.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs