📅  最后修改于: 2023-12-03 15:24:44.603000             🧑  作者: Mango
在Python中,我们可以使用PIL库中的Image和ImageDraw来处理图像,然后使用matplotlib库来显示图像。要将图像放入烧瓶中,我们需要使用这些库中的函数来实现。
以下是将图像放入烧瓶中的步骤:
1.导入所需的库和模块
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
2.打开图像并创建绘图对象
img = Image.open('image.png')
draw = ImageDraw.Draw(img)
3.绘制烧瓶
width, height = img.size
draw.ellipse((width/2-30, height/2-70, width/2+30, height/2-10), fill=(255, 255, 255), outline=(0, 0, 0), width=2)
draw.rectangle((width/2-10, height/2-70, width/2+10, height/2+10), fill=(255, 255, 255), outline=(0, 0, 0), width=2)
draw.rectangle((width/2-70, height/2+10, width/2+70, height/2+50), fill=(255, 255, 255), outline=(0, 0, 0), width=2)
draw.line((width/2, height/2-70, width/2, height/2-90), fill=(0, 0, 0), width=2)
4.保存和显示结果
img.save('result.png')
plt.imshow(img)
plt.show()
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
img = Image.open('image.png')
draw = ImageDraw.Draw(img)
width, height = img.size
draw.ellipse((width/2-30, height/2-70, width/2+30, height/2-10), fill=(255, 255, 255), outline=(0, 0, 0), width=2)
draw.rectangle((width/2-10, height/2-70, width/2+10, height/2+10), fill=(255, 255, 255), outline=(0, 0, 0), width=2)
draw.rectangle((width/2-70, height/2+10, width/2+70, height/2+50), fill=(255, 255, 255), outline=(0, 0, 0), width=2)
draw.line((width/2, height/2-70, width/2, height/2-90), fill=(0, 0, 0), width=2)
img.save('result.png')
plt.imshow(img)
plt.show()
输出结果如下图所示: