| 156 | return d |
| 157 | |
| 158 | def draw_text(self, text, font_path="AlibabaPuHuiTi-3-85-Bold.ttf"): |
| 159 | R = self.ttf_size |
| 160 | fs = int(0.8*R) |
| 161 | interval = 128 // self.max_len |
| 162 | img_tensor = torch.ones((self.max_len, R, R), dtype=torch.float) |
| 163 | for i, char in enumerate(text): |
| 164 | img = Image.new('L', (R, R), 255) # Create a white image |
| 165 | draw = ImageDraw.Draw(img) |
| 166 | font = ImageFont.truetype(font_path, fs) |
| 167 | text_size = font.getsize(char) |
| 168 | text_position = ((R - text_size[0]) // 2, (R - text_size[1]) // 2) |
| 169 | draw.text(text_position, char, font=font, fill=interval*i) |
| 170 | img_tensor[i] = torch.from_numpy(np.array(img)).float() / 255.0 |
| 171 | return img_tensor |
| 172 | |
| 173 | def draw_glyph(self, text, rect, font_path="AlibabaPuHuiTi-3-85-Bold.ttf"): |
| 174 | resolution = self.resolution |