# start drawing grey image
for word, freq in frequencies:
if freq == 0:
continue
# select the font size
rs = self.relative_scaling
if rs != 0:
font_size = int(round((rs * (freq / float(last_freq))
+ (1 - rs)) * font_size))
if random_state.random() < self.prefer_horizontal:
orientation = None
else:
orientation = Image.ROTATE_90
tried_other_orientation = False
while True:
# try to find a position
font = ImageFont.truetype(self.font_path, font_size)
# transpose font optionally
transposed_font = ImageFont.TransposedFont(
font, orientation=orientation)
# get size of resulting text
box_size = draw.textbbox((0, 0), word, font=transposed_font, anchor="lt")
# find possible places using integral image:
result = occupancy.sample_position(box_size[3] + self.margin,
box_size[2] + self.margin,
random_state)
・・・省略・・・
# cython: language_level=3
# cython: boundscheck=False
# cython: wraparound=False
import array
import numpy as np
def query_integral_image(unsigned int[:,:] integral_image, int size_x, int
size_y, random_state):
cdef int x = integral_image.shape[0]
cdef int y = integral_image.shape[1]
cdef int area, i, j
cdef int hits = 0
# count how many possible locations
for i in xrange(x - size_x):
for j in xrange(y - size_y):
area = integral_image[i, j] + integral_image[i + size_x, j + size_y]
area -= integral_image[i + size_x, j] + integral_image[i, j + size_y]
if not area:
hits += 1
if not hits:
# no room left
return None
# pick a location at random
cdef int goal = random_state.randint(0, hits)
hits = 0
for i in xrange(x - size_x):
for j in xrange(y - size_y):
area = integral_image[i, j] + integral_image[i + size_x, j + size_y]
area -= integral_image[i + size_x, j] + integral_image[i, j + size_y]
if not area:
hits += 1
if hits == goal:
return i, j