【问题标题】:Create a half circle in angle 45 in Python在 Python 中创建角度为 45 的半圆
【发布时间】:2014-01-08 18:00:38
【问题描述】:

我需要在图片左侧创建一个角度为 45(月亮)、半径为 20 的半圆。我是 Python 图像处理的新手。我已经下载了 PIL 库,有人可以给我建议吗? 谢谢

【问题讨论】:

标签: python python-3.x python-imaging-library


【解决方案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

【讨论】:

  • 这很好,但对我没有帮助。我将解释:我需要创建一个函数,它的输入是类 Matrix 的对象(即由矩阵表示的图片),输出是上面有月亮的图片。我不能在类 Matrix 上使用这些函数,我试图更改你的代码,以便它对我有用,但它没有
  • Matrix 是您自己设计的类,还是某个第三方库的一部分? Matrixpython-imaging-library 有什么关系?
  • 矩阵是我大学正在使用的一个类,我们用矩阵来表示图片..请问您有什么方法可以帮助我吗?
  • 您的大学是否提供可以处理Matrix 图像的绘图包?或者您可以将Matrix 转换为Image.frombuffer() 可以使用的东西吗?
  • 不,为了创建例如白线,我编写了矩阵以及行和列,如下所示: mat[i,j]=255 [the value for white] 。我想我真的需要手工创造月亮
【解决方案2】:

使用pieslice 函数轻松绘制半圆:

from PIL import Image, ImageDraw

# Create a new empty 100x100 image for the sake of example.
# Use Image.open() to draw on your image instead, like this:
# img = Image.open('my_image.png')
img = Image.new('RGB', (100, 100))

radius = 25

# The circle position and size are specified by
# two points defining the bounding rectangle around the circle
topLeftPoint = (0, 0)
bottomRightPoint = (radius * 2, radius * 2)

draw = ImageDraw.Draw(img)

# Zero angle is at positive X axis, and it's going clockwise.
# start = 0, end = 180 would be bottom half circle.
# Adding 45 degrees, we get the diagonal half circle.
draw.pieslice((topLeftPoint, bottomRightPoint), start = 45, end = 180 + 45, fill='yellow')

img.save('moon.png')

结果:

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 2012-07-14
    • 2020-12-15
    • 1970-01-01
    • 2020-10-27
    • 2021-08-18
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    相关资源
    最近更新 更多