2017-12-21 16 views
0

黒い画像に白い点を描くサンプルコードを書いた。私は一度に一点を描くことができました。私は入力としてポイントのセットを与え、黒い画像上に白い画像スポットを描画したいと思います。誰かがどのように進むべきか私に示唆することができますか?黒い画像に点を描く

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) 
+0

探しているものを達成するためにmatlabplotを使うことができます。[Documentation](https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter) –

答えて

0

コードは、ImageHandlerクラスのコードが必要としているように見えます。現在、単一のポイントを含むリストが渡されています。この同じ点が黒い画像に1度描かれているので、あなたは1点しか見ることができず、常に同じ位置になります。

代わりに、複数のポイントを含むリストを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を変更してください。

関連する問題