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.

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

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



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

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))
        print 'Incorrect data'

example data:

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

sin 23

tan 56.0

cot 3
Incorrect data

factorial 123

Edited 4 Years Ago by pyTony: n/a

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.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])
    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))) ####################
        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."
d.lff(n) #Calling the class with the function which will LoadFromFile-lff
print "Drawing, please wait..."
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."

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.

Edited 4 Years Ago by Lucaci Andrew: n/a

position 100 50
line1 50 0
line2 0 50
line3 -50 0
line4 0 -50
This article has been dead for over six months. Start a new discussion instead.