0

I've written a program which draws a box template which can be printed, cut, scored and folded into a small box.

The program askes the user for the length and width of the box in mm. The tabs of the box are all 15mm.

After a bit of Googling, I found out that Turtle uses pixels to determine its movement distance, eg. turtle.forward(100) moves the Turtle forward 100 pixels (I think).

I found an converter online which converts pixels into mm. My screen is 96dpi and based on this I found out that 1 mm is equal to 3.7795275591 pixels.

So I put this into my code:

px = 3.7795275591

length = px * float(input("Length: "))
width = px * float(input("Width: "))

When I measure the on-screen image or the printed image, the lines aren't correct, both different results. I changed the px = to something around 3.22 and I was getting accurate measurements on-screen but not in printed format.

Obviously, I am completely misunderstanding what the deal is with pixels and screens and I have no idea how to get accurate measurements but I would really like to.

Can you help me?

Here is all my code, in case it's of relevance. I understand that it could probably be coded much better, I'm beginner level at best!

import turtle
from PIL import Image

def turt_setup():
    turtle.screensize(canvwidth=1920, canvheight=1080)
    turtle.speed(5)

    turtle.penup()
    turtle.left(180)
    turtle.forward(200)
    turtle.right(180)
    turtle.pendown()

    turtle.color(input("Color: "))


def turt_draw():
    #Draw top of box
    turtle.forward(length)
    turtle.left(90)
    turtle.forward(width)
    turtle.left(90)
    turtle.forward(length)
    turtle.left(90)
    turtle.forward(width)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(length)

    #Draw tabs
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)

    turtle.forward(width)

    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)

    turtle.forward(length)

    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)

    turtle.forward(width)

    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)

    turtle.forward(length)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(16)
    turtle.right(90)

    #Draw bottom of box
    turtle.forward(x)
    turtle.left(90)
    turtle.forward(y-1)
    turtle.left(90)
    turtle.forward(x)
    turtle.left(90)
    turtle.forward(y)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(x)

    #Draw tabs
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)

    turtle.forward(y+1)

    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)
    turtle.left(90)
    turtle.forward(15)

    turtle.forward(x)

    turtle.getscreen()

def save_files():
    turtle.getcanvas().postscript(file="box.eps")

    image_eps = 'box.eps'
    im = Image.open(image_eps)
    fig = im.convert('RGBA')
    image_png= 'box.png'
    fig.save(image_png, lossless = True)



#Main section
turt_setup()

px = 3.7795275591


length = px * float(input("Length: "))
width = px * float(input("Width: "))

x = length -1
y = width -1

turt_draw()
save_files()

turtle.done()
    
  • 1
    I can at least tell you that the resolution of your screen doesn't really matter, only the resolution at which the image is printed. You probably need to decide on a print resolution (say, 300 dpi), generate the image with that in mind, and then make sure to instruct users to print it at that resolution. – CrazyChucky Jun 03 '22 at 15:30
  • Thanks very much =], this should help a lot. I'm just trying to figure out how to find printing resolution out now. – SatsumaSegment Jun 03 '22 at 15:52
  • 1
    That will vary by what method you're using to print it. The default print dialogs in Windows and macOS usually have an option to specify DPI. – CrazyChucky Jun 03 '22 at 15:58

0 Answers0