10.6.4 Displaying Card Images with Matplotlib

  • Let’s display Card images
  • We downloaded public-domain card images from Wikimedia Commons:

https://commons.wikimedia.org/wiki/Category:SVG_English_pattern_playing_cards

  • Located in the ch10 examples folder’s card_images subfolder
In [1]:
from deck import DeckOfCards
In [2]:
deck_of_cards = DeckOfCards()

Enable Matplotlib in IPython

  • inline required only in Jupyter
In [3]:
%matplotlib inline

Create the Base Path for Each Image

  • Use the pathlib module’s Path class to construct the full path to each image on our system
  • Path method joinpath appends the subfolder containing the card images to the path for the current folder (.)
In [4]:
from pathlib import Path
In [5]:
path = Path('.').joinpath('card_images')

Import the Matplotlib Features

  • We’ll use a function from matplotlib.image to load the images
In [6]:
import matplotlib.pyplot as plt
In [7]:
import matplotlib.image as mpimg

Create the Figure and Axes Objects

  • NOTE: In Jupyter, all code that modifies the on-screen presentation of a Figure must be in one cell, so we combined several cells below

  • The first statement in the following cell uses Matplotlib function subplots to create a Figure object in which we’ll display the images as 52 subplots with four rows (nrows) and 13 columns (ncols)

  • Returns a tuple containing the Figure and an array of the subplots’ Axes objects
In [8]:
figure, axes_list = plt.subplots(nrows=4, ncols=13)

# added next two statements to increase figure size in notebook
figure.set_figwidth(16)
figure.set_figheight(9)

for axes in axes_list.ravel():
    axes.get_xaxis().set_visible(False)
    axes.get_yaxis().set_visible(False)
    image_name = deck_of_cards.deal_card().image_name
    img = mpimg.imread(str(path.joinpath(image_name).resolve()))
    axes.imshow(img)

figure.tight_layout()

Configure the Axes Objects and Display the Images

  • The loop iterates through all the Axes objects in axes_list
    • ravel provides a one-dimensional view of a multidimensional array
  • For each Axes object,
    • hide the x- and y-axes.
    • deals a Card and get its image_name
    • get full path to the image
    • use matplotlib.image module’s imread function to load the image
    • call Axes method imshow to display the current image in the current subplot

Maximize the Image Sizes

  • Matplotlib Figure object’s tight_layout method removes most of the extra white space in the window

Shuffle and Re-Deal the Deck

In [9]:
deck_of_cards.shuffle()
In [10]:
# added this statement to create a separate figure in the notebook
figure, axes_list = plt.subplots(nrows=4, ncols=13)

# added next two statements to increase figure size in notebook
figure.set_figwidth(16)
figure.set_figheight(9)

for axes in axes_list.ravel():
    axes.get_xaxis().set_visible(False)
    axes.get_yaxis().set_visible(False)
    image_name = deck_of_cards.deal_card().image_name
    img = mpimg.imread(str(path.joinpath(image_name).resolve()))
    axes.imshow(img)
    
# added this statement for execution in the notebook
figure.tight_layout()

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.