Tuesday, 22 May 2018

Imageio.imread() Replaces Scipy.misc.imread()

Some of the code we wrote reads data from image files using a helper function scipy.misc.imread().

However, recently, users were notified that this function is deprecated:


We're encouraged to use the imageio.imread() function instead.

From imread() to imread()

The change is very easy. We first change the import statements which include the helper library.

From this:
import scipy.misc

To this:
import imageio

We then change the actual function which reads image data from files.

From this form:
img_array = scipy.misc.imread(image_file_name, flatten=True)

To this form:
img_array = imageio.imread(image_file_name, as_gray=True)

Easy!

We can see the new function is used in a very similar way. We still provide the name of the image file we want to read into a array of data.

Previously we used flattern=True to convert the image pixels into a greyscale value, instead of having separate numbers for the red, green, blue and maybe alpha channels. We now use as_grey=True which does the same thing.

I thought we might have to mess about with inverting number ranges from 0-255 to 255-0 but it seems we don't need to.


Github Code Updated

The notebooks which use imread() have been updated on the main github repository.

This does mean the code is slightly different to that described in the book, but the change should be easy to understand until a new version of the book is released.

No comments:

Post a Comment