When trying to convert a list of .tif/.tiff files I run into a key error that is thrown from PIL package. I'm running the Anaconda diet on a Mac. I haven't been able to find many answers on why this happens. I can see that the list of images is 6 but it appears it is trying to continue to iterate on this list when it is at the end of the list.
I also was trying to only look for tif files but the commented out part doesn't seem to work.
Here's the code block in question.
imgs = []
for fname in os.listdir(storePath):
#if not fname.endswith(".tif") or fname.endswith(".tiff"):
#continue
path = os.path.join(storePath, fname)
if os.path.isdir(path):
continue
print(path)
imgs.append(path)
with open("name.pdf","wb") as f:
f.write(img2pdf.convert(imgs))
Here's the error stack:
setting <module 'PIL.TiffImagePlugin' from '/Users/userName/opt/anaconda3/lib/python3.9/site-packages/PIL/TiffImagePlugin.py'>.STRIP_SIZE = 5625
KeyError Traceback (most recent call last)
/var/folders/22/86x9q83n67j39t2kwfy970w40000gn/T/ipykernel_61196/4104548731.py in <module>
9 imgs.append(path)
10 with open("name.pdf","wb") as f:
---> 11 f.write(img2pdf.convert(imgs))
~/opt/anaconda3/lib/python3.9/site-packages/img2pdf.py in convert(*images, **kwargs)
2358 rotation,
2359 iccp,
-> 2360 ) in read_images(
2361 rawdata,
2362 kwargs["colorspace"],
~/opt/anaconda3/lib/python3.9/site-packages/img2pdf.py in read_images(rawdata, colorspace, first_frame_only, rot)
1728 while True:
1729 try:
-> 1730 imgdata.seek(img_page_count)
1731 except EOFError:
1732 break
~/opt/anaconda3/lib/python3.9/site-packages/PIL/TiffImagePlugin.py in seek(self, frame)
1099 if not self._seek_check(frame):
1100 return
-> 1101 self._seek(frame)
1102 # Create a new core image object on second and
1103 # subsequent frames in the image. Image may be
~/opt/anaconda3/lib/python3.9/site-packages/PIL/TiffImagePlugin.py in _seek(self, frame)
1141 self.tag = self.ifd = ImageFileDirectory_v1.from_v2(self.tag_v2)
1142 self.__frame = frame
-> 1143 self._setup()
1144
1145 def tell(self):
~/opt/anaconda3/lib/python3.9/site-packages/PIL/TiffImagePlugin.py in _setup(self)
1311
1312 # extract relevant tags
-> 1313 self._compression = COMPRESSION_INFO[self.tag_v2.get(COMPRESSION, 1)]
1314 self._planar_configuration = self.tag_v2.get(PLANAR_CONFIGURATION, 1)
1315
KeyError: 34712