你如何将海龟的形状设置为 PIL 图像 [英] How do you set a turtle's shape to a PIL image

查看:54
本文介绍了你如何将海龟的形状设置为 PIL 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速问题:是否可以从 PIL 图像对象注册 python 海龟形状?最好不要将其保存为磁盘上的文件.

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.

注意:我使用了多个 PIL 对象.我的意思是我同时加载了多个图像,所以我需要可以同时处理多个形状的东西.不会合并任何图像.(只是为了澄清.)

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.)

另外:是否可以创建一个像 stdout 那样的假文件"或其他东西来做到这一点?一般来说,这会派上用场.

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

这不是重复的.它不是问如何正常设置海龟形状,而是关于如何将其设置为 PIL 对象.这与路径字符串不同.

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.

推荐答案

我已经拼凑了使用 PIL(例如从 JPG)创建海龟光标图像所需的步骤.我在 tkinter 中嵌入乌龟作为使用乌龟独立,您可以在 Tk 设置和失败之前轻松调用 PIL 的 ImageTk:

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.

这篇关于你如何将海龟的形状设置为 PIL 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆