Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert images drawn by turtle to PNG in Python

I'm making a abstract art template generator in Python that takes inputs of minimum radius, maximum radius, and number of circles. It draws random circles in random places, also meeting the user's specifications. I want to convert the Turtle graphics into a PNG so that the user can then edit the template however he/she wants to, but I don't know how to proceed. Here's my code:

import random  
import time  
import turtle  

print("Abstract Art Template Generator")
print()
print("This program will generate randomly placed and sized circles on a blank screen.")
num = int(input("Please specify how many circles you would like to be drawn: "))
radiusMin = int(input("Please specify the minimum radius you would like to have: "))
radiusMax = int(input("Please specify the maximum radius you would like to have: "))
screenholder = input("Press ENTER when you are ready to see your circles drawn: ")

t = turtle.Pen()

win = turtle.Screen()


def mycircle():
    x = random.randint(radiusMin,radiusMax) 
    t.circle(x)

    t.up()
    y = random.randint(0,360)
    t.seth(y)
    if t.xcor() < -300 or t.xcor() > 300:
        t.goto(0, 0)
    elif t.ycor() < -300 or t.ycor() > 300:
        t.goto(0, 0)
    z = random.randint(0,100)
    t.forward(z)
    t.down()


for i in range(0, num):
    mycircle()


turtle.done()
like image 989
TimRin Avatar asked Feb 25 '16 14:02

TimRin


People also ask

How to save a turtle image in Python?

To save the turtle image we have to remember two important things: The python file and the image file should be in the same folder. The image file should be in the “gif” format only. If the image is not in the form of a “gif” then we have to change it by using the paint and saving the file in gif format.

How to convert JPG_to_PNG in Python?

It is the standard Python interface to the Tk GUI toolkit, and is Python’s de facto standard GUI. Step 1: Import the library. In Function jpg_to_png we first Check whether The Selecting the image is in the same Format ( .jpg) which to convert to . png if not then return Error. Else Convert the image the to .png

How to draw a turtle on the screen in Python?

Turtle (pen). To draw something on the screen, we need to move the turtle (pen), and to move the turtle, there are some functions like the forward (), backward (), etc. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

What is the use of the turtle module in Python?

Turtle is an inbuilt module in Python. It provides: Drawing using a screen (cardboard). Turtle (pen). To draw something on the screen, we need to move the turtle (pen), and to move the turtle, there are some functions like the forward (), backward (), etc. Attention geek!


1 Answers

You can use turtle.getcanvas() to generate Tkinker canvas. Then save it as postscript file.

...
cv = turtle.getcanvas()
cv.postscript(file="file_name.ps", colormode='color')

turtle.done()

Then you can convert it to png (I think you will find how to do it). Or use PIL with Tkinker - more about this method here

like image 187
Valentin Briukhanov Avatar answered Sep 22 '22 18:09

Valentin Briukhanov