Learning Art Using Python and Anki

Let’s learn art by transforming The Art Book into a spaced-repetition Anki Deck.

Step 1: Download The Art Book

Get the PDF of this book and save it as TheArtBook.pdf

Step 2: Make an image of each page

Let’s use python and a library called pdf2image does what it says on the tin: saves each page as eg page5.jpg.

pip install pdf2image

pages = convert_from_path('TheArtBook.pdf')

for idx,page in enumerate(pages):
	page.save('page'+str(idx)+'.jpg', 'JPEG')
One jpg for each page of the pdf

Step3: Crop Images into Text, Painting

Let’s use another Python library PIL to crop images into their text and painting parts and save as eg page5-text.jpg for the text and page5-painting.jpg for the image. There are pages 5 to 502.

from PIL import Image

for page in range(5, 503):
    im = Image.open(r"page" + str(page) + ".jpg")
    width, height = im.size

    left = 0
    top = 0
    right = width
    bottom = 300

    im1 = im.crop((left, top, right, bottom))
    im1 = im1.save("page" + str(page) + "-text.jpg")

    im2 = im.crop((left, 300, right, height-80))
    im2 = im2.save("page" + str(page) + "-painting.jpg")
Image cut into Painting and Text

Step 4: Anki with Python

Let’s use the Python library genanki to create Anki decks and cards for us in Python. Install:

pip install genanki

Copy the Images into the Anki media folder:

~/.local/share/Anki2/User\\ 1/collection.media/

Create an Anki Deck, a Model with Painting as the Question and Text as the Answer, and create a package that can be opened in Anki:

import genanki

# Model of note tempalte with Question = Image and Answer = Text
my_model = genanki.Model(
  'Simple Model with Media',
    {'name': 'QuestionMedia'},
    {'name': 'AnswerMedia'},
      'name': 'Card 1',
      'qfmt': '<img src="{{QuestionMedia}}"/><br>',
      'afmt': '{{FrontSide}}<hr id="answer"><img src="{{AnswerMedia}}"/>',

# Make a deck
my_deck = genanki.Deck(
  'The Art Book')

# Init package and media package
my_package = genanki.Package(my_deck)
my_package.media_files = []

# Loop pages and create notes, add to deck
for page in range (5, 503):
    my_note = genanki.Note(
        fields=['page' + str(page) + '-painting.jpg',
        'page' + str(page) + '-text.jpg'])

# Get media assets 
for page in range (5, 503):
    my_package.media_files.append('page' + str(page) + '-text.jpg')
    my_package.media_files.append('page' + str(page) + '-painting.jpg')

# Make actual packge to import into Anki
my_package = genanki.Package(my_deck)

# genanki.Package(my_deck).write_to_file('TheArtBook.apkg')

Step 5: Use Anki to learn art!

Open the TheArtBook.apkg package in Anki from File -> Import or launch from the command line:

anki TheArtBook.apkg

Now we have a deck for 502 famous pieces of Art we can learn to name using spaced repetition:

Contact: matthewfelgate@gmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s