Search code examples
pythonimagematplotlibdraw

Adding a white circle to an image


im using python 3.7.4, im trying to add to an image a white circle, but im inable to add the color white. this is my code so far:(i already made a certain image)

from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def ima(n,m):
    what=Image.new(mode='L', size=(n,n), color=m)
    mat=what.load()
    for x in range(n):
        for y in range(n):
            mat[x,y]=x%256
    return what
image=surprise(200,255) #my random image
from PIL import Image, ImageDraw
image=ima(200,255)
draw=ImageDraw.Draw(image)
draw.ellipse([(50,50),(190,245)],fill='white',outline='white') #i want the fill to be white,i tried writing None, it did not give me a white circle.(a circle of a differnet color)
plt.show(block=image)
imageplot=plt.imshow(image)

Solution

  • This version works:

    #!/usr/bin/env python3
    from PIL import Image, ImageDraw
    import matplotlib.pyplot as plt
    
    def ima(n,m):
        """Create and return an nxn gradient image"""
        what=Image.new(mode='L', size=(n,n), color=m)
        mat=what.load()
        for x in range(n):
            for y in range(n):
                mat[x,y]=x%256
        return what
    
    # Create image 200x200
    image=ima(200,255)
    
    # Get drawing handle
    draw=ImageDraw.Draw(image)
    draw.ellipse([(50,50),(190,245)],fill='white',outline='white')
    
    # Display result
    image.show() 
    

    enter image description here