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

Drawing in Python shapes using a .txt file for coordinates

Go down

Drawing in Python shapes using a .txt file for coordinates

Post by Lucaci Andrei on Sun Feb 19, 2012 8:47 pm

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'''
Created on Feb 16, 2012

@author: sin
'''
import Image, ImageDraw
import time
class draw:
def __init__(self, filename, name, width, height):
self.width = width
self.height = height
self.name = name
self.filename = filename
self.lff
self.pos = {}

def lff(self, filename):
fh = open(filename, 'r')
line = open(filename).read().splitlines() # Creating a list called line in which are stored all the coordinates
pos = (str(line[0])).split(' ') # Breaking the list 'line' apart, retrieving the coordinates specifically for each x and y coordinates
ln1 = (str(line[4])).split(' ')
ln2 = (str(line[3])).split(' ')
ln3 = (str(line[2])).split(' ')
ln4 = (str(line[1])).split(' ')
self.pos['posx'] = int(pos[1]) # Adding positions to the dictionary
self.pos['posy'] = int(pos[2])
self.pos['ln1x'] = int(ln4[1])
self.pos['ln1y'] = int(ln4[2])
self.pos['ln2x'] = int(ln3[1])
self.pos['ln2y'] = int(ln3[2])
self.pos['ln3x'] = int(ln2[1])
self.pos['ln3y'] = int(ln2[2])
self.pos['ln4x'] = int(ln1[1])
self.pos['ln4y'] = int(ln1[2])
fh.close()
self.print_pos()

def print_pos(self):
print "\nPosition x: ", self.pos['posx']
print "Position y: ", self.pos['posy']
print "Line 1 position x: ", self.pos['ln1x']
print "Line 1 position y: ", self.pos['ln1y']
print "Line 2 position x: ", self.pos['ln2x']
print "Line 2 position y: ", self.pos['ln2y']
print "Line 3 position x: ", self.pos['ln3x']
print "Line 3 position y: ", self.pos['ln3y']
print "Line 4 position x: ", self.pos['ln4x']
print "Line 4 position y: ", self.pos['ln4y'], "\n"

def draws(self, width, height):
img = Image.new("RGB", (width, height), "#000000") # Using the modules Image and ImageDraw
draw = ImageDraw.Draw(img) # You can find these and download from http://www.pythonware.com/products/pil/

r, g, b = 255, 255, 255 # Assigning colors for the RGB values, r=red, g=green, b=blue

draw.line((self.pos['ln1x'] + self.pos['posx'], self.pos['ln1y'] + self.pos['posy']), fill=0) ####################
draw.line((self.pos['ln2x'] + self.pos['posx'], self.pos['ln2y'] + self.pos['posy']), fill=0) # For some reasons #
draw.line((self.pos['ln3x'] + self.pos['posx'], self.pos['ln3y'] + self.pos['posy']), fill=0) # this won't work #
draw.line((self.pos['ln4x'] + self.pos['posx'], self.pos['ln4y'] + self.pos['posy']), fill=0) ####################

#But:

draw.polygon(((self.pos['ln1x'] + self.pos['posx'], self.pos['ln1y'] + self.pos['posy']), (self.pos['ln2x'] + self.pos['posx'], self.pos['ln2y'] + self.pos['posy']), (self.pos['ln3x'] + self.pos['posx'], self.pos['ln3y'] + self.pos['posy']), (self.pos['ln4x'] + self.pos['posx'], self.pos['ln4y'] + self.pos['posy'])), fill=(int(r), int(g), int(b)))
#^- draws the polygon with the coordinates given from the 4 lines, and the position.

img.save(self.name + ".img", "IMG") #Saving the image under the name of draw.png. You can see the additional "PNG" instruction
#specifying its type


n = raw_input("Please specify the file: \n")
n = n + ".txt"
name = raw_input("Please insert the name of the image: \n")
print "Image options"
width = int(raw_input("Width: "))
height = int(raw_input("Height: "))
d = draw(n, name, width, height) # Assigning to the class draw the input filename.
print "Reading file."
time.sleep(2)
d.lff(n) #Calling the class with the function which will LoadFromFile-lff
print "Drawing, please wait..."
time.sleep(2)
if __name__ == "__main__":
d.draws(width, height) #Actual drawing
print "\nDrawing complete. You now can access the image", name + '.png'
print "Happy coding, love from s|n."
time.sleep(3)


Here's the attachment for this code snippet...
Download paint.txt
avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 26
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