[solved] Display Image from URL with Tk

I’m new to Ruby, and experiment around a bit. I want to grab the image
of a webcam, and show it with Tk in a window:

#!/usr/bin/ruby
require ‘tk’
image = TkPhotoImage.new(:file => “http://www.exampleurlhere.jpg”)
label[‘image’] = image
Tk.mainloop

As result, I get some errors:

/usr/local/lib/ruby/2.2.0/tk.rb:2061:in _invoke': couldn't open "http://www.exampleurlhere.jpg": no such file or directory (RuntimeError) from /usr/local/lib/ruby/2.2.0/tk.rb:2061:in _ip_invoke_core’
from /usr/local/lib/ruby/2.2.0/tk.rb:2097:in _tk_call_core' from /usr/local/lib/ruby/2.2.0/tk.rb:2125:in tk_call_without_enc’
from /usr/local/lib/ruby/2.2.0/tk/image.rb:80:in initialize' from /usr/local/lib/ruby/2.2.0/tk/image.rb:191:in initialize’
from /usr/local/lib/ruby/2.2.0/tk/image.rb:58:in block (2 levels) in new' from /usr/local/lib/ruby/2.2.0/tk/image.rb:57:in synchronize’
from /usr/local/lib/ruby/2.2.0/tk/image.rb:57:in block in new' from /usr/local/lib/ruby/2.2.0/tk/image.rb:56:in instance_eval’
from /usr/local/lib/ruby/2.2.0/tk/image.rb:56:in new' from wc1.rb:3:in

What is wrong?

Greetings, Dino
from Germany

I have a new code that works with a local image file:

#!/usr/bin/ruby

require ‘tk’
require ‘tkextlib/tkimg’

$resultsVar = TkVariable.new
root = TkRoot.new
root.title = “Webcam-Picture”

image = TkPhotoImage.new
image.file = “image.jpg”

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

How can I replace “image.jpg” through an image file on a web page
(Webcam), for example http://example.com/image.jpg?

Now I just have to get out, which is shown the image of the desired
size, and the windows on the desktop is centered :slight_smile:

Now, it is perfect for me:

#!/usr/bin/ruby

require ‘tk’
require ‘tkextlib/tkimg’
require ‘net/http’

root = TkRoot.new
root.title = ‘MyWebcam’
root.geometry(‘704x576+300+300’)

File.write(‘/tmp/webcam.jpg’,
Net::HTTP.get(URI.parse(‘http://example.com/image.jpg’)))

image = TkPhotoImage.new
image.file = ‘/tmp/webcam.jpg’

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

Ruby is great!

The problem is solved. By taste and look with duckduckgo.com I have the
following code generated:

#!/usr/bin/ruby

require ‘tk’
require ‘tkextlib/tkimg’
require ‘net/http’

$resultsVar = TkVariable.new
root = TkRoot.new
root.title = ‘MyWebcam’

File.write(‘image.jpg’,
Net::HTTP.get(URI.parse(‘http://example.com/image.jpg’)))

image = TkPhotoImage.new
image.file = ‘image.jpg’

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

Now I just have to get out, which is shown the image of the desired
size, and the windows on the desktop is centered :slight_smile: