Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向图像中添加白色圆圈_Python_Image_Matplotlib_Draw - Fatal编程技术网

Python 向图像中添加白色圆圈

Python 向图像中添加白色圆圈,python,image,matplotlib,draw,Python,Image,Matplotlib,Draw,我在使用python 3.7.4,我试图在图像中添加一个白色圆圈,但我无法添加白色。 这是我到目前为止的代码:(我已经制作了一个特定的图像) 此版本适用于: #!/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

我在使用python 3.7.4,我试图在图像中添加一个白色圆圈,但我无法添加白色。 这是我到目前为止的代码:(我已经制作了一个特定的图像)

此版本适用于:

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

使用matplotlib的
imshow
时,可以指定一个colormap(
cmap
)参数,如果不指定,matplotlib将使用默认的colormap,这可能不是您所期望的。您可以使用
plt.colorbar()
查看正在使用的颜色映射。有关一些示例,请参见我修订的代码。另见

导入matplotlib.pyplot作为plt
从PIL导入图像,ImageDraw
def ima(n,m):
what=Image.new(mode='L',size=(n,n),color=m)
mat=what.load()
对于范围(n)内的x:
对于范围(n)内的y:
材料[x,y]=x%256
还什么
image=ima(200255)
draw=ImageDraw.draw(图像)
绘制椭圆([(50,50),(190245)],填充为白色,轮廓为白色)
plt.close(“全部”)
plt.图()

plt.imshow(image)#命令image.show()显示AttributeError:'NoneType'对象没有属性'show'。我使用了imageplot=plt.imshow(image)和plt.show(block=image),如何使用image.show()?好的,尝试使用
image.save('result.png')将其保存在当前目录中
然后查看。您使用的是什么操作系统?@Mary您需要
plt.imshow(image,cmap='Greys_r')
然后是
plt.show()
以使用“正确”的灰度值显示图像。
plt.show(block=True)
意味着应该阻止程序,以便用户可以与图像交互(例如缩放)。一旦用户关闭绘图窗口,程序将继续
plt.show(block=False)
在没有交互的情况下显示图像,并在程序结束时自动关闭绘图(在本示例代码中为立即关闭)。我再次尝试(没有image.save),结果成功了!非常感谢!请注意,
Image.new(mode='L',…
会创建一个具有256个灰度值的黑白图像。请注意,在颜色贴图名称后添加“\u r”会给出反向颜色贴图的名称。因此,您可以使用“grey\u r”以白色显示圆。颜色贴图的其他可能选项有“bone”、“copper”或“pink”,这取决于灰度图像的预期用途。
#!/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() 
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()