Re: [問題] 如何將圖片載入

看板Python作者 (生の直感、死の予感)時間16年前 (2007/09/28 10:14), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/6 (看更多)
※ 引述《musicmen ()》之銘言: : ※ 引述《musicmen ()》之銘言: : : 求敎一下.小弟新手 : : 我用的是PIL Image的模組 : : import Image : : im = Image.open("pic.png") : : 它顯示結果如下..(感覺應該是沒有設定路徑的關係)所以抓不到圖片 : : Traceback (most recent call last): : : File "<interactive input>", line 1, in ? : : File "C:\Python24\Lib\site-packages\PIL\Image.py",line 1888, in open : : fp = __builtin__.open(fp, "rb")IO : : Error: [Errno 2] No such file or directory: 'Ping.png' : : 請問該如何設定? : 感謝大大的指導 : 已經可將圖片載入 : 不過目前又碰到另一個問題 : 我想一次loading約2000張圖片 : 不過Image.open指令似乎在500張的時候就會停住,顯示 : IOError: [Errno 24] Too many open files: 'C:/Python24/tepic/ping507.png' : 我的迴圈是 : list = [] : nu =2000 : for i in range(nu) : im =Image.open('C:/Python24/ping'+'str(i)'+'.png') : list.append(im) : 請問是500張是他loading的極限嗎?我的圖片每張都大約2kb : 如果一次要present 10000張圖片的是否有更好的寫法? 執行了 Image.open以後 你會發現那些被open過的圖檔不能夠被刪除 這是os 在底層負責處理file的機制。你的情況看起來是超過了os能夠 容忍的同時處理檔案數量。 http://www.pythonware.com/library/pil/handbook/image.htm open Image.open(infile) => image Image.open(infile, mode) => image Opens and identifies the given image file. This is a lazy operation; the actual image data is not read from the file until you try to process the data (call the load method to force loading). If the mode argument is given, it must be "r". You can use either a string (representing the filename) or a file object. In the latter case, the file object must implement read, seek, and tell methods, and be opened in binary mode. 從P.I.L 的 document, 首先他有提到open 是lazy的,所以光是open圖檔 圖檔內容不會被loading進去,需要執行load()去強迫load 另外os 也會一直保持檔案開啟的狀況,即使使用了load()將內容先讀入, Image物件雖然不需要檔案內容了,python 檔案開啟的狀態也不會立刻終止。 (file obj不會立即消滅) 總之有兩個簡單的作法: 1. 只存image data 部份 之後要使用時再產生image 物件來用 ths = [] # 存image data for ..... ths.append(Image.open('...').getData()) 2. 手動將open的file 關閉 ths = [] # 存image object for ..... i = Image.open('...') f = i.fp # file object i.load() f.close() ths.append(i) 另外 "list" 是python的關鍵字。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.110.216.37 ※ 編輯: Lucemia 來自: 140.110.216.37 (09/28 10:15)
文章代碼(AID): #16_6E1m1 (Python)
討論串 (同標題文章)
文章代碼(AID): #16_6E1m1 (Python)