在黑色图像上绘制点

2024-06-16 15:09:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个示例代码来在黑色图像上绘制白点。我一次只能画一个点。我想给一组点作为输入,并绘制在黑色图像上的白色图像点。有人能建议我怎么做吗?你知道吗

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)

Tags: 图像imageselfforgetdefhiwidth
1条回答
网友
1楼 · 发布于 2024-06-16 15:09:03

你在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以获得更多或更少的斑点。你知道吗

相关问题 更多 >