網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
python的ImageTk.PhotoImage大坑
如果大家遇到這樣的報(bào)錯(cuò):
Exception in Tkinter callback
Traceback (most recent call last):
? File "E:\Anaconda3_files\lib\site-packages\PIL\Image.py", line 2515, in fromarray
? ? mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 3), '<f8')During handling of the above exception, another exception occurred:
Traceback (most recent call last):
? File "E:\Anaconda3_files\lib\tkinter\__init__.py", line 1705, in __call__
? ? return self.func(*args)
? File "D:\Junior Spring\Digital Image Processing and Experiment\數(shù)字實(shí)驗(yàn)備份\結(jié)課實(shí)驗(yàn)\ImgProcessing.py", line 806, in Sobel_Sharpening
? ? image = ImageTk.PhotoImage(Image.fromarray(img))
? File "E:\Anaconda3_files\lib\site-packages\PIL\Image.py", line 2517, in fromarray
? ? raise TypeError("Cannot handle this data type")
TypeError: Cannot handle this data type
網(wǎng)上很多教程的方法我也試過(guò),沒(méi)有用,也調(diào)試不出為什么
這里有個(gè)很關(guān)鍵的信息:Cannot handle this data type
說(shuō)明是數(shù)據(jù)的類型錯(cuò)了,但再三檢查后,明明就是帶入的<class ‘numpy.ndarray’>類型
所以,大坑來(lái)了
請(qǐng)仔細(xì)檢查自己array里面每個(gè)數(shù)的類型,它必須是<class ‘numpy.uint8’>,否則就會(huì)報(bào)錯(cuò)
可以這樣改:
dst = dst.astype(np.uint8)
image = ImageTk.PhotoImage(Image.fromarray(dst))
Tkinter PhotoImage 踩坑記錄
1.直接使用PhotoImage(file= ‘xxxx’)報(bào)錯(cuò):_tkinter.TclError: couldn’t recognize data in image file “xxxxx.png”
原因:PhotoImage支持的圖片格式有限。
解決辦法:使用PILLOW庫(kù)的ImageTk
- 1.如果沒(méi)有安裝PILLOW插件,請(qǐng)安裝插件,使用 “pip install PILLOW”命令安裝即可
- 2.生成PhotoImage對(duì)象:
代碼:
from PIL import Image
from PIL import ImageTk
img = Image.open(filePath)
img = ImageTk.PhotoImage(img)
2.PhotoImage顯示問(wèn)題:顯示空白框,大小是圖片的真實(shí)大小
原因:見(jiàn)https://docs.Python.org/2/library/tkinter.html#images,說(shuō)白了就是圖像數(shù)據(jù)引用被回收了圖片就顯示不出來(lái)了,只會(huì)顯示一個(gè)空box。
解決辦法:保存PhotoImage對(duì)象即可,示例代碼如下:
代碼:
imgDict = {}
def getImgWidget(filePath):
? ? if os.path.exists(filePath) and os.path.isfile(filePath):
? ? ? ? if filePath in imgDict and imgDict[filePath]:
? ? ? ? ? ? return imgDict[filePath]
? ? ? ? img = Image.open(filePath)
? ? ? ? #print(img.size)
? ? ? ? img = ImageTk.PhotoImage(img)
? ? ? ? imgDict[filePath] = img
? ? ? ? return img
? ? return None
原文鏈接:https://blog.csdn.net/weixin_42815846/article/details/106864921
相關(guān)推薦
- 2022-05-03 Python?PyQt5學(xué)習(xí)之自定義信號(hào)_python
- 2022-08-19 redis在windows下啟動(dòng)# Creating Server TCP listening so
- 2022-07-04 Python中字典常用操作的示例詳解_python
- 2022-06-01 python畫立方體--魔方_python
- 2023-04-04 Python筆記之Scipy.stats.norm函數(shù)使用解析_python
- 2022-08-30 Spark中緩存和檢查點(diǎn)的區(qū)別
- 2023-07-26 node中的內(nèi)置模塊path和fs
- 2022-10-08 Pandas數(shù)據(jù)分析之pandas文本處理_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支