1

I am generating fractal images, specifically Koch curves using turtle in Python. I intend to calculate the lacunarity of the image, for which I need that image in a 2D binary matrix form.

How do I convert that turtle output into a 2D binary matrix of 0s and 1s which I can use for further computation?

qwerty_uiop
  • 61
  • 1
  • 8
  • 1
    What have you tried so far? Where are you getting stuck exactly? What isn't working the way you expect? Please provide a minimal, complete and verifiable code example https://stackoverflow.com/help/mcve – Karl Dec 22 '18 at 08:14
  • I'm able to generate the images as turtle outputs. I'm stuck at (1) converting the turtle outputs into some format like jpg ot bmp, (2) importing those file formats as a 2d binary matrix I think my question is less about the code to do it, and more about whether such an inbuilt functionality exists and how to use it. – qwerty_uiop Dec 22 '18 at 08:24
  • look for imageio, pillow, scikit-image and https://stackoverflow.com/questions/4071633/python-turtle-module-saving-an-image or https://www.reddit.com/r/learnpython/comments/7wwtj5/saving_turtle_graphics_output_as_png_or_jgp/ or https://mail.python.org/pipermail/python-list/2007-April/449512.html or https://bytes.com/topic/python/answers/629332-saving-output-turtle-graphics – Joe Dec 22 '18 at 09:23
  • https://stackoverflow.com/questions/25050156/save-turtle-output-as-jpeg – Joe Dec 22 '18 at 09:24
  • When you say "2D binary matrix of 0s and 1s", you mean an image (a matrix of pixels), right? – martineau Dec 22 '18 at 12:09
  • @martineau I mean, I need a monochrome image and not a 24 bit RGB/8 bit grey or such formats. So I have a couple of bitmap images. I dont know what format they are. How do I convert them into a monochrome image? – qwerty_uiop Dec 22 '18 at 12:40
  • qwerty_uiop: I need to know exactly what format you want because the image turtle graphics produces is 24-bit RGB/8, so it needs to be converted. It doesn't have to be an image file format, and could be a 2D binary matrix (list-of-lists) of integer values whose bits represent pixel values—literally a "bitmap". However, if you want to save that into a file, some format for that needs to be defined—it could be done is more than one way. – martineau Dec 23 '18 at 17:01
  • I'm interested in how you calculated the lacunarity! – Shawn Jan 15 '19 at 22:47

1 Answers1

0

The script below creates a small tkinter app that draws something with turtle graphics when the Draw button is clicked. When the Save button was clicked, it saves the graphics into a bitmapped image file.

It uses the PIL (Python Image Library) module to convert the turtle graphics canvas into RGB image, and the converts that into a 1-bit per pixel bitmapped image.

Here's the turtlescreen.png bitmapped image file it creates:

turtlescreen.png image file

You've never indicated what the image format you want, so you may need to modify the script to create some other format—it currently creates a .png format image. The extension of the image's filename determines this.

from PIL import ImageGrab
import tkinter as tk
import tkinter.messagebox as tkMessageBox
import turtle

WIDTH, HEIGHT = 500, 400
IMG_FILENAME = 'turtlescreen.png'  # Extension determines file format.


def draw_stuff(canvas):
    screen = turtle.TurtleScreen(canvas)

    t = turtle.RawTurtle(screen.getcanvas())
    t.speed(0)  # Fastest.
    t.pencolor('black')
    t.hideturtle()

    # Draw a snowflake.
    size = 10

    def branch(size):
        for i in range(3):
            for i in range(3):
                t.forward(10.0*size/3)
                t.backward(10.0*size/3)
                t.right(45)
            t.left(90)
            t.backward(10.0*size/3)
            t.left(45)
        t.right(90)
        t.forward(10.0*size)

    # move the pen into starting position
    t.penup()
    t.forward(10*size)
    t.left(45)
    t.pendown()

    # Draw 8 branches.
    for i in range(8):
        branch(size)
        t.left(45)

    print('done')

def getter(root, widget):
    x = root.winfo_rootx() + widget.winfo_x()
    y = root.winfo_rooty() + widget.winfo_y()
    x1 = x + widget.winfo_width()
    y1 = y + widget.winfo_height()
    return ImageGrab.grab().crop((x, y, x1, y1))

def save_file(root, canvas, filename):
    """ Convert the Canvas widget into a bitmapped image. """
    # Get image of Canvas and convert it to bitmapped image.
    img = getter(root, canvas).convert('L').convert('1')
    img.save(IMG_FILENAME)  # Save image file.

    tkMessageBox.showinfo("Info", "Image saved as %r" % filename, parent=root)


# main
root = tk.Tk()
canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT,
                   borderwidth=0, highlightthickness=0)
canvas.pack()

btn_frame = tk.Frame(root)
btn_frame.pack()
btn1 = tk.Button(btn_frame, text='Draw', command=lambda: draw_stuff(canvas))
btn1.pack(side=tk.LEFT)
btn2 = tk.Button(btn_frame, text='Save',
                 command=lambda: save_file(root, canvas, IMG_FILENAME))
btn2.pack(side=tk.LEFT)
btn3 = tk.Button(btn_frame, text='Quit', command=root.quit)
btn3.pack(side=tk.LEFT)

root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301