0

I need to create a half circle in angle 45 (a moon) , of radius 20 in the left side of a pic. I'm new to the image processing in Python. I've downloaded the PIL library, can anyone give me an advice? Thanks

CnR
  • 351
  • 1
  • 6
  • 15

2 Answers2

1

This might do what you want:

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

Ref: http://effbot.org/imagingbook/imagedraw.htm


This program might satisfy the newly-described requirements:

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
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • This is good but it doesn't help me. I will explain: I need to create a function that it's input is an object of class Matrix (that's the picture represented by a matrix) and the output is the picture with the moon on it. I can't use these functions on the class Matrix, I tried to change your code so it will work for me but it doesn't – CnR Jan 08 '14 at 19:47
  • 1
    Is `Matrix` a class of your own design, or is it part of some third-party library? And how is `Matrix` related to [tag:python-imaging-library]? – Robᵩ Jan 08 '14 at 19:55
  • Matrix is a class that my university is using, we use matrices to represent pictures.. do you have any way to help me please? – CnR Jan 08 '14 at 19:59
  • Does your university supply a drawing package that can manipulate `Matrix` images? Or can you convert a `Matrix` to something that `Image.frombuffer()` can use? – Robᵩ Jan 08 '14 at 20:13
  • No, in order to create a white line for exmaple, I write the matrix and the lines and columns, like that: mat[i,j]=255 [the value for white] . I guess I really need to create the moon by hand – CnR Jan 08 '14 at 20:18
  • Create a moon using PIL. Conver the image to a list of pixel values using `im.getdata()`. Apply that list to your Matrix. I'll put up an example in a few minutes. – Robᵩ Jan 08 '14 at 20:32
0

Draw half circle easily using the pieslice function:

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

Result:

Resulting moon image

DarthVanger
  • 1,063
  • 10
  • 10