Pregunta

I am reading this tutorial on word-clouds and it requires the shapes of rasterized strings. Then one could compute intersections of the word shapes with numpy.

The author in the tutorial complains about Python Image Library. A more direct way, might be using pyCairo. However, I couldn't not get the find the bitmask corresponding to each glyphs.

Ideally, I would like to input 1 and return some collection of 1's and zeros, that I can put into numpy.

0000011100000
0000111100000
0001111100000
0000011100000
0000011100000
0000011100000
0000011100000
0000011100000
1111111111111
1111111111111

Here is my attempt with Cairo, but I can't get the bitmask out of Cairo or draw it or anything 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 of getting ascii representations of numbers might be appropriate.

¿Fue útil?

Solución

After running your above code, you can convert the rendered letter to a numpy boolean array as follows:

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

You can verify that it worked by displaying the resulting array with matplotlib:

import matplotlib.pyplot as plt
plt.imshow(bitmask)
plt.show()
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top