在黑色图像上绘制斑点

时间:2017-12-21 10:59:01

标签: python python-imaging-library

我写了一个示例代码,在黑色图像上绘制白点。我能够一次绘制一个点。我想给出一组点作为输入,并在黑色图像上绘制白色图像点。任何人都可以建议我如何进行?

from PIL import Image,ImageDraw
import time
class ImageHandler(object):
"""
with the aspect of no distortion and looking from the same view
"""

    reso_width = 0
    reso_height = 0
    radius = 10
    def __init__(self,width,height,spotlight_radius= 10):
        self.reso_width = width
        self.reso_height = height
        self.radius = spotlight_radius

    def get_image_spotlight(self,set_points): #function for drawing spot light
        image,draw = self.get_black_image()
        for (x,y) in set_points:
            draw.ellipse((x-self.radius,y-self.radius,x+self.radius,y+self.radius),fill = 'white')
        image.show("titel")
        return image

    def get_black_image(self):   #function for drawing black image
        image = Image.new('RGBA',(self.reso_width,self.reso_height),"black")#(ImageHandler.reso_width,ImageHandler.reso_height),"black")
        draw = ImageDraw.Draw((image))
        return image,draw


hi = ImageHandler(1000,1000)
a = []
hi.get_image_spotlight((a))
for i in range(0,100):
    a = [(500,500)]
    hi.get_image_spotlight((a))
    time.sleep(1000)

1 个答案:

答案 0 :(得分:1)

ImageHandler类中的代码看起来像是符合您的要求。目前它正在传递一个包含单个点的列表。同样的一点是在黑色图像上绘制一次,所以你只能看到一个点,它总是在同一个位置。

而是将包含多个点的列表传递给get_image_spotlight()。您可以生成一个随机的点列表,然后绘制它们:

from random import randrange

spot_count = 10
points = [(randrange(1000), randrange(1000)) for _ in range(spot_count)]
img = hi.get_image_spotlight(points)

这将在黑色背景上创建一个包含10个白点的图像。更改spot_count更多或更少的地点。

相关问题