【问题标题】:TIF to JPEG Conversion - No such file or directoryTIF 到 JPEG 转换 - 没有这样的文件或目录
【发布时间】: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


【解决方案1】:

os.listdir(dir) 仅返回该目录中的文件名 (documentation)。

要打开文件,您需要获取完整的文件路径。你可以使用os.path.join(documentation)

root_dir = "C:/Users/name/Desktop/phantom80_labelImg/TIF/"
for filename in os.lisdir(root_dir):
    infile = os.path.join(root_dir, filename)
    # rest of your code

【讨论】:

  • 是的,这是我刚刚尝试的下一件事,它奏效了。感谢您的文档!
猜你喜欢
  • 2018-11-20
  • 2021-06-24
  • 1970-01-01
  • 2015-02-20
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多