Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 a neat picture based on recursive number
#1
i need to save an image created by turtle module
here's my code
import turtle

recurse = [""]*1000000
recurse[2] = "<>"
n = 2
p = 1
while n < len(recurse):
   for v in range(2,int(len(recurse)/n)):
      if recurse[v] != "":
         recurse[v*n] = recurse[v] +recurse[n]
	  
   n += 1
   while n<len(recurse) and recurse[n] != "":
      n += 1
   if n == len(recurse):
      break
   p += 1
   recurse[n] = "<" +recurse[p] +">"
turtle.screensize(3000,3000)
turtle.up()   
turtle.sety(-200)
turtle.down()
turtle.speed(0)
turtle.hideturtle()
for i in range(2,400):
   for j in range(0,len(recurse[i])):
      if recurse[i][j] == ">":
         turtle.right(60)
         turtle.forward(5)
      else:
         turtle.left(30)
         turtle.forward(5)
   print(recurse[i],i)

input()
but i want to save the image as a png. so far i can only use the print screen button.
heres an image that i used a couple print screens on https://ibb.co/kPRY2z
Quote
#2
1) You can save a turtle canvas as a post script file, e.g. turtle.getscreen().getcanvas().postscript(file='yourfile.ps')

Another approach that doesn't require to change the code is to override turtle object (and use e.g. Pillow package for image generation):


import turtle
import math
from PIL import Image, ImageDraw

recurse = [""]*1000000
recurse[2] = "<>"
n = 2
p = 1
while n < len(recurse):
   for v in range(2,int(len(recurse)/n)):
      if recurse[v] != "":
         recurse[v*n] = recurse[v] +recurse[n]
   n += 1
   while n<len(recurse) and recurse[n] != "":
      n += 1
   if n == len(recurse):
      break
   p += 1
   recurse[n] = "<" +recurse[p] +">"
   
   
class Turtle:

    def __init__(self, size=(1000, 1000), position=(0,0), output=None, format="PNG", imagetype='1', fillcolor=1):

        for k, w in locals().items():
            setattr(self, k, w)
        
        self._init_image()
    
    def _init_image(self):
        self.image = Image.new(self.imagetype, self.size)
        self.draw = ImageDraw.Draw(self.image)
    
    
    def up(self):
        self.angle = math.pi / 2.0
    
    def down(self):
        self.angle = 2.0 * math.pi - math.pi / 2.0
    
    def right(self, angle):
        self.angle += angle / 180.0 * math.pi
    
    def left(self, angle):
        self.angle -= angle / 180.0 * math.pi
    
    def forward(self, length):
        x1, y1 = self.position 
        x2, y2 = x1 + float(length) * math.cos(self.angle), y1 + float(length) * math.sin(self.angle)
        self.draw.line((x1, y1, x2, y2), fill=self.fillcolor)
        self.position = (x2, y2)
    
    def save(self):
        if self.output:
            self.image.save(self.output, self.format)
            
    def screensize(self, w, h):
        self.size = (w, h)
        self._init_image()

    def setx(self, x):
        self.position = (x, self.position[1])
    
    def sety(self, y):
        self.position = (self.position[0], y)
    
    def speed(self,val):
        pass

    def hideturtle(self):
        pass
    
    @property
    def getx(self):
        return self.position[0]
    
    @property
    def gety(self):
        return self.position[1]


turtle = Turtle(output='output.png', position=(1000, 1000))

turtle.screensize(3000, 3000)
turtle.up()   
#turtle.sety(-200)
turtle.down()
turtle.speed(0)
turtle.hideturtle()
for i in range(2,400):
   for j in range(0,len(recurse[i])):
      if recurse[i][j] == ">":
         turtle.right(60)
         turtle.forward(5)
      else:
         turtle.left(30)
         turtle.forward(5)
      print(turtle.getx, turtle.gety) # for debugging purposes 
   print(recurse[i],i)
   
 
turtle.save()

input()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary kennybassett 2 1,048 Oct-02-2018, 02:05 PM
Last Post: Alfalfa
  tkinter picture importation problema SheeppOSU 1 747 May-16-2018, 09:26 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)