如何在Python中将裁剪的图像放在Tkinter画布上 [英] How to put a cropped image on a Tkinter Canvas in Python

查看:346
本文介绍了如何在Python中将裁剪的图像放在Tkinter画布上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了很多类似的问题,但并没有解决此问题的方法: 我想

I found a lot of similar questions, but not quite the solution to this case : I want to

  1. 从磁盘加载图像文件
  2. 作物(懒惰与否)
  3. 将其放在TKinter画布上

哦,最好不要将第1步做成gif文件,但是即使必须这样做,我也会很高兴.就是这样.

And oh, it would even be better that step 1 would not need to be a gif-file, but even if it has to be I'll be happy. That's it..

我可以加载文件,可以裁剪(在PIL中),可以将其放置在画布上(在TKinter中),但是我似乎无法将所有内容组合在一起.到TKinter就足够了吗?)我当然是TKinter的新手.

I can load a file, I can crop it (In PIL) I can place it on a canvas (In TKinter), but I don't seem able to combine it all.. (So maybe a simple cast from PIL to TKinter is enough?) I'm a newbee in TKinter of course.

推荐答案

ImageTk PIL中的模块.

from Tkinter import *
from PIL import Image, ImageTk

root = Tk()
canvas = Canvas(root, width=500, height=500)
canvas.pack()

im = Image.open("image.png")
cropped = im.crop((0, 0, 200, 200))
tk_im = ImageTk.PhotoImage(cropped)
canvas.create_image(250, 250, image=tk_im)

root.mainloop()

这篇关于如何在Python中将裁剪的图像放在Tkinter画布上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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