4

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.

john mangual
  • 7,718
  • 13
  • 56
  • 95
  • related: http://stackoverflow.com/questions/9632995/is-there-a-python-library-that-allows-to-easily-print-ascii-art-text – wflynny Feb 20 '14 at 17:37
  • You can start to debug this by using surface write_to_png to check the output is what you think it is. – Stuart Axon Mar 11 '14 at 12:21
  • possible duplicate of [Convert string to binary in python](http://stackoverflow.com/questions/18815820/convert-string-to-binary-in-python) – skrrgwasme Jul 22 '14 at 22:47

1 Answers1

-1

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()
Marcel Stimberg
  • 415
  • 3
  • 9
  • Note that this needs a few edits to your code that I made (but that have not yet been through the review): you did not use the imported cairo as `cr` (I replaced it by a simple `import cairo`), the font size was much too small (I replaced it by 200), and in the `move_to` you have to use `WIDTH/2` and `HEIGHT/2` instead of 0.5 . – Marcel Stimberg Jan 21 '15 at 17:03
  • So in fact the edits have been rejected, I should have just added the changes as a comment. – Marcel Stimberg Jan 22 '15 at 11:14