Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pierian-data

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: pierian-data/complete-python-3-bootcamp
Path: blob/master/14-Working-with-Images/02-Image-Exercise-Solution.ipynb
Views: 648
Kernel: Python 3


Content Copyright by Pierian Data

Image Exercise - Solution

In the folder "Working with Images" (same folder this notebook is located in) there are two images we will be working with:

  • word_matrix.png

  • mask.png

The word_matrix is a .png image that contains a spreadsheet of words with a hidden message in it.

Your task is to use the mask.png image to reveal the hidden message inside the word_matrix.png. Keep in mind, you may need to resize the mask.png in order for this to work.

This exercise is more open-ended, so we won't guide you with the steps, instead, letting you explore and figure things out on your own as you would in a real world situation. However, if you get stuck, you can always view the solutions video or notebook for guidance. Best of luck!

Import Images

from PIL import Image
words = Image.open('word_matrix.png')
words
Image in a Jupyter notebook
mask = Image.open("mask.png")
mask
Image in a Jupyter notebook

Resize Images to Match

mask.size
(505, 251)
words.size
(1015, 559)
mask = mask.resize((1015,559))
mask.size
(1015, 559)

Add in alpha parameter

Now we can't just paste them over, otherwise we won't see what is underneath, we need to add an alpha value.

mask.putalpha(200) # links.putalpha(128)
mask
Image in a Jupyter notebook
words.paste(mask,(0,0),mask)
words
Image in a Jupyter notebook

Excellent! Hope you enjoyed this one!