本文整理匯總了Python中PIL.ImageDraw.Draw方法的典型用法代碼示例。如果您正苦於以下問題:Python ImageDraw.Draw方法的具體用法?Python ImageDraw.Draw怎麽用?Python ImageDraw.Draw使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類PIL.ImageDraw
的用法示例。
在下文中一共展示了ImageDraw.Draw方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: displayImageFileOnLCD
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def displayImageFileOnLCD(filename):
print 'displays ', filename
title = 'Review Mode'
# resize/dither to screen resolution and send to LCD
image = Image.open(filename)
im_width, im_height = image.size
if im_width < im_height:
image = image.rotate(90)
image.thumbnail(S_SIZE, Image.ANTIALIAS)
image_sized = Image.new('RGB', S_SIZE, (0, 0, 0))
image_sized.paste(image,((S_SIZE[0] - image.size[0]) / 2, (S_SIZE[1] - image.size[1]) / 2))
# draw filename
draw = ImageDraw.Draw(image_sized)
font = ImageFont.truetype('arial.ttf', 18)
draw.rectangle([(0, 0), (115, 22)], fill=(255,255,255), outline=(0,0,0))
draw.text((2, 2), title, fill='black', font=font)
draw.rectangle([(279, 217), (399, 239)], fill=(255,255,255), outline=(0,0,0))
draw.text((290, 218), filename, fill='black', font=font)
# display on LCD
image_sized = ImageOps.invert(image_sized)
image_sized = image_sized.convert('1') # convert image to black and white
lcd.write(image_sized.tobytes())
示例2: create_image_from_text
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def create_image_from_text(in_text):
colours = (255, 255, 250)
font_file = '/usr/share/fonts/truetype/freefont/FreeSansBold.ttf'
font_size = 12
font = ImageFont.truetype(font_file, font_size)
w, h = font.getsize(in_text)
text_x, text_y = width, 0
text_width, text_height = width, 0
text_width += w + width # add some padding so the ip scrolls off the unicorn hat
text_height = max(text_height, h, 16) # no more than the size of the unicorn hat
image = Image.new('RGB', (text_width, text_height), (0, 0, 0))
draw = ImageDraw.Draw(image)
draw.text((text_x, text_y), in_text, colours, font=font)
return (image, text_width)
# DISPLAY
示例3: _draw_single_box
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def _draw_single_box(image, xmin, ymin, xmax, ymax, display_str, font, color='black', thickness=4):
draw = ImageDraw.Draw(image)
(left, right, top, bottom) = (xmin, xmax, ymin, ymax)
draw.line([(left, top), (left, bottom), (right, bottom),
(right, top), (left, top)], width=thickness, fill=color)
text_bottom = bottom
# Reverse list and print from bottom to top.
text_width, text_height = font.getsize(display_str)
margin = np.ceil(0.05 * text_height)
draw.rectangle(
[(left, text_bottom - text_height - 2 * margin), (left + text_width,
text_bottom)],
fill=color)
draw.text(
(left + margin, text_bottom - text_height - margin),
display_str,
fill='black',
font=font)
return image
開發者ID:Sunarker,項目名稱:Collaborative-Learning-for-Weakly-Supervised-Object-Detection,代碼行數:22,代碼來源:visualization.py
示例4: render_frame
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def render_frame(image, y, frame):
w, h = image.size
fw, fh = frame.size
x = (fw - w) // 2
draw = Draw(frame)
# Draw shadow and image.
off = ARGS.shadow_size
draw.rectangle(
[x + off, y + off, x + off + w, y + off + h], ARGS.rgb_shadow)
frame.paste(image, (x, y))
draw.rectangle([x, y, x + w, y + h], outline=ARGS.rgb_outline)
# Draw a frame border.
draw.rectangle([0, 0, fw - 1, fh - 1], outline=ARGS.rgb_window)
示例5: draw_text
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def draw_text(
img: Image,
text: str,
location: tuple = (0, 0),
text_color=(0, 0, 0)
) -> Image:
draw = ImageDraw.Draw(img)
try:
# For Linux
font = ImageFont.truetype("DejaVuSans.ttf", 20)
except Exception:
logger.warning("No font DejaVuSans; use default instead")
# For others
font = ImageFont.load_default()
draw.text(location, text, font=font, fill=text_color)
return img
示例6: draw
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def draw(self, image):
fontpath = FONTPATH[ random.sample(range(2),1)[0] ]
color = (self.color[0], self.color[1], self.color[2], 255)
font = ImageFont.truetype( fontpath , randint(25, 27) * 10)
text = Image.new("RGBA", (250, 300), (0, 0, 0, 0))
textdraw = ImageDraw.Draw(text)
textdraw.text((0, 0), str(self.number), font=font, fill=color)
#textdraw.text((0, 0), 'j', font=font, fill=color)
text = text.rotate(self.angle, expand=True)
text = text.resize((int(text.size[0] / 10), int(text.size[1] / 10)))
base = int(self.priority * (200 / 6))
rand_min = (self.offset - base - 2) if (self.offset - base - 2) >= -15 else -15
rand_min = 0 if self.priority == 0 else rand_min
rand_max = (33 - text.size[0]) if self.priority == 5 else (33 - text.size[0] + 10)
try:
displace = randint(rand_min, rand_max)
except:
displace = rand_max
location = (base + displace, randint(3, 23))
self.next_offset = location[0] + text.size[0]
image.paste(text, location, text)
# plt.imshow(image)
示例7: make_photo
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def make_photo(self, dir_):
"""生成驗證碼
:param dir_: 存放驗證碼照片的文件夾
"""
from PIL import Image # 安裝pillow: pip install pillow
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageFilter
image = Image.new('RGB', (self.width, self.height), (255, 255, 255))
font = ImageFont.truetype('arial.ttf', 36)
draw = ImageDraw.Draw(image)
for w in range(self.width):
for h in range(self.height):
draw.point((w, h), fill=self.color1())
for index, t in enumerate(self.flag):
draw.text(((self.width - 10) // self.number * index + 10, 10), t, font=font, fill=self.color2())
image = image.filter(ImageFilter.BLUR)
image.save(dir_ + sep + ''.join(self.flag) + '.jpg', 'jpeg')
return image
示例8: drawSlants
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def drawSlants(side):
randcolor = lambda : (randint(0,255),randint(0,255),randint(0,255))
img = Image.new("RGB", (side,side), "#FFFFFF")
draw = ImageDraw.Draw(img)
y = 0
min_w = int(side * 0.01)
max_w = int(side * 0.1)
adj = max_w * 2
while y <= side+adj:
w = randint(min_w, max_w)
c = randcolor()
draw.line([-adj , y, y, -adj], width=w, fill=c)
draw.line([y, side+adj, side+adj, y], width=w, fill=c)
y+=w
return img
#################
# TRIANGULATION #
#################
示例9: nGradient
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def nGradient(side, *colors):
img = Image.new("RGB", (side,side), "#FFFFFF")
draw = ImageDraw.Draw(img)
nc = len(colors)
div = side//(nc-1)
[r,g,b] = colors[0]
p=0
for i in range(1,nc):
dc = [(y-x)/div for x,y in zip(colors[i-1], colors[i])]
for x in range(p, p+div):
draw.line([x,0,x,side], fill=tuple(map(int, [r,g,b])))
r+=dc[0]
g+=dc[1]
b+=dc[2]
p+=div
return img
示例10: check_OP_1_Connection
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def check_OP_1_Connection():
connected = Image.new('1', (128, 64))
draw = ImageDraw.Draw(connected)
draw.text((0, 25), "Connecting.....", font=getFont(), fill='white')
displayImage(connected)
# if is_connected():
if do_mount("OP1"):
connected = Image.new('1', (128, 64))
draw = ImageDraw.Draw(connected)
draw.text((0, 25), "Connected", font=getFont(), fill='white')
displayImage(connected)
return True
else:
connected = Image.new('1', (128, 64))
draw = ImageDraw.Draw(connected)
draw.text((0, 25), "No Connection!", font=getFont(), fill='white')
displayImage(connected)
config["USB_Mount_Path"] = ""
config["OP_1_Mounted_Dir"] = ""
time.sleep(1)
return False
示例11: unmount_OP_Z
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def unmount_OP_Z():
if getMountPath("OPZ") != "":
unmountDisplay = Image.new('1', (128, 64))
draw = ImageDraw.Draw(unmountDisplay)
draw.text((30, 25), "Ejecting!", font=getFont(), fill='white')
displayImage(unmountDisplay)
unmountdevice(config["OP_Z_Mounted_Dir"])
config["USB_Mount_Path"] = ""
unmountDisplay = Image.new('1', (128, 64))
draw = ImageDraw.Draw(unmountDisplay)
draw.text((30, 25), "Ejected!", font=getFont(), fill='white')
displayImage(unmountDisplay)
time.sleep(1)
return True
else:
unmountDisplay = Image.new('1', (128, 64))
draw = ImageDraw.Draw(unmountDisplay)
draw.text((15, 25), "No Device to Eject", font=getFont(), fill='white')
displayImage(unmountDisplay)
time.sleep(1)
return False
示例12: debug_img
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def debug_img(img, bitmap, logistic_output):
# create a debug image with three columns; 1) original RGB. 2) black/white
# bitmap of labels 3) black/white bitmap of predictions (with centroids coloured
# red.
h, w, _channels = bitmap.shape
canvas = Image.new('RGB', (w*3, h), (50, 50, 50))
# original input image on left
img = zero_centered_array_to_pil_image(img)
img = img.resize((w, h))
canvas.paste(img, (0, 0))
# label bitmap in center
canvas.paste(bitmap_to_pil_image(bitmap), (w, 0))
# logistic output on right
canvas.paste(bitmap_to_pil_image(logistic_output), (w*2, 0))
# draw red dots on right hand side image corresponding to
# final thresholded prediction
draw = ImageDraw.Draw(canvas)
for y, x in centroids_of_connected_components(logistic_output):
draw.rectangle((w*2+x,y,w*2+x,y), fill='red')
# finally draw blue lines between the three to delimit boundaries
draw.line([w,0,w,h], fill='blue')
draw.line([2*w,0,2*w,h], fill='blue')
draw.line([3*w,0,3*w,h], fill='blue')
# done
return canvas
示例13: side_by_side
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def side_by_side(rgb, bitmap):
h, w, _ = rgb.shape
canvas = Image.new('RGB', (w*2, h), (50, 50, 50))
# paste RGB on left hand side
lhs = zero_centered_array_to_pil_image(rgb)
canvas.paste(lhs, (0, 0))
# paste bitmap version of labels on right hand side
# black with white dots at labels
rhs = bitmap_to_pil_image(bitmap)
rhs = rhs.resize((w, h))
canvas.paste(rhs, (w, 0))
# draw on a blue border (and blue middle divider) to make it
# easier to see relative positions.
draw = ImageDraw.Draw(canvas)
draw.polygon([0,0,w*2-1,0,w*2-1,h-1,0,h-1], outline='blue')
draw.line([w,0,w,h], fill='blue')
canvas = canvas.resize((w, h//2))
return canvas
示例14: drawCaption
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def drawCaption(img, caption):
img_txt = Image.fromarray(img)
# get a font
fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 50)
# get a drawing context
d = ImageDraw.Draw(img_txt)
# draw text, half opacity
d.text((10, 256), 'Stage-I', font=fnt, fill=(255, 255, 255, 255))
d.text((10, 512), 'Stage-II', font=fnt, fill=(255, 255, 255, 255))
if img.shape[0] > 832:
d.text((10, 832), 'Stage-I', font=fnt, fill=(255, 255, 255, 255))
d.text((10, 1088), 'Stage-II', font=fnt, fill=(255, 255, 255, 255))
idx = caption.find(' ', 60)
if idx == -1:
d.text((256, 10), caption, font=fnt, fill=(255, 255, 255, 255))
else:
cap1 = caption[:idx]
cap2 = caption[idx+1:]
d.text((256, 10), cap1, font=fnt, fill=(255, 255, 255, 255))
d.text((256, 60), cap2, font=fnt, fill=(255, 255, 255, 255))
return img_txt
示例15: drawCaption
# 需要導入模塊: from PIL import ImageDraw [as 別名]
# 或者: from PIL.ImageDraw import Draw [as 別名]
def drawCaption(self, img, caption):
img_txt = Image.fromarray(img)
# get a font
fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 50)
# get a drawing context
d = ImageDraw.Draw(img_txt)
# draw text, half opacity
d.text((10, 256), 'Stage-I', font=fnt, fill=(255, 255, 255, 255))
d.text((10, 512), 'Stage-II', font=fnt, fill=(255, 255, 255, 255))
if img.shape[0] > 832:
d.text((10, 832), 'Stage-I', font=fnt, fill=(255, 255, 255, 255))
d.text((10, 1088), 'Stage-II', font=fnt, fill=(255, 255, 255, 255))
idx = caption.find(' ', 60)
if idx == -1:
d.text((256, 10), caption, font=fnt, fill=(255, 255, 255, 255))
else:
cap1 = caption[:idx]
cap2 = caption[idx+1:]
d.text((256, 10), cap1, font=fnt, fill=(255, 255, 255, 255))
d.text((256, 60), cap2, font=fnt, fill=(255, 255, 255, 255))
return img_txt