0

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. And with that I mean that I have multiple images loaded at the same time, so I need something that can work with multiple shapes at the same time. No images will be combined. (Just to clarify.)

Also: Is it possible to create a 'fake file' like stdout or something to do that? In general that would come in handy.

THIS IS NOT A DUPLICATE. it is not asking how to normally set the turtle shape, it is about how to set it to a PIL object. that is diffrent from a path string.

Timo Herngreen
  • 112
  • 1
  • 9
  • 1
    Stackoverflow is for helping you to get your code working rather than writing it for you. There is a much higher chance this question won't just be closed if you make an attempt yourself and then ask why it's not working. – holdenweb Dec 15 '18 at 15:44
  • 1
    @holdenweb it is not that I want my code written for me, I already have a lot of code, but I need this part. I actually searched for an answer for a year (from time to time), but i couldn't find it. EDIT: I have 94 lines of code already. – Timo Herngreen Dec 15 '18 at 15:49
  • 1
    and also, other people that are searching for this may need it. – Timo Herngreen Dec 15 '18 at 15:52
  • 1
    also if one doesn't have any information on how to do something, said person can't do said thing. So I can't try, – Timo Herngreen Dec 15 '18 at 15:54
  • So show the portion of the code that reads the `PIL` image, and the portion that makes the turtle draw, and ask how to make the turtle look like the image. Something like [this question](https://stackoverflow.com/questions/40447880/how-can-i-import-an-image-in-python-turtle), which essentially duplicates yours and may therefore help. – holdenweb Dec 15 '18 at 16:08
  • I do not want to DRAW the image, I want to add a PIL Image as a turtle shape with screen.addshape. @holdenweb – Timo Herngreen Dec 15 '18 at 16:12
  • OK, I've re-opened the question. – holdenweb Dec 15 '18 at 16:18
  • @holdenweb the PIL image does not come from a file! it comes from a generator. – Timo Herngreen Dec 15 '18 at 16:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185300/discussion-between-timo-herngreen-and-holdenweb). – Timo Herngreen Dec 15 '18 at 17:04

1 Answers1

0

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.

cdlane
  • 40,441
  • 5
  • 32
  • 81