本文整理汇总了Python中ImageDraw.Draw方法的典型用法代码示例。如果您正苦于以下问题:Python ImageDraw.Draw方法的具体用法?Python ImageDraw.Draw怎么用?Python ImageDraw.Draw使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ImageDraw
的用法示例。
在下文中一共展示了ImageDraw.Draw方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: new_image
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def new_image(self, **kwargs):
back_color = kwargs.get("back_color", "white")
fill_color = kwargs.get("fill_color", "black")
if fill_color.lower() != "black" or back_color.lower() != "white":
if back_color.lower() == "transparent":
mode = "RGBA"
back_color = None
else:
mode = "RGB"
else:
mode = "1"
# L mode (1 mode) color = (r*299 + g*587 + b*114)//1000
if fill_color.lower() == "black": fill_color = 0
if back_color.lower() == "white": back_color = 255
img = Image.new(mode, (self.pixel_size, self.pixel_size), back_color)
self.fill_color = fill_color
self._idr = ImageDraw.Draw(img)
return img
示例2: render
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def render(self, img):
font = ImageFont.truetype(*self.font)
textSize = font.getsize(self.text)
draw = ImageDraw.Draw(img)
# Find the text's origin given our alignment and current image size
x = int((img.size[0] - textSize[0] - self.borderSize*2) * self.alignment[0] + 0.5)
y = int((img.size[1] - textSize[1] - self.borderSize*2) * self.alignment[1] + 0.5)
# Draw the border if we need one. This is slow and ugly, but there doesn't
# seem to be a better way with PIL.
if self.borderSize > 0:
for bx in (-1,0,1):
for by in (-1,0,1):
if bx and by:
draw.text((x + bx * self.borderSize,
y + by * self.borderSize),
self.text, font=font, fill=self.borderColor)
# And the text itself...
draw.text((x,y), self.text, font=font, fill=self.textColor)
### The End ###
示例3: show
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def show(self, img_x=1350, img_y=20, dry_run=False):
"""
show image representing the interval
"""
try:
import Image
import ImageDraw
except ImportError:
print('cannot import python PIL imaging')
return
img = Image.new('RGB', (img_x, img_y), (100, 100, 100))
draw = ImageDraw.Draw(img)
i_min, i_max = self.hull()
print(hex(i_min), hex(i_max))
addr2x = lambda addr: ((addr - i_min) * img_x) // (i_max - i_min)
for a, b in self.intervals:
draw.rectangle((addr2x(a), 0, addr2x(b), img_y), (200, 0, 0))
if dry_run is False:
img.show()
示例4: build_sentance
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def build_sentance(sentance, word): #Build sentance.
while True:
if '$word' in sentance:
break
else:
print '$word not found, try again. Example: \'The $word crossed the road.\''
index = sentance.index('$word')
startSentance = sentance[:index]
index = index + 5 #remove '$word'
endSentance = sentance[index:] + '. .'
builtSentance = startSentance + word + endSentance
return builtSentance
#Draw word on blank canvas.
#Calculate length in pixels from colour coordinates.
#If word fits, add to Array=<successList>.
示例5: encode
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def encode(font, c):
im = Image.new("L", (128, 160))
dr = ImageDraw.Draw(im)
dr.text((10,40), c, font=font1, fill=255)
# print im.getbbox()
im = im.crop((10, 44, 16, 51))
(w,h) = im.size
nyb = (np.array(im).flatten() * 10 / 255).tolist()
return nyb
print c, "".join(["%x" % x for x in nyb])
saved = 0
for i in range(len(nyb)):
if nyb[i:i+3] == [0,0,0]:
saved += 1
# print c, "saved", saved
return len(nyb) - saved
示例6: main
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def main():
epd = epd7in5b.EPD()
epd.init()
# For simplicity, the arguments are explicit numerical coordinates
# image_red = Image.new('1', (EPD_WIDTH, EPD_HEIGHT), 255) # 255: clear the frame
# draw_red = ImageDraw.Draw(image_red)
# image_black = Image.new('1', (EPD_WIDTH, EPD_HEIGHT), 255) # 255: clear the frame
# draw_black = ImageDraw.Draw(image_black)
# font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 24)
# draw_red.rectangle((0, 6, 640, 40), fill = 0)
# draw_red.text((200, 10), 'e-Paper demo', font = font, fill = 255)
# draw_red.rectangle((200, 80, 600, 280), fill = 0)
# draw_red.chord((240, 120, 580, 220), 0, 360, fill = 255)
# draw_black.rectangle((20, 80, 160, 280), fill = 0)
# draw_red.chord((40, 80, 180, 220), 0, 360, fill = 0)
# epd.display_frame(epd.get_frame_buffer(image_black),epd.get_frame_buffer(image_red))
# display images
frame_black = epd.get_frame_buffer(Image.open('black.png'))
frame_red = epd.get_frame_buffer(Image.open('white.png'))
epd.display_frame(frame_black, frame_red)
示例7: legend
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def legend(self, heights):
template = os.path.join(self.inpath, "density.png")
legend_img = os.path.join(self.target,"pop_density_legend.png")
font_file = "./pilfonts/helvB14.pil"
font = ImageFont.load(font_file)
im = Image.open(template)
draw = ImageDraw.Draw(im)
text = ""
X = 75
Y = 90
for h in heights:
text = "%.1f\n" % (h)
draw.text((X, Y), text, (0,0,0), font=font)
Y += 47
print "saved legend to ", legend_img
im.save(legend_img)
示例8: new_image
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def new_image(self, **kwargs):
back_color = kwargs.get("fill_color", "white")
fill_color = kwargs.get("back_color", "black")
if fill_color.lower() != "black" or back_color.lower() != "white":
if back_color.lower() == "transparent":
mode = "RGBA"
back_color = None
else:
mode = "RGB"
else:
mode = "1"
img = Image.new(mode, (self.pixel_size, self.pixel_size), back_color)
self.fill_color = fill_color
self._idr = ImageDraw.Draw(img)
return img
示例9: banner
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def banner():
ShowText = 'For Educational Purpose Only'
font = ImageFont.truetype('arialbd.ttf', 15) #load the font
size = font.getsize(ShowText) #calc the size of text in pixels
image = Image.new('1', size, 1) #create a b/w image
draw = ImageDraw.Draw(image)
draw.text((0, 0), ShowText, font=font) #render the text to the bitmap
for rownum in range(size[1]):
line = []
for colnum in range(size[0]):
if image.getpixel((colnum, rownum)): line.append(' '),
else: line.append('#'),
print (''.join(line))
print ("Access the site https://databases.today to download the available leaks.\n")
示例10: literally_show
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def literally_show(self, airport_code):
display = Matrix16x8.Matrix16x8()
display.begin()
display.set_brightness(4)
font = ImageFont.truetype(os.path.join(os.path.dirname(__file__), 'thintel/Thintel.ttf'), 15)
if len(airport_code) == 4:
image = Image.new('1', (21, 8))
draw = ImageDraw.Draw(image)
blankimage = Image.new('1', (16, 8))
blankdraw = ImageDraw.Draw(blankimage)
blankdraw.text((0, 0), '', fill=255)
for i in xrange(58):
n = 5 - abs((i % 12) - 5)
draw.text((0, 0), airport_code, font=font, fill=255)
display.set_image(blankimage)
display.write_display()
display.set_image(image.crop((n, 0, n + 16, 8)))
display.write_display()
sleep( 0.5 if i > 0 else 3)
elif len(airport_code) == 3 or len(airport_code) == 0:
image = Image.new('1', (16, 8))
draw = ImageDraw.Draw(image)
draw.text((0, 0), airport_code, font=font, fill=255)
display.set_image(image)
display.write_display()
示例11: _draw_rectangle
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def _draw_rectangle(self, img, xy):
"""Draw a black rectangle.
@param img: PIL Image object
@param xy: Coordinates as refined in PIL rectangle() doc
@return: Image with black rectangle
"""
dr = ImageDraw.Draw(img)
dr.rectangle(xy, fill="black", outline="black")
return img
示例12: equal
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def equal(self, img1, img2, skip_area=None):
"""Compares two screenshots using Root-Mean-Square Difference (RMS).
@param img1: screenshot to compare.
@param img2: screenshot to compare.
@return: equal status.
"""
if not HAVE_PIL:
return None
# Trick to avoid getting a lot of screen shots only because the time in the windows
# clock is changed.
# We draw a black rectangle on the coordinates where the clock is locates, and then
# run the comparison.
# NOTE: the coordinates are changing with VM screen resolution.
if skip_area:
# Copying objects to draw in another object.
img1 = img1.copy()
img2 = img2.copy()
# Draw a rectangle to cover windows clock.
for img in (img1, img2):
self._draw_rectangle(img, skip_area)
# To get a measure of how similar two images are, we use
# root-mean-square (RMS). If the images are exactly identical,
# this value is zero.
diff = ImageChops.difference(img1, img2)
h = diff.histogram()
sq = (value * ((idx % 256)**2) for idx, value in enumerate(h))
sum_of_squares = sum(sq)
rms = math.sqrt(sum_of_squares/float(img1.size[0] * img1.size[1]))
# Might need to tweak the threshold.
return rms < 8
示例13: __init__
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def __init__(self, image, size=None, color=None):
if not hasattr(image, "im"):
image = Image.new(image, size, color)
self.draw = ImageDraw.Draw(image)
self.image = image
self.transform = None
示例14: render_action_to_png
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def render_action_to_png(step, action):
import Image, ImageDraw
img = Image.new('RGB', (50, 50), (50, 50, 50))
canvas = ImageDraw.Draw(img)
lx, ly = int(25+(action[0][0]*25)), int(25+(action[0][1]*25))
canvas.line((25,25, lx,ly), fill="black")
img.save("/tmp/action_%03d.png" % step)
示例15: _init
# 需要导入模块: import ImageDraw [as 别名]
# 或者: from ImageDraw import Draw [as 别名]
def _init(self, code):
size = self.calculate_size(len(code[0]), len(code), self.dpi)
self._image = Image.new('RGB', size, self.background)
self._draw = ImageDraw.Draw(self._image)