Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent for using matplotlib.image in ruby

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

like image 629
Rockwell Rice Avatar asked Mar 16 '18 20:03

Rockwell Rice


People also ask

How do I read an image in matplotlib?

The imread() function in pyplot module of matplotlib library is used to read an image from a file into an array. Parameters: This method accepts the following parameters. fname : This parameter is the image file to read. format: This parameter is the image file format assumed for reading the data.

How do I save a matplotlib file as a JPEG?

To save plot figure as JPG or PNG file, call savefig() function on matplotlib. pyplot object. Pass the file name along with extension, as string argument, to savefig() function.

What is matplotlib in image processing?

The matplotlib function imshow() creates an image from a 2-dimensional numpy array. The image will have one square for each element of the array. The color of each square is determined by the value of the corresponding array element and the color map used by imshow() .


1 Answers

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.

like image 180
Alexander Presber Avatar answered Oct 11 '22 17:10

Alexander Presber