Skip to content Skip to sidebar Skip to footer

How Do You Set A Turtle's Shape To A Pil Image

Quick question: Is it possible to register a python turtle shape from a PIL Image Object? Preferrably without saving it as a file on disk. NOTE: I am using multiple PIL Objects. An

Solution 1:

I've pieced together the steps needed to create a turtle cursor image using PIL (e.g. from a JPG). I embedded turtle in tkinter as using turtle standalone you can easily invoke PIL's ImageTk before Tk is setup and fail:

from tkinter import *
from turtle import TurtleScreen, RawTurtle, Shape
from PIL import Image, ImageTk

def register_PIL(name, image):
    photo_image = ImageTk.PhotoImage(image)
    shape = Shape("image", photo_image)
    screen._shapes[name] = shape  # underpinning, not published API

root = Tk()

canvas = Canvas(root, width=500, height=500)
canvas.pack(fill=BOTH, expand=YES)

screen = TurtleScreen(canvas)
turtle = RawTurtle(screen)

image = Image.open("test.jpg")
register_PIL("aardvark", image)
turtle.shape("aardvark")

# ...

screen.mainloop()

You may be able to do the same with with standalone turtle with care.

Post a Comment for "How Do You Set A Turtle's Shape To A Pil Image"