Tuesday, March 27, 2018

Is there an equivalent for using matplotlib.image in ruby

Leave a Comment

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

1 Answers

Answers 1

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.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment