向imag添加白圈

2024-09-28 01:23:53 发布

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

我使用的是python3.7.4,我试图给一个图像添加一个白色的圆圈,但是我无法添加白色。 这是我到目前为止的代码:(我已经做了一个特定的图像)

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)

Tags: from图像imageimportpilmatplotlibasplt
2条回答

此版本适用于:

#!/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

当您使用matplotlib的imshow时,您可以指定colormap(cmap)参数,如果您不这样做,matplotlib将使用默认的colormap,这可能不是您所期望的。通过使用plt.colorbar(),您可以看到正在使用什么颜色映射。请参阅我修订的代码中的一些示例。另见matplotlib colormap documentation。你知道吗

import matplotlib.pyplot as plt
from PIL import Image, ImageDraw

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=ima(200,255)
draw=ImageDraw.Draw(image)

draw.ellipse([(50,50),(190,245)], fill='white', outline='white') 

plt.close('all')

plt.figure()
plt.imshow(image) # <  matplotlib using it's default color translation
plt.colorbar()

plt.figure()
plt.imshow(image, cmap='Greys')
plt.colorbar()

plt.figure()
plt.imshow(image, cmap='gray')
plt.colorbar()

plt.show()

相关问题 更多 >

    热门问题