作者Lucemia (生の直感、死の予感)
看板Python
标题Re: [问题] 如何将图片载入
时间Fri Sep 28 10:14:24 2007
※ 引述《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)