在尝试将档案中的图像作为枕头对象保存到列表时,我遇到了一些问题。我正在使用python和jupyter笔记本的枕头库来处理图像。最初,我想列出一个词汇表,其中的一个键是一个枕头对象,这样我就可以轻松获得自己的图像(在我看来,这可能是我错了)。我一次将存档中的图像作为文件对象打开一次,然后将其作为枕头对象打开。在此步骤中,我想保存该枕头对象,以便以后使用。所有图像都很好,如果我在将它们附加到列表时使用显示,则会显示它们。但是有一件事对我来说看起来很奇怪。如果在将图像添加到列表时不显示图像,则从该列表中获取图像时将无法进一步显示它们。我希望你能帮助我。我想问题可能出在我对文件的处理上。也许有更好的方法来满足我的需求。
import zipfile
from PIL import Image
import pytesseract
import cv2 as cv
import numpy as np
#%%
file = zipfile.ZipFile('small_img.zip', 'r')
data = []
images = []
for name in file.namelist():
with file.open(name, 'r') as img_file:
image = Image.open(img_file)
data.append({'name': name, 'PilObject': image, 'text': None, 'bounding_boxes': None})
images.append(image)
#display(image)
#if I leave the line above I could display my images further, fetching them from the list
#If not, an error occurs
#%%
display(images[0])
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
d:\pythonprojects\untitled\venv\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
343 method = get_real_method(obj, self.print_method)
344 if method is not None:
--> 345 return method()
346 return None
347 else:
d:\pythonprojects\untitled\venv\lib\site-packages\PIL\Image.py in _repr_png_(self)
670 """
671 b = io.BytesIO()
--> 672 self.save(b, "PNG")
673 return b.getvalue()
674
d:\pythonprojects\untitled\venv\lib\site-packages\PIL\Image.py in save(self, fp, format, **params)
2122
2123 # may mutate self!
-> 2124 self._ensure_mutable()
2125
2126 save_all = params.pop("save_all", False)
d:\pythonprojects\untitled\venv\lib\site-packages\PIL\Image.py in _ensure_mutable(self)
616 def _ensure_mutable(self):
617 if self.readonly:
--> 618 self._copy()
619 else:
620 self.load()
d:\pythonprojects\untitled\venv\lib\site-packages\PIL\Image.py in _copy(self)
609
610 def _copy(self):
--> 611 self.load()
612 self.im = self.im.copy()
613 self.pyaccess = None
d:\pythonprojects\untitled\venv\lib\site-packages\PIL\ImageFile.py in load(self)
241 while True:
242 try:
--> 243 s = read(self.decodermaxblock)
244 except (IndexError, struct.error) as e:
245 # truncated png/gif
d:\pythonprojects\untitled\venv\lib\site-packages\PIL\PngImagePlugin.py in load_read(self, read_bytes)
861 self.__idat = self.__idat - read_bytes
862
--> 863 return self.fp.read(read_bytes)
864
865 def load_end(self):
~\AppData\Local\Programs\Python\Python37\lib\zipfile.py in read(self, n)
928 self._offset = 0
929 while n > 0 and not self._eof:
--> 930 data = self._read1(n)
931 if n < len(data):
932 self._readbuffer = data
~\AppData\Local\Programs\Python\Python37\lib\zipfile.py in _read1(self, n)
996 data = self._decompressor.unconsumed_tail
997 if n > len(data):
--> 998 data += self._read2(n - len(data))
999 else:
1000 data = self._read2(n)
~\AppData\Local\Programs\Python\Python37\lib\zipfile.py in _read2(self, n)
1028 n = min(n, self._compress_left)
1029
-> 1030 data = self._fileobj.read(n)
1031 self._compress_left -= len(data)
1032 if not data:
~\AppData\Local\Programs\Python\Python37\lib\zipfile.py in read(self, n)
751 "is an open writing handle on it. "
752 "Close the writing handle before trying to read.")
--> 753 self._file.seek(self._pos)
754 data = self._file.read(n)
755 self._pos = self._file.tell()
AttributeError: 'NoneType' object has no attribute 'seek'
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=3600x6300 at 0x24E8704B1C8>
答案 0 :(得分:0)
我很好奇,问题中的代码是否按原样工作?
因为似乎发生了各种奇怪的事情。
(由于缺少opencv软件包,我无法按原样进行测试。)
以此为基准,检查图像是否有效,
从档案中读取时。
import zipfile
from PIL import Image
archive = zipfile.ZipFile('media.zip', 'r')
fileNames = archive.namelist()
fileNames.pop(0) # Remove directory entry (here, 'media/').
# Opening the image files, could be as simple as this:
images = [Image.open(fName) for fName in fileNames]
# Test images, by displaying them with tkinter.
import tkinter as tk
from PIL import ImageTk
tk.Tk()
# Store references, to keep PhotoImages alive (for tkinter).
tkImages = [ImageTk.PhotoImage(img) for img in images]
for img in tkImages: tk.Label(image=img).pack()
tk.mainloop()
能否更新打开图像的循环,
根据这个,看看问题是否仍然存在?