AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

Drawing in Python

Go down

Drawing in Python

Post by Lucaci Andrei on Wed Feb 15, 2012 1:40 am

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import Image, ImageDraw #As a first thing, you must have the third party graphics libraries.
#You can easily download them off of http://www.pythonware.com/products/pil/

def circleplusc():# Defining the new function
img = Image.new("RGB", (20, 20), "#FFFFFF") #Creating the new imagine, with the 20X20 pixels.
draw = ImageDraw.Draw(img) # We assign the function imagedraw.draw(img) to a value draw, for an easy use up ahead.

r, g, b = 0, 0, 0 # Assigning colors to the rgb values, r=0, g=0, b=0, meaning r=red, g=green, b=blue
r1, g1, b1 = 255, 255, 255 #Same as above.
draw.ellipse((0, 0, 16, 16), fill=(int(r), int(g), int(b))) #We draw the eclipse, oval, circle, depending on the coordinates.
draw.text((6, 3), "C", fill=(int(r1), int(g1), int(b1)))# We draw the text, literally... having as colors the opposite of the elipse.

img.save("circleplusc.png", "PNG") # We save the imagine as a PNG.
if __name__ == "__main__":
circleplusc() #We run the function.

#Tested with Python 2.7.2 interpretor, I personally use Eclipse for a better use on Python, but still, tested with Python also.
#The output is a PNG image, of a 20x20 pixels, with a black circle, and nearly its center a white C
#Tested and completed Feb 15, 2012 by sin.


A quick use of the code:
Code:
import Image, ImageDraw #As a first thing, you must have the third party graphics libraries.
#You can easily download them off of http://www.pythonware.com/products/pil/

def circleplusc():# Defining the new function
    img = Image.new("RGB", (20, 20), "#FFFFFF") #Creating the new imagine, with the 20X20 pixels.
    draw = ImageDraw.Draw(img) # We assign the function imagedraw.draw(img) to a value draw, for an easy use up ahead.

    r, g, b = 0, 0, 0 # Assigning colors to the rgb values, r=0, g=0, b=0, meaning r=red, g=green, b=blue
    r1, g1, b1 = 255, 255, 255 #Same as above.
    draw.ellipse((0, 0, 16, 16), fill=(int(r), int(g), int(b))) #We draw the eclipse, oval, circle, depending on the coordinates.
    draw.text((6, 3), "C", fill=(int(r1), int(g1), int(b1)))# We draw the text, literally... having as colors the opposite of the elipse.

    img.save("circleplusc.png", "PNG") # We save the imagine as a PNG.
if __name__ == "__main__":
    circleplusc() #We run the function.

#Tested with Python 2.7.2 interpretor, I personally use Eclipse for a better use on Python, but still, tested with Python also.
#The output is a PNG image, of a 20x20 pixels, with a black circle, and nearly its center a white C
#Tested and completed Feb 15, 2012 by sin.
avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 25
Location : Cluj-Napoca

View user profile http://www.andreil.wgz.ro

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum