直播源码网站,点击图片可进行任意方向旋转功能
直播源码网站,点击图片可进行任意方向旋转功能实现的相关代码
一、使用PIL库
在Python中, 实现turtle的图片旋转需要使用PIL库, 该库可通过pip安装。
使用PIL的Image.open函数可以实现加载图片; 使用ImageTk.PhotoImage可将PIL.Image对象转换为tkinter使用的类型。
PIL与tkinter实现的显示图片的程序如下:
from tkinter import *
from PIL import Image,ImageTk
root=Tk()
cv=Canvas(root,bg='black')
cv.pack(side=TOP,expand=True,fill=BOTH)
image=Image.open("bh.png","r")
image=image.resize((100,100))
imtk=ImageTk.PhotoImage(image)
id=cv.create_image(100,100,image=imtk)
cv.after(20,animate)
root.mainloop()
二、实现图片旋转
打开冗长的turtle模块的源代码, (读了半天)找到了TurtleScreenBase类, 该类用于底层的绘制图形等操作。程序的关键是使用自定义的函数替换turtle模块中原有的函数。
完整代码如下:
from turtle import *
from turtle import TurtleScreenBase
try:
from PIL import Image,ImageTk
except ImportError:
Image=None
images={}
# 使用自定义的函数替换turtle模块中原有的函数
def _image(self,filename):
img=Image.open(filename)
im = ImageTk.PhotoImage(img)
im.raw = img
im.zoomcache = [None,None]
return im
def _createimage(self, image):
"Create and return image item on canvas."
id = self.cv.create_image(0, 0, image=image)
return id
def _drawimage(self, item, pos, image, angle=None,zoom=None):
"Configure image item as to draw image object at position (x,y) on canvas)"
w=self.window_width();h=self.window_height()
if not (-h//2 < pos[1] < h//2\
and -w//2 <= -pos[0] < w//2):
self.cv.itemconfig(item, image=self._blankimage()) # 清除图像
return
prev=image
if zoom:
# zoomcache为列表, 格式为[<放大倍数>, <图像>], 用于储存图像放大后的副本
if zoom == image.zoomcache[0]:
image=image.zoomcache[1]
else:
raw=image.raw
size=(int(raw.size[0] * zoom), int(raw.size[1] * zoom))
raw = raw.resize(size,resample=Image.BILINEAR)
image=ImageTk.PhotoImage(raw)
image.raw=raw
prev.zoomcache=[zoom,image]
if angle is not None:
raw=image.raw
image=ImageTk.PhotoImage(raw.rotate(angle))
image.raw=raw
images[item]=image # 创建 img 的引用, 防止img消失
x, y = pos
self.cv.coords(item, (x * self.xscale, -y * self.yscale))
self.cv.itemconfig(item, image=image)
def register_shape(self, name, shape=None):
if shape is None:
if name.lower()[-3:] in (".gif","jpg","bmp","png"):
shape = Shape("image", self._image(name))
else:
raise TurtleGraphicsError("Bad arguments for register_shape.\n"
+ "Use help(register_shape)" )
# 从turtle模块复制的部分
elif isinstance(shape, tuple):
shape = Shape("polygon", shape)
## else shape assumed to be Shape-instance
self._shapes[name] = shape
# turtle的_drawturtle方法, 当Turtle的形状将要绘制时调用
def _drawturtle(self):
"""Manages the correct rendering of the turtle with respect to
its shape, resizemode, stretch and tilt etc."""
# 从turtle模块复制的部分
screen = self.screen
shape = screen._shapes[self.turtle.shapeIndex]
ttype = shape._type
titem = self.turtle._item
if self._shown and screen._updatecounter == 0 and screen._tracing > 0:
self._hidden_from_screen = False
tshape = shape._data
if ttype == "polygon":
if self._resizemode == "noresize": w = 1
elif self._resizemode == "auto": w = self._pensize
else: w =self._outlinewidth
shape = self._polytrafo(self._getshapepoly(tshape))
fc, oc = self._fillcolor, self._pencolor
screen._drawpoly(titem, shape, fill=fc, outline=oc,
width=w, top=True)
elif ttype == "image":
# 形状为图像时
screen._drawimage(titem, self._position, tshape,
self.heading(),self._stretchfactor[0])
elif ttype == "compound":
for item, (poly, fc, oc) in zip(titem, tshape):
poly = self._polytrafo(self._getshapepoly(poly, True))
screen._drawpoly(item, poly, fill=self._cc(fc),
outline=self._cc(oc), width=self._outlinewidth, top=True)
else:
if self._hidden_from_screen:
return
if ttype == "polygon":
screen._drawpoly(titem, ((0, 0), (0, 0), (0, 0)), "", "")
elif ttype == "image":
screen._drawimage(titem, self._position,
screen._shapes["blank"]._data)
elif ttype == "compound":
for item in titem:
screen._drawpoly(item, ((0, 0), (0, 0), (0, 0)), "", "")
self._hidden_from_screen = True
if Image:
TurtleScreenBase._image=_image
TurtleScreenBase._createimage=_createimage
TurtleScreenBase._drawimage=_drawimage
TurtleScreen.register_shape=register_shape
RawTurtle._drawturtle=_drawturtle
scr=getscreen()
scr.register_shape('blackhole.jpg')
shape('blackhole.jpg')
while True:
forward(60)
left(72)
done()
以上就是 直播源码网站,点击图片可进行任意方向旋转功能实现的相关代码,更多内容欢迎关注之后的文章