Hi, firstly i'm gonna be honest and say that this is homework for my programming class, but the teacher doesn't have a problem with us using the internet to get help aslong as we dont blatantly steal code.

The problem is to take a text file containing for example the following:

position 50 10
line 50 0
line 0 50
line -50 0
line 0 -50

and interpret these into python drawing commands (this case would be a square).

I'm totally stuck with what to do here to write this program, all i've got at the moment is a little code to actually read the text file:

f = open("test1.txt","r")
for line in f:
    print line,
    line = line.strip()
    data = split(line, " ")

but i've got no idea at all where to go from here. Obviously i'm not asking for a complete solution, but a point in the right direction would be much appreciated. Thanks.

Recommended Answers

All 5 Replies

Do you have a library that you're supposed to be using for the drawing? If not, you might want to look in to tk.

#!/usr/bin/env python

from Tkinter import *

tk = Tk()
tk.wm_geometry("400x400")

canvas = Canvas(tk, width=400, height=400)
canvas.pack()

canvas.create_line(0,0,400,400)
canvas.create_line(400,0,0,400)

tk.mainloop()

Sorry, yeah we've got a Canvas module, the functions that we've to use for this are:

create_oval(x1,y1,x2,y2)
create_line( x1, y1, x2, y2)
and a function for position & for move also.

Okay, so it looks like you need a few variables, for the current x and y of the "paintbrush", then you can loop over your data which will be in the format: (command, x, y)

do a bunch of ifs for the commmand, i.e.

if command == "line":
    <paint the line>

then at the end set your new x and y to be where your brush left off.

In order to help you without giving ready answer here is your code adapted to do math functions from textfile:

import math
f = open("funcs.txt","r")
for line in f:
    print line,
    line = line.strip()
    data = line.split(" ")
    if data[0] in dir(math) and len(data) == 2:
        print eval("math.%s(%s)" % tuple(data))
    else:
        print 'Incorrect data'
    print

example data:

sqrt 4
sin 23
tan 56.0
cot 3
factorial 123
sqrt 4
2.0

sin 23
-0.846220404175

tan 56.0
-0.611273688192

cot 3
Incorrect data

factorial 123
12146304367025329675766243241881295855454217088483382315328918161829235892362167668831156960612640202170735835221294047782591091570411651472186029519906261646730733907419814952960000000000000000000000000000

I've look into your assignment and I've come up with a possible module.
I'll post the code below, and an attachament with the .txt containing the positions.
Please, report me if there were any bugs inside the program, 'cose I'm a noob @ Python, and I don't want to miss any chance of learning from my miTSAkes:D

Note that the elements from the .txt file must be with that configuration, meaning you can change only the values, NOT the strings.

'''
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'] + 100, self.pos['ln1y'] + 100), fill=(int(r), int(g), int(b))) ####################
        draw.line((self.pos['ln2x'] + 100, self.pos['ln2y'] + 100), fill=(int(r), int(g), int(b))) # For some reasons #
        draw.line((self.pos['ln3x'] + 100, self.pos['ln3y'] + 100), fill=(int(r), int(g), int(b))) # this won't work  # 
        draw.line((self.pos['ln4x'] + 100, self.pos['ln4y'] + 100), fill=(int(r), int(g), int(b))) ####################
        
        #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 + ".png", "PNG") #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)

"""
Additional information: the paint.txt file for example:
position 100 50
line1 50 0
line2 0 50
line3 -50 0
line4 0 -50
"""

Post a reply with your opinion or comments.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.