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
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.
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.
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() .
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:
Displaying the result inline as in the python version:
seems to be impossible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With