简体   繁体   English

在黑色图像上绘制斑点

[英]Drawing spots on black image

I wrote a sample code to draw white spots on a black image. 我编写了一个示例代码在黑色图像上绘制白色斑点。 I was able to draw single spot at a time. 我一次可以画一个点。 I would like to give a set of points as input and draw white image spots on the black image. 我想提供一组点作为输入,并在黑色图像上绘制白色图像点。 Can anyone suggest me on how to proceed? 有人可以建议我如何进行吗?

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)

Your code in the ImageHandler class looks like it does what you require. 您在ImageHandler类中的代码看起来像它所需要的。 Presently it is being passed a list containing a single point. 目前正在传递包含单个点的列表。 This same point is being drawn on the black image once, so you'll only ever see one spot, and it will always be in the same position. 同一点在黑色图像上被绘制一次,因此您只会看到一个点,并且始终处于同一位置。

Instead pass a list containing more than one point to get_image_spotlight() . 而是将包含多个点的列表传递给get_image_spotlight() You could generate a random list of points and then draw them: 您可以生成一个随机的点列表,然后绘制它们:

from random import randrange

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

This will create an image with 10 white spots on a black background. 这将在黑色背景上创建带有10个白色斑点的图像。 Change spot_count for more or fewer spots. spot_count更改为更多或更少的斑点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM