8

Been experimenting with using Ruby inside a jupyter notebook. With Python I can do this

import matplotlib.image as mpimg

Does anyone know the equivalent with Ruby, I have not been able to find it in any of the iRuby or sciruby documentation?

To clarify a little, In my gemfile I have this

gem 'iruby'
gem 'cztop'
gem 'matplotlib'

But cannot seem to get access to the image part of matplotlib that I am use to using in python.

I am trying to find the Ruby version of what, in Python, I would write like this

#importing some useful packages
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
...

%matplotlib inline

#reading in an image
image = mpimg.imread('test_images/solidWhiteRight.jpg')

#printing out some stats and plotting
print('This image is:', type(image), 'with dimesions:', image.shape)
plt.imshow(image)  #call as plt.imshow(gray, cmap='gray') to show a grayscaled image

Thanks so much for any suggestions

Rockwell Rice
  • 3,376
  • 5
  • 33
  • 61
  • [This thread](https://stackoverflow.com/q/1113611/9335023) you may find helpful, especially section `Python has flexible name space handling` and [this](https://stackoverflow.com/a/1114551/9335023) answer. – Maciej Nędza Mar 16 '18 at 21:22
  • Sure I see how those are useful, my question is bigger scope, I cannot find any way to access the library I need because it is not even present in the ruby environment and I am curious how I get it in Ruby, or if it exists in a Ruby version. – Rockwell Rice Mar 16 '18 at 22:16
  • No, AFAIK it does not exist in Ruby version. Without saying exactly what you need from it, I might suggest mini_magick (which shoulld already be supported by iRuby), or maybe chunky_png (you can put an image from it into iRuby using `IRuby.display png.to_blob, mime: "image/png"`). – Amadan Mar 17 '18 at 00:20

1 Answers1

7

This is how far I can get it to work in jupyter notebook on MacOSX:

require 'matplotlib/pyplot'
plt = Matplotlib::Pyplot

image = plt.imread 'test.png'

puts "This image's dimensions: #{image.shape}"

plt.imshow(image)
plt.show()

I used your Gemfile with the additional gem rbczmq to avoid the kernel dying (hint found here):

gem 'iruby'
gem 'cztop'
gem 'matplotlib'
gem 'rbczmq'

Note that I used a .png because matplotlib can only read PNGs natively without PIL installed.

This is how the result will look like:

enter image description here

Displaying the result inline as in the python version:

enter image description here

seems to be impossible.

Alexander Presber
  • 6,429
  • 2
  • 37
  • 66
  • Did it actually show you the image back? Mine prints the dimensions but does not show the image when using your code. `plt.imshow(image)` should display the image under the dimensions. – Rockwell Rice Mar 21 '18 at 14:16
  • `plt.imshow(image)` does not show the result of the rendering, you have to explicitly call `plt.show()`. – Alexander Presber Mar 21 '18 at 14:24
  • Ya sorry put wrong code in there and could not edit it after 5 minutes. I tried with the exact thing but did not work, I am trying to install some things and see if I can get that – Rockwell Rice Mar 21 '18 at 14:35
  • well I need this to work either by displaying inline in the notebook (ideally), or at least work from running a file. But I cannot get this to display anything at all. – Rockwell Rice Mar 21 '18 at 15:03