【发布时间】:2020-12-09 18:51:17
【问题描述】:
我正在使用这个code(@user2019716 的回答)将 .tif 转换为 .jpeg 以用于 tensorflow 对象检测 API。我之前已经完成了转换,没有任何问题,但今天出于某种原因,我收到了No such file or directory: '__0.tif' 错误,我不明白为什么会这样。我检查了我放入C:/Users/name/Desktop/phantom80_labelImg/TIF/ 的目录,并且有一个从__0.tif 到__34.tif 的.tif 文件列表。我知道该代码有效,因为我以前使用过它,但我不知道为什么它现在不读取文件 .tif 文件。
有什么建议吗?
import os
from PIL import Image
for infile in os.listdir("C:/Users/name/Desktop/phantom80_labelImg/TIF/"):
print("file : " + infile)
if infile[-3:] == "tif" or infile[-3:] == "bmp" :
# print "is tif or bmp"
outfile = infile[:-3] + "jpeg"
im = Image.open(infile)
print("new filename : " + outfile)
out = im.convert("RGB")
out.save(outfile, "JPEG", quality=90)
C:\Users\name\anaconda3\envs\MaskRCNN_SpeCraft\python.exe C:/Users/name/z/MaskRCNN_SpeCraft/tif_to_jpeg.py
Traceback (most recent call last):
File "C:/Users/name/z/MaskRCNN_SpeCraft/tif_to_jpeg.py", line 12, in <module>
im = Image.open(infile)
File "C:\Users\name\anaconda3\envs\MaskRCNN_SpeCraft\lib\site-packages\PIL\Image.py", line 2891, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '__0.tif'
C:\Users\name\z\MaskRCNN_SpeCraft
file : __0.tif
Process finished with exit code 1
【问题讨论】:
-
将路径名中的 / 替换为 // 可以吗?
-
@AdityaChavan 不,我收到同样的错误。
-
为了便于访问,请将您的代码和错误替换为文本。
-
注释掉其余代码,看看 print(infile) 行是否打印文件名
-
如果您尝试保存到完整路径,而不仅仅是文件名,这行得通吗?即不仅仅是
outfile做'C:/whatever/' + outfile
标签: python pycharm python-imaging-library jpeg tiff