使用PIL,我想通过指定正方形边长和旋转角度在图像上绘制一个旋转的正方形。正方形应该是白色的,背景应该是灰色的。例如,下图旋转45度:
我知道如何在PIL中旋转的唯一方法是旋转整个图像。但如果我从下面的图像开始:
然后旋转45度,我得到:
这种方法只是引入黑色部分来填充图像的“未定义”区域。在
我怎么才能旋转正方形呢?在
生成我的原始方块(第二个图)的代码如下:
from PIL import Image
image = Image.new('L', (100, 100), 127)
pixels = image.load()
for i in range(30, image.size[0] - 30):
for j in range(30, image.size[1] - 30):
pixels[i, j] = 255
rotated_image = image.rotate(45)
rotated_image.save("rotated_image.bmp")
如果你只想画一个任意角度的纯色正方形,可以用三角法计算旋转正方形的顶点,然后用
polygon
绘制。在结果:
我们将其推广到矩形:
旋转代码:
图纸代号:
^{pr2}$相关问题 更多 >
编程相关推荐