Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ImageGen:
- def __init__(self, img_path, font_path, font_size=21):
- self.img = Image.open(os.path.join(img_path))
- self.font = ImageFont.truetype(font_path, font_size)
- self.text = ""
- self._img_size = self.img.size
- self.draw = ImageDraw.Draw(self.img)
- @property
- def img_size(self):
- return self._img_size
- def gen_text(self, start_pos, end_pos):
- Rus = RussianDictionary()
- from random import randint
- num = randint(start_pos,end_pos)
- sentense = list()
- for i in range(num):
- sentense.append(Rus.get_random_word())
- self.text = " ".join(sentense)
- def _start_pos(self, width):
- lines = textwrap.wrap(self.text, width)
- _, y = self.draw.textsize(self.text, font=self.font)
- max_width, max_height = 0,0
- for line in lines:
- width, height = draw.textsize(line, font=self.font)
- if width > max_width:
- max_width = width
- if height > max_height:
- max_height = height
- x = randint(1, self.img_size - max_width)
- y = randint(1, self.img_size - y*len(lines))
- return x,y
- def make_text_border(self, pos):
- positions = [list(C_((tmp, pos[1] - 1, pos[1], pos[1] + 1), 2))[:3]
- for tmp in [pos[0] - 1,pos[0],pos[0] + 1]]
- for i in positions:
- for q in i:
- self.draw.text(q, self.text, (0,0,0), font=self.font)
- def gen_image(self):
- lines = textwrap.wrap(self.text, width=25)
- x_text, y_text = self._start_pos(25)
- for line in lines:
- width, height = self.draw.textsize(line, font=self.font)
- make_text_border((x_text , y_text))
- self.draw.text((x_text , y_text), line, font=self.font)
- y_text += height
- def save_image(self, path, name):
- self.img.save(os.path.join(path, name))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement