2014-01-08 29 views
0

我需要在图片左侧的半径为20的角45(月亮)中创建一个半圆。我不熟悉Python中的图像处理。我已经下载了PIL库,任何人都可以给我一个建议? 感谢用Python在角度45中创建一个半圆

+1

不知道这帮助,但这个问题有几个答案... http://stackoverflow.com/questions/2980366/ draw-circle-pil-python – tjoenz

+0

Python不是绘图工具。根本不是。试试inkscape。 –

+3

什么是“45角的半圆”?你可以张贴一张照片吗? – Kevin

回答

1

这可能会做你想要什么:

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") 

编号:http://effbot.org/imagingbook/imagedraw.htm


这个程序可能满足新描述要求:

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 
+0

这很好,但它不帮助我。我将解释:我需要创建一个函数,它的输入是Matrix类的对象(这是矩阵表示的图片),输出是带有月亮的图片。我不能在类Matrix上使用这些函数,我试图改变你的代码,所以它可以用于我,但它不是 – CnR

+1

'Matrix'是你自己设计的一类,还是它的一部分第三方图书馆?那么'Matrix'与[tag:python-imaging-library]有什么关系? –

+0

矩阵是我的大学正在使用的一类,我们使用矩阵来表示图片..您有什么方法可以帮助我吗? – CnR