Advertisement
j0h

turtle2svg.py

j0h
Feb 9th, 2024 (edited)
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import turtle
  3. import time
  4. import os
  5. """
  6. I couldnt make svg_turtle work, so I did some hacky bs instead
  7. anyway, we hide the turtle cursor, get the canvas, save the file
  8. then show the currsor again. this prevents the curror from
  9. appearing in the saved image
  10.  
  11. This program saves the turtle canvas as an svg.
  12. and a post script file, (whatever rm it)
  13. as the current unix time.
  14.  
  15. ep2svg is provided by the geg package
  16.  
  17. """
  18.  
  19. # Draw something with the turtle
  20. turtle.forward(100)
  21. turtle.right(90)
  22. turtle.forward(100)
  23. turtle.right(90)
  24. turtle.forward(100)
  25. turtle.right(90)
  26. turtle.forward(100)
  27.  
  28. # Get the current Unix time
  29. current_time = int(time.time())
  30.  
  31. print("Current Unix time:", current_time)
  32. #hide the turtle for file write
  33. turtle.hideturtle()
  34. # Get the turtle canvas as a string
  35. canvas_data = turtle.getcanvas().postscript()
  36.  
  37. # Construct the file name with the current time and the .ps extension
  38. ps_file_name = f"{current_time}.ps"
  39. svg_file_name = f"{current_time}.svg"
  40.  
  41. # Write the canvas data to a PostScript file
  42. with open(ps_file_name, 'w') as f:
  43.     f.write(canvas_data)
  44.  
  45. # Show the turtle again
  46. turtle.showturtle()
  47.  
  48.  
  49. # Convert the PostScript file to SVG
  50. os.system('eps2svg ' + ps_file_name + ' ' + svg_file_name)
  51. # Close the turtle graphics window
  52. turtle.done()
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement