图像操作不适用于多处理

时间:2018-06-26 07:34:34

标签: python multiprocessing python-imaging-library

我之前发布了一个问题,出于类似原因,该问题的代码无法正常工作。它并没有引起太大的注意,所以我删除了它,并发现了问题的最小一部分。

考虑代码:

from multiprocessing import Pool
from PIL import Image 
import PIL.TiffImagePlugin as tiff 
def f(im,i):
    im.seek(i)
    print(i) 
manual = False  
if (__name__=='__main__' and not manual):
    p = Pool()
    im = Image.open('page.tiff') 
    im.load()
    print(p.starmap(f,[(im,0),(im,1),(im,2),(im,3)]))
if(manual): 
    im = Image.open('page.tiff') 
    f(im,0) 
    f(im,1)
    f(im,2)
    f(im,3)

如果manual = True(即不使用任何multiprocessing),则输出为:

C:\Users\H.P\Downloads\states\General>python mptTest.py
0
1
2
3

如果manual = False,则出现错误:

C:\Users\H.P\Downloads\states\General>python mptTest.py
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
  File "C:\Users\H.P\AppData\Local\Programs\Python\Python36\lib\multiprocessing\pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "C:\Users\H.P\AppData\Local\Programs\Python\Python36\lib\multiprocessing\pool.py", line 47, in starmapstar
    return list(itertools.starmap(args[0], args[1]))
  File "C:\Users\H.P\Downloads\states\General\mptTest.py", line 5, in f
    im.seek(i)
  File "C:\Users\H.P\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\TiffImagePlugin.py", line 998, in seek
    if not self._seek_check(frame):
  File "C:\Users\H.P\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\ImageFile.py", line 276, in _seek_check
    if (frame < self._min_frame or
AttributeError: 'TiffImageFile' object has no attribute '_min_frame'
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "mptTest.py", line 15, in <module>
    print(p.starmap(f,[(im,0),(im,1),(im,2),(im,3)]))
  File "C:\Users\H.P\AppData\Local\Programs\Python\Python36\lib\multiprocessing\pool.py", line 274, in starmap
    return self._map_async(func, iterable, starmapstar, chunksize).get()
  File "C:\Users\H.P\AppData\Local\Programs\Python\Python36\lib\multiprocessing\pool.py", line 644, in get
    raise self._value
AttributeError: 'TiffImageFile' object has no attribute '_min_frame'

_min_frame源自__init__文件的ImageFile.py。因此,在调用它时,它必须已经调用了__init__()方法。

为什么会这样?这是PIL中的错误吗?

0 个答案:

没有答案