Getting text string bitmask in python

I am reading this word-cloud lesson , and he needs forms of rasterized lines. Then one could compute the intersections of the words shape with numpy.

The author in the tutorial complains about the Python Image Library . A more direct way can use pyCairo. However, I could not help but find a bitmask corresponding to each glyph.

Ideally, I would like to enter 1 and return some collection of 1 and zeros that can be put into numpy.

0000011100000
0000111100000
0001111100000
0000011100000
0000011100000
0000011100000
0000011100000
0000011100000
1111111111111
1111111111111

Here is my attempt with Cairo, but I cannot get a bitmash from Cairo or draw it or something else:

import cairo as cr
WIDTH, HEIGHT = 256, 256

surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context (surface)

ctx.set_source_rgb(0.0, 0.0, 0.0)
ctx.select_font_face("Georgia", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
ctx.set_font_size(1.2)
x_bearing, y_bearing, width, height = ctx.text_extents("a")[:4]
ctx.move_to(0.5 - width / 2 - x_bearing, 0.5 - height / 2 - y_bearing)
ctx.show_text("a")

In fact, any reliable way to get ascii representations of numbers might be appropriate.

+3
1

numpy :

import numpy as np
ar = np.frombuffer(surface.get_data(), dtype=np.int32)
bitmask = (ar.reshape(WIDTH, HEIGHT) != 0)

, , matplotlib:

import matplotlib.pyplot as plt
plt.imshow(bitmask)
plt.show()
0

All Articles