Flags

Slicing NumPy arrays allows you to edit rectangular blocks of data.

image0

Here is the code that carves out a red square from the image:

from PIL import Image
import numpy as np

a = np.zeros((300, 500, 3), dtype=np.uint8)

a[100:200, 200:299, 0] = 255

im = Image.fromarray(a)
im.save('area.png')

Hints

  • the slicing contains an interval from:to for each dimension

  • the smallest from value is zero

  • the highest to value is the size of that dimension plus one

  • negative numbers count from the back

  • both from and to can be omitted


Challenge

Draw the flag of a country of your choice, e.g.:

image1