我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用PIL.ImageDraw.ImageDraw()。
def text2img(text, font_color="Blue", font_size=25): """????? TEXT ???""" font = ImageFont.truetype('data/simsun.ttc', font_size) #?????? text = text.split('\n') mark_width = 0 for i in range(len(text)): (width, height) = font.getsize(text[i]) if mark_width < width: mark_width = width mark_height = height * len(text) #?????? mark = Image.new('RGBA', (mark_width,mark_height)) draw = ImageDraw.ImageDraw(mark, "RGBA") draw.setfont(font) for i in range(len(text)): (width, height) = font.getsize(text[i]) draw.text((0, i*height), text[i], fill=font_color) return mark
def add_num(img): draw = ImageDraw.ImageDraw(img) width, height=img.size fontSize = min(width, height) // 10 fnt = ImageFont.truetype('Arial.tff', fontSize) color = (255,0,0) draw.text((0.9*width, 0.1*height), "1", fill=color, font=fnt) img.save('avatar.jpg', 'jpeg') return
def test_create_draw_object(self): self.img._create_image() self.img._create_draw_object() self.assertIsInstance(self.img.draw, ImageDraw.ImageDraw)
def add_num_to_img(img): draw=ImageDraw.ImageDraw(img) myFont=ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=40) fillcolor='#ff0000' width,height=img.size draw.text((width-40,0),'99',font=myFont,fill=fillcolor) img.save('result.jpg','jpeg') return 0
def code_img(code, size): r = Random() code = code len_code = len(code) font = ImageFont.truetype("Essence_Sans.ttf", size) font_width, font_height = font.getsize(code) font_width += size / 2 print font_width, font_height img = Image.new("RGBA", (font_width, font_height), (255,) * 4) draw = ImageDraw.ImageDraw(img) draw.text((size/10, -size/10), code, font=font, fill=(0, 0, 0)) params = [1, 0, 0, 0, 1 - float(r.randint(1, 10)) / 100, 0, 0.001, float(r.randint(1, 2)) / 500 ] print params img = img.transform((font_width, font_height), Image.PERSPECTIVE, params) img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) img.save("test.jpg")
def draw_poly(image, polygon, color=255): """Draw a polygon in the given color at the given location""" pil_image = fromarray(image) validated_color = color draw = ImageDraw(pil_image) if len(image.shape) > 2 and image.shape[2] > 1: validated_color = tuple(color) draw.polygon(polygon.boundary.coords, fill=validated_color, outline=validated_color) return np.asarray(pil_image)
def getTextInfo(filename): headers = {'Content-Type':'application/x-www-form-urlencoded'} baseUrl = u"https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + getAccessToken() data = {'image': base64.b64encode(open(filename , 'rb').read())} data['recognize_granularity'] = 'small' data['detect_direction'] = True data['vertexes_location'] = True r = requests.post(baseUrl , data = data , headers = headers) info = json.loads(r.text) ''' wordsRes = info['words_result'] words = wordsRes[0]['words'] location = wordsRes[0]['location'] captha = Image.open(filename) draw = ImageDraw.ImageDraw(captha) x = location['left'] y = location['top'] w = location['width'] h = location['height'] draw.rectangle((x , y , x + w , y + h) , outline = 'black') chars = wordsRes[0]['chars'] charMap = {} for item in chars: charMap[item['char']] = item['location'] for item in charMap.values(): x = item['left'] y = item['top'] w = item['width'] h = item['height'] draw.rectangle((x , y , x + w , y + h) , outline = 'blue') ''' return info
def _draw_gradient(image, y1, y2): w = image.width h = y2 - y1 gradient_view = Image.new('RGBA', (w, h)) d = ImageDraw.ImageDraw(gradient_view) for y in range(gradient_view.height): d.line((0, y, w, y), (0, 0, 0, int(y / h * 0.7 * 255))) image.paste(gradient_view, (0, y1), gradient_view)
def _draw_tag(image, tag, tag_type, config_type): label = TextLabel(tag, ImageFont.truetype( os.path.join(CommonUtil.resource_abs_path(), ConfigManager().tag_font(config_type)), ConfigManager().tag_font_size(config_type))) label.size_to_fit() ttype = tag_type if tag_type > len(TAG_COLOR_LIST) - 1: ttype = 0 color = TAG_COLOR_LIST[ttype] # trick: ??N???????????resize?1??? tag_height = ConfigManager().tag_h(config_type) trick_scale = 4 trick_tag_view_size = ((label.fittingSize[0] + tag_height) * trick_scale, tag_height * trick_scale) tag_view = Image.new('RGBA', trick_tag_view_size, (*color, 0)) draw = ImageDraw.Draw(tag_view) draw.pieslice((0, 0, trick_tag_view_size[1], trick_tag_view_size[1]), 90, 270, color) draw.rectangle((trick_tag_view_size[1] / 2, 0, trick_tag_view_size[1] / 2 + label.fittingSize[0] * trick_scale, trick_tag_view_size[1]), color) draw.pieslice((label.fittingSize[0] * trick_scale, 0, trick_tag_view_size[0], trick_tag_view_size[1]), 270, 90, color) tag_view = tag_view.resize((label.fittingSize[0] + tag_height, tag_height), Image.ANTIALIAS) label.draw_label((int(tag_height / 2), int((tag_height - label.fittingSize[1]) / 2)), tag_view) image.paste(tag_view, (ConfigManager().tag_x(config_type), ConfigManager().tag_y(config_type)), tag_view)
def split_char_normal(image, width=64, height=64, char_width=4): """ :param image: :return: images ???????? """ images = [] x = util.projection(image) bounds = [] draw_bounds = [] x = [min(x)] + x + [max(x)] for i in xrange(len(x) - 1): if x[i] <= x[0] < x[i + 1]: bounds.append(i) elif x[i] > x[0] >= x[i + 1]: bounds.append(i - 1) for i in xrange(0, len(x), 2): if i + 1 < len(bounds) and bounds[i + 1] - bounds[i] >= char_width: image_char = image.crop((bounds[i], 0, bounds[i + 1], image.size[1])) y1, y2 = util.get_width(util.projection(image_char, lambda a, b: b), True) sig_char_image = image_char.crop((0, y1, image_char.size[0], y2)) draw_bounds.append((bounds[i], y1, bounds[i + 1], y2)) sig_char_image = sig_char_image.resize((width, height)) images.append(sig_char_image) image = image.convert('RGB') draw = ImageDraw.ImageDraw(image) for bound in draw_bounds: x1, y2, x2, y2 = bound draw.line((x1, y1, x1,y2), fill=(255,0,0), width=2) draw.line((x1, y1, x2,y1), fill=(255,0,0), width=2) draw.line((x2, y1, x2,y2), fill=(255,0,0), width=2) draw.line((x1, y2, x2,y2), fill=(255,0,0), width=2) images.append(image) return images