在Python中创建角度为45的半圆
我需要创建一个角度为45(一个月亮)的半圆,半径为20,在图片的左侧。我不熟悉Python中的图像处理。我已经下载了PIL库,有人能给我一个建议吗?在Python中创建角度为45的半圆,python,python-3.x,python-imaging-library,Python,Python 3.x,Python Imaging Library,我需要创建一个角度为45(一个月亮)的半圆,半径为20,在图片的左侧。我不熟悉Python中的图像处理。我已经下载了PIL库,有人能给我一个建议吗? 谢谢这可能会满足您的要求: import Image, ImageDraw im = Image.open("Two_Dalmatians.jpg") draw = ImageDraw.Draw(im) # Locate the "moon" in the upper-left region of the image xy=[x/4 for
谢谢这可能会满足您的要求:
import Image, ImageDraw
im = Image.open("Two_Dalmatians.jpg")
draw = ImageDraw.Draw(im)
# Locate the "moon" in the upper-left region of the image
xy=[x/4 for x in im.size+im.size]
# Bounding-box is 40x40, so radius of interior circle is 20
xy=[xy[0]-20, xy[1]-20, xy[2]+20, xy[3]+20]
# Fill a chord that starts at 45 degrees and ends at 225 degrees.
draw.chord(xy, 45, 45+180, outline="white", fill="white")
del draw
# save to a different file
with open("Two_Dalmatians_Plus_Moon.png", "wb") as fp:
im.save(fp, "PNG")
参考:
该计划可能满足新描述的要求:
import Image, ImageDraw
def InitializeMoonData():
''''
Return a 40x40 half-circle, tilted 45 degrees, as raw data
Only call once, at program initialization
'''
im = Image.new("1", (40,40))
draw = ImageDraw.Draw(im)
# Draw a 40-diameter half-circle, tilted 45 degrees
draw.chord((0,0,40,40),
45,
45+180,
outline="white",
fill="white")
del draw
# Fetch the image data:
moon = list(im.getdata())
# Pack it into a 2d matrix
moon = [moon[i:i+40] for i in range(0, 1600, 40)]
return moon
# Store a copy of the moon data somewhere useful
moon = InitializeMoonData()
def ApplyMoonStamp(matrix, x, y):
'''
Put a moon in the matrix image at location x,y
Call whenever you need a moon
'''
# UNTESTED
for i,row in enumerate(moon):
for j,pixel in enumerate(row):
if pixel != 0:
# If moon pixel is not black,
# set image pixel to white
matrix[x+i][y+j] = 255
# In your code:
# m = Matrix(1024,768)
# m = # some kind of math to create the image #
# ApplyMoonStamp(m, 128,128) # Adds the moon to your image
不确定这是否有帮助,但这个问题有一些答案。。。Python不是用于绘图的工具。根本不是。试试inkscape。45度角的半圆是什么样子?你能贴一张照片吗?这很好,但对我没有帮助。我将解释:我需要创建一个函数,它的输入是类矩阵的对象(即矩阵表示的图片),输出是带月亮的图片。我不能在类矩阵上使用这些函数,我试图更改您的代码,使其对我有效,但它不是您自己设计的类,或者它是某个第三方库的一部分?矩阵与什么有关系?矩阵是我们大学正在使用的一门课,我们用矩阵来表示图片。。你有什么办法帮我吗?你们大学有没有提供一个可以处理矩阵图像的绘图软件包?或者您可以将
矩阵
转换为Image.frombuffer()
可以使用的东西吗?不,为了为exmaple创建一条白线,我写下矩阵以及行和列,如下所示:mat[I,j]=255[白色的值]。我想我真的需要手工创造月亮