如何用python绘制圆,Python PIL-绘制圆

I am trying to draw a simple circle and save this to a file using the Python Imaging Library:

import Image, ImageDraw

image = Image.new('RGBA', (200, 200))

draw = ImageDraw.Draw(image)

draw.ellipse((20, 180, 180, 20), fill = 'blue', outline ='blue')

draw.point((100, 100), 'red')

image.save('test.png')

The point draw.point appears on the image, but the ellipse itself does not. I tried changing the mode to just RGB (I thought the mode might affect what is displayed), but this did not solve it.

How can I fix this? Thanks!

解决方案

Instead of specifying the upper right and lower left coordinates, swap them to get the upper left and lower right.

draw.ellipse((20, 20, 180, 180), fill = 'blue', outline ='blue')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 PIL 库来绘制形 mask。 下面是一个示例代码: ```python from PIL import Image, ImageDraw # 生成一个空白图片 img = Image.new('L', (300, 300), 0) # 创建一个draw = ImageDraw.Draw(img) # 绘制draw.ellipse((50, 50, 250, 200), fill=255) # 保存图片 img.save('ellipse_mask.png') ``` 这个代码会生成一个 300x300 的黑色图片,其中心区域为一个白色的椭形。你可以根据需要修改椭形的位置、大小和颜色。 ### 回答2: 要使用Python绘制形的mask,可以使用OpenCV库。以下是一个简单的方法: 首先,导入所需的库: ``` import cv2 import numpy as np ``` 然后,创建一个新的空白图像,宽度为w,高度为h: ``` w = 400 h = 300 mask = np.zeros((h, w), dtype=np.uint8) ``` 接下来,定义椭的参数,包括中心位置(cx, cy),长轴长度(rx),短轴长度(ry)和旋转角度(angle): ``` cx = w // 2 # 椭中心点的x坐标 cy = h // 2 # 椭中心点的y坐标 rx = w // 4 # 椭长半轴长度 ry = h // 8 # 椭短半轴长度 angle = 30 # 椭的旋转角度 ``` 接下来,使用cv2.ellipse函数绘制: ``` cv2.ellipse(mask, (cx, cy), (rx, ry), angle, 0, 360, 255, -1) ``` 这里的参数依次为:要绘制的图像、中心位置、长轴和短轴长度、旋转角度、起始角度、结束角度、线的颜色、线宽(-1表示填充)。 最后,可以查看和保存生成的mask图像: ``` cv2.imshow('Mask', mask) cv2.waitKey(0) cv2.imwrite('ellipse_mask.png', mask) ``` 这样就可以使用Python绘制形的mask了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值