本文整理汇总了Python中PIL.ImageDraw类的典型用法代码示例。如果您正苦于以下问题:Python ImageDraw类的具体用法?Python ImageDraw怎么用?Python ImageDraw使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ImageDraw类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: draw
def draw(self, image_draw: ImageDraw):
image_draw.line((self._coordinates.x - self._diameter, self._coordinates.y - self._diameter,
self._coordinates.x + self._diameter, self._coordinates.y + self._diameter),
fill=self._color, width=self._linewidth)
image_draw.line((self._coordinates.x + self._diameter, self._coordinates.y - self._diameter,
self._coordinates.x - self._diameter, self._coordinates.y + self._diameter),
fill=self._color, width=self._linewidth)
示例2: fill
def fill(self,seedPoint,fillValue=1 ):
"fills an area starting with seed"
i = self.index(seedPoint);
v = self.p[:];
x = Image.fromarray(v.T);
ImageDraw.Draw(x);
ImageDraw.floodfill(x,(i[0],i[1]),fillValue);
v[:] = np.array(x).T;
示例3: createMask
def createMask(imageIn, threshold=10, fillHoles=True, backgroundColor=255, blurRadius=0.0,
maskScale=1.0):
"""
Given an image, create a mask by locating the pixels that are not the backgroundColor
(within a threshold).
@param threshold How far away from the backgroundColor a pixel must be to be included
in the mask
@param fillHoles If true, the inside of the mask will be filled in. This is useful if
the inside of objects might contain the background color
@param backgroundColor the background color.
@param blurRadius If set to some fraction > 0.0, then the edges of the mask will be blurred
using a blur radius which is this fraction of the image size.
@param maskScale If set to < 1.0, then the effective size of the object (the area where
the mask includes the object) will be scaled down by this
amount. This can be useful when the outside of the object contains
some noise that you want to trim out and not include in the mask.
@retval the mask as a PIL 'L' image, where 255 is areas that include the object, and 0
are areas that are background. If blurRadius is > 0, then it will
also contain values between 0 and 255 which act as compositing values.
"""
image = imageIn.convert('L')
bwImage = image.point(lambda x: (abs(x - backgroundColor) > threshold) * 255)
if not fillHoles:
mask = bwImage
else:
bwImage = ImageOps.expand(bwImage, 1, fill=0)
maskColor = 128
ImageDraw.floodfill(bwImage, (0, 0), maskColor)
mask = bwImage.point(lambda x: (x != maskColor) * 255)
mask = ImageOps.crop(mask, 1)
# Are we reducing the object size?
if maskScale < 1.0:
newSize = [int(x * maskScale) for x in mask.size]
reducedMask = mask.resize(newSize, Image.ANTIALIAS)
sizeDiff = numpy.array(mask.size) - numpy.array(newSize)
pos = [int(x / 2) for x in sizeDiff]
mask = ImageChops.constant(mask, 0)
mask.paste(reducedMask, tuple(pos))
# Blur the mask
if blurRadius > 0.0:
radius = int(round(blurRadius * (mask.size[0] + mask.size[1]) / 2))
if radius > 1:
mask = blur(mask, radius=radius, edgeColor=0)
else:
import pdb; pdb.set_trace()
return mask
示例4: fillImageFaster
def fillImageFaster(self, begin, paint, current, image):
buffer = QBuffer()
buffer.open(QBuffer.ReadWrite)
image.save(buffer, "PNG")
pil_im = Image.open(io.BytesIO(buffer.data()))
ImageDraw.floodfill(pil_im, begin, (paint.red(), paint.green(), paint.blue()))
self.image().image = QtGui.QImage(pil_im.convert("RGB").tobytes("raw", "RGB"), pil_im.size[0], pil_im.size[1], QtGui.QImage.Format_RGB888)
self.update()
示例5: test_floodfill
def test_floodfill():
# Arrange
im = Image.new("RGB", (w, h))
draw = ImageDraw.Draw(im)
draw.rectangle(bbox2, outline="yellow", fill="green")
centre_point = (int(w/2), int(h/2))
# Act
ImageDraw.floodfill(im, centre_point, ImageColor.getrgb("red"))
del draw
# Assert
assert_image_equal(im, Image.open("Tests/images/imagedraw_floodfill.png"))
示例6: __init__
def __init__(self, simplemap):
self.image = Image.new("RGB", simplemap.image.size)
self.simple = simplemap
ImageDraw.floodfill(self.image, (0, 0), (255, 255, 255))
self.territories = set()
draw = ImageDraw.Draw(self.image)
self.territory_colours = territory_colours = simplemap.get_territories()
self.inv_territory_colours = inv_territory_colours = dict([(v, k) for (k, v) in territory_colours.items()])
for fillpass in range(3):
for y in xrange(self.image.size[1]):
for x in xrange(self.image.size[0]):
colour = simplemap.image.getpixel((x, y))
if fillpass == 1 and colour in territory_colours.values():
tid = inv_territory_colours[colour] * 100
n_x, n_y = x, y
neighbours = [(x + 1, y), (x, y + 1), (x - 1, y), (x, y - 1)]
neighbours = [(x if x > 0 else self.image.size[0] - 1, y) for (x, y) in neighbours]
neighbours = [(x if x < self.image.size[0] else 0, y) for (x, y) in neighbours]
neighbours = [(x, y if y > 0 else self.image.size[1] - 1) for (x, y) in neighbours]
neighbours = [(x, y if y < self.image.size[1] else 0) for (x, y) in neighbours]
neighbours = set(self.image.getpixel(neighbour) for neighbour in neighbours)
neighbours = set(
colour
for colour in neighbours
if colour[2] < 255 and colour != (0, 0, 0) and colour != (255, 0, 0)
)
if neighbours:
colour = max(neighbours)
tid = colour_to_territory_id(colour)
else:
tid = inv_territory_colours[colour] * 100
# generate a new tid
tid += 1
while tid in self.territories:
tid += 1
self.territories.add(tid)
colour = territory_id_to_colour(tid)
x, y = n_x, n_y
ImageDraw.floodfill(self.image, (x, y), colour)
elif colour == (255, 255, 255):
if x < self.image.size[0] - 1:
next_pixel = simplemap.image.getpixel((x + 1, y))
if fillpass == 2 and (next_pixel in territory_colours.values()):
# We're not in the sea
colour = self.image.getpixel((x + 1, y))[:2] + (255,)
draw.point((x, y), tuple(colour))
continue
draw.point((x, y), colour)
elif colour in set([(0, 0, 0), (255, 0, 0)]):
draw.point((x, y), colour)
示例7: test_floodfill
def test_floodfill(self):
red = ImageColor.getrgb("red")
for mode, value in [
("L", 1),
("RGBA", (255, 0, 0, 0)),
("RGB", red)
]:
# Arrange
im = Image.new(mode, (W, H))
draw = ImageDraw.Draw(im)
draw.rectangle(BBOX2, outline="yellow", fill="green")
centre_point = (int(W/2), int(H/2))
# Act
ImageDraw.floodfill(im, centre_point, value)
# Assert
expected = "Tests/images/imagedraw_floodfill_"+mode+".png"
im_floodfill = Image.open(expected)
self.assert_image_equal(im, im_floodfill)
# Test that using the same colour does not change the image
ImageDraw.floodfill(im, centre_point, red)
self.assert_image_equal(im, im_floodfill)
# Test that filling outside the image does not change the image
ImageDraw.floodfill(im, (W, H), red)
self.assert_image_equal(im, im_floodfill)
# Test filling at the edge of an image
im = Image.new("RGB", (1, 1))
ImageDraw.floodfill(im, (0, 0), red)
self.assert_image_equal(im, Image.new("RGB", (1, 1), red))
示例8: DrawBalls
def DrawBalls(self, differentialMethod, step):
# First, track the border for all balls and store
# it to pos0 and edgePos. The latter will move along the border,
# pos0 stays at the initial coordinates.
for ball in self.balls:
ball.pos0 = self.trackTheBorder(ball.pos + 1j)
if ball.pos0 == None:
ball.tracking = False
else:
ball.edgePos = ball.pos0
ball.tracking = True
# print "Done with tracking"
loopIndex = 0
while loopIndex < 200:
loopIndex += 1
for ball in self.balls:
if not ball.tracking:
continue
# store the old coordinates
old_pos = ball.edgePos
# walk along the tangent, using chosen differential method
ball.edgePos = differentialMethod(ball.edgePos, step, self.calcTangent)
# correction step towards the border
ball.edgePos, tmp = self.stepOnceTowardsBorder(ball.edgePos)
draw = ImageDraw.Draw(self.image)
draw.line((old_pos.real, old_pos.imag, ball.edgePos.real, ball.edgePos.imag), fill=self.color)
del draw
# check if we've gone a full circle or hit some other
# edge tracker
for ob in self.balls:
if ob.tracking:
if (ob is not ball) and abs(ob.pos0 - ball.edgePos) < step: # or loopIndex > 3
ball.tracking = False
tracking = 0
for ball in self.balls:
if ball.tracking:
tracking += 1
if tracking == 0:
break
for ball in self.balls:
if ball.tracking:
ball.pos = complex(round(ball.pos.real), round(ball.pos.imag))
ImageDraw.floodfill(self.image, (ball.pos.real, ball.pos.imag), self.color) # , self.color)
示例9: test_floodfill_thresh
def test_floodfill_thresh(self):
# floodfill() is experimental
# Arrange
im = Image.new("RGB", (W, H))
draw = ImageDraw.Draw(im)
draw.rectangle(BBOX2, outline="darkgreen", fill="green")
centre_point = (int(W/2), int(H/2))
# Act
ImageDraw.floodfill(
im, centre_point, ImageColor.getrgb("red"),
thresh=30)
# Assert
self.assert_image_equal(
im, Image.open("Tests/images/imagedraw_floodfill2.png"))
示例10: PrintText
def PrintText(self, text):
# led.draw_text2(x-axis, y-axis, whatyouwanttoprint, size) < Understand?
# So led.drawtext2() prints simple text to the OLED display like so:
#text = 'Hello!'
# Create the image to write to the display
# THIS MAY NEED TO CHANGE BASED ON STRING LENGTH!
image = Image.new('1', (128, 64))
draw = ImageDraw(image)
draw.text((0, 0), text, font=ImageFont.load_default(), fill=255)
# Clear the Display
self.led.clear()
self.led.display()
# Write the text-based image to the display
self.led.image(image)
self.led.display()
示例11: drawterritory
def drawterritory(t, color=None):
"""Draw an entire territory (will draw in color provided, default is owning player's color)"""
terr = territories[str(riskboard.territories[t].name)]
#Create colored version of the image
canvas.delete(terr.name)
if len(backcolors) > 0 and current_state.owners[t] is not None:
for fp in terr.floodpoints:
if color:
ImageDraw.floodfill(terr.photo, fp, color)
else:
ImageDraw.floodfill(terr.photo, fp, hex_to_rgb(backcolors[current_state.owners[t]]))
terr.currentimage = ImageTk.PhotoImage(terr.photo)
canvas.create_image(terr.x, terr.y, anchor=Tkinter.NW,
image=terr.currentimage, tags=(terr.name,))
drawarmy(t, 1)
示例12: floodFill
def floodFill(canvas, origImage, edgeImage, color, filledImage=None):
(width, height) = origImage.size
edgePixels = edgeImage.load()
fillRegionCoords = []
temporaryFill = (100,100,100)
for x in xrange(width):
for y in xrange(height):
if (edgePixels[x, y] == color):
fillRegionCoords += [(x,y)]
ImageDraw.floodfill(edgeImage, (x,y), temporaryFill)
#fill temporarily to make sure fillRegionCoords does not have
#multiple coordinates that would fill the same region
if (filledImage == None):
filledImage = Image.open(canvas.data.edgeImageFile)
for (x,y) in fillRegionCoords:
fillColor = regionColor(origImage, filledImage, (x,y))
ImageDraw.floodfill(filledImage, (x,y), fillColor)
return filledImage
示例13: test_floodfill_border
def test_floodfill_border(self):
# floodfill() is experimental
# Arrange
im = Image.new("RGB", (w, h))
draw = ImageDraw.Draw(im)
draw.rectangle(bbox2, outline="yellow", fill="green")
centre_point = (int(w/2), int(h/2))
# Act
ImageDraw.floodfill(
im, centre_point, ImageColor.getrgb("red"),
border=ImageColor.getrgb("black"))
del draw
# Assert
self.assert_image_equal(
im, Image.open("Tests/images/imagedraw_floodfill2.png"))
示例14: test_floodfill_border
def test_floodfill_border():
# floodfill() is experimental
if hasattr(sys, 'pypy_version_info'):
# Causes fatal RPython error on PyPy
skip()
# Arrange
im = Image.new("RGB", (w, h))
draw = ImageDraw.Draw(im)
draw.rectangle(bbox2, outline="yellow", fill="green")
centre_point = (int(w/2), int(h/2))
# Act
ImageDraw.floodfill(
im, centre_point, ImageColor.getrgb("red"),
border=ImageColor.getrgb("black"))
del draw
# Assert
assert_image_equal(im, Image.open("Tests/images/imagedraw_floodfill2.png"))
示例15: imfill
def imfill(arr, edge):
"""Fill holes in images.
NOTE: dtype of input array will be temporarily converted uint8!
This is because PIL's fromarray function works only with numpy
arrays of data type 'uint8'. This may cause some data losses, so
proceed with caution!
Input:
arr -- a numpy.array to be floodfilled
edge -- a value of edges
"""
# using arr.astype to preserve array's dtype, as fromarray requires
# array whose dtype is uint8
img = Image.fromarray(arr.astype('uint8')) # read-only
aimg = img.copy()
ImageDraw.floodfill(aimg, (0,0), edge, edge)
invimg = ImageChops.invert(aimg)
invarr = asarray(invimg)
arr[invarr==255] = edge
return arr