I would like to figure out how to save a bitmap or vector graphics image after creating a drawing with python's turtle module. After a bit of googling I can't find an easy answer. I did find a module called canvas2svg, but I'm very new to python and I don't know how to install the module. Is there some built in way to save images of the turtle canvas? If not where do I put custom modules for python on an Ubuntu machine?
from tkinter import * # Python 3
#from Tkinter import * # Python 2
import turtle
turtle.forward(100)
ts = turtle.getscreen()
ts.getcanvas().postscript(file="duck.eps")
This will help you; I had the same problem, I Googled it, but solved it by reading the source of the turtle module.
The canvas (tkinter) object has the postscript function; you can use it.
The turtle module has "getscreen" which gives you the "turtle screen" which gives you the Tiknter canvas in which the turtle is drawing.
This will save you in encapsulated PostScript format, so you can use it in GIMP for sure but there are other viewers too. Or, you can Google how to make a .gif from this. You can use the free and open source Inkscape application to view .eps files as well, and then save them to vector or bitmap image files.
I wrote the svg-turtle package that supports the standard Turtle interface from Python, and writes an SVG file using the svgwrite module. Install it with pip install svg-turtle, and then call it like this:
from svg_turtle import SvgTurtle
def draw_spiral(t):
t.fillcolor('blue')
t.begin_fill()
for i in range(20):
d = 50 + i*i*1.5
t.pencolor(0, 0.05*i, 0)
t.width(i)
t.forward(d)
t.right(144)
t.end_fill()
def write_file(draw_func, filename, width, height):
t = SvgTurtle(width, height)
draw_func(t)
t.save_as(filename)
def main():
write_file(draw_spiral, 'example.svg', 500, 500)
print('Done.')
if __name__ == '__main__':
main()
The canvasvg package is another option. After you run some turtle code, it will convert all the items on the tkinter canvas into an SVG file. This requires tkinter support and a display, where svg-turtle doesn't.
Related
I am trying to make a program to display one single image (.png extension) at once but giving a button to the user to change the picture.
What I have done is:
Reading the Image from my directory with help of Pillow module
Appended it to a list
With a button I increase or decrease the index of the list.
(Note I have to read 600 images approx.)
Here's the code:
import os
from tkinter import *
from PIL import ImageTk,Image
import threading,time
#Define the tkinter instance
x=0
win= Tk()
dir_path= os.path.dirname(os.path.realpath(__file__))
print(dir_path)
l1=[]
#Define the size of the tkinter frame
win.geometry("700x400")
def start():
threading.Thread(target=bg).start()
win.after(5000,threading.Thread(target=fg).start())
#Define the function to start the thread
def bg():
print("bg")
for i in range(1,604):
a=Image.open(f"{dir_path}\\{i}.png")
a=a.resize((500,700), Image.ANTIALIAS)
b=ImageTk.PhotoImage(a)
l1.append(b)
print(b)
print(len(l1))
def fg():
def add():
global x
x+=1
img2=l1[x]
d.configure(image=img2)
d.image = img2
d.update()
global d
d=Label(win,image=l1[x])
d.pack()
Button(win,text="add",command=add).place(x=0,y=0)
label= Label(win)
label.pack(pady=20)
#Create button
b1= Button(win,text= "Start", command=start)
b1.pack(pady=20)
win.mainloop()
But the problem is that the Tkinter gets dead freeze and laggy to an extent that the GUI is not operable.
So my question is,
How to fix Tkinter dead Frezzes and if there is any way to read the images as fast as possible?
The freezes of Tkinter depends on reading speed of the interpreter, because:
continuously reading and Showing the pictures are a huge job for Python.
reading images using opencv or any other image processing module wont help as reading part can become faster, but showing the image in tkinter is done using python only, so rapid changes in Label will cause tkinter to crash.
Solution:
Switch to a different compiler based language for example c++.
this solution is specifically for a image slideshow,
The solution is to use selenium with python. You can specify the image location in driver.get(image_path) and it will open the image for you, and if you want to change the image with a button, then just rename all your images with the numbers and make a button to add +1 to the index.
I try to solve error for this code. I'm using mac os
file is in right directory and use gif file.
but I kept getting error. How can I solve this(please help me)
import turtle
import random
screen=turtle.Screen()
image1="/Users/jameslee/Downloads/front.gif"
image2="/Users/jameslee/Downloads/back.gif"
screen.addshape(image1)
screen.addshape(image2)
t1=turtle.Turtle()
coin=random.randint(0, 1)
if coin==0:
t1.shape(image1)
t1.stamp()
else :
t1.shape(image2)
t1.stamp()
Your code is fine. I've simplified it below for my testing purposes. The next issue is the *.gif files themselves. Either they are variants of GIF that tkinter doesn't recognize, or they aren't GIF files (e.g. something else renamed with a *.gif extension.) Since you say you're on Mac OSX, we can test this. Run /Applications/Utilities/Terminal.app, cd to the directory in question and run the Unix file command:
> cd /Users/jameslee/Downloads
> file front.gif
front.gif: GIF image data, version 89a, 50 x 50
>
Your output should be similar -- let us know what you get. Your code simplified:
from turtle import Screen, Turtle
from random import choice
image1 = "/Users/jameslee/Downloads/front.gif"
image2 = "/Users/jameslee/Downloads/back.gif"
screen = Screen()
screen.addshape(image1)
screen.addshape(image2)
turtle = Turtle()
turtle.shape(choice([image1, image2]))
turtle.stamp()
screen.exitonclick()
I am new to Python turtle module, I wrote a very easy code, like,
from turtle import *
import turtle
forward(100)
right(90)
forward(200)
circle(10)
ts = turtle.getscreen()
turtle.getcanvas().postscript(file = "duck.eps")
I got a .eps image, it looks well, but what I want is an animated image in .gif. However, when I try to say,
turtle.getcanvas().postscript(file = "duck.gif")
I got the duck.gif, but I cannot open it !!
What should I do now?
I have two pictures "gif" and want to insert them as small pictures and add them to Python code. I want the picture to stay in an accurate location as well decrease it size. When i try to run this code the shell shows an error "screen isn't defined"
import turtle
import time
from tkinter import *
screen=turtle.Turtle()
screen=turtle.getscreen()
screen.register_shape("health.gif")
screen.penup()
screen.shape("health.gif")
screen.goto(x+50,y+150)
I don't know why you're getting that error... but you have a mixup with your variable names... you create a variable you call screen that is turtle.Turtle() but then you overwrite that variable by doing turtle.getscreen()
Doing:
import turtle
t=turtle.Turtle()
screen=t.getscreen()
screen.register_shape("health.gif")
t.penup()
t.shape("health.gif")
Draws a health.gif image (if that happens to be in your working directory)
so I'm trying to import a test .GIF file into my graphics window for Python 3.x and I can't seem to get it to work. So far I have:
from graphics import *
from tkinter import *
def main():
win = GraphWin ("Angry Birds!", 500,500)
angry = PhotoImage(250,250,'angry.gif')
angry.draw(win)
win.mainloop()
win.getMouse()
win.close()
main()
Yes I am in a class trying to learn but we never went over this and my professor neven mentioned downloading external packages like I've seen mentioned on this website of PIL. What am I missing?