gpt4 book ai didi

python - 当路径由 os.path.join 创建时打开图像文件

转载 作者:太空宇宙 更新时间:2023-11-03 15:48:23 24 4
gpt4 key购买 nike

我使用两个不同的脚本。在第一个中,有这样的内容:

f = open(filename, 'r')

file, file_ext = os.path.splitext(filename)

thumb=open(file +"_thumb.txt","w")

for line in f:
array = line.split(',')

a = str(array[0])

t=a[11:14]+ "\\" + a[15:19] + "\\" + (a[11:])+".jpg" +"\n"

thumb.write(t)

thumb.close()

在第二个中:

Dirname = str(self.lneDirIn1.text())

f=open(file +"_thumb.txt","r")

for line in f:
line=str(line)

print(line)

cl_img_path=os.path.normpath((os.path.join(Dirname,line)))

print(cl_img_path)

cl_img=Image.open(str(cl_img_path))

当我运行第二个时,出现错误,因为os.path.join实际上加入了该行的“\n”,因此无法打开cl_img 。但是,当我单独打印“行”时,它不会显示“\n”

这是错误:

Traceback (most recent call last):
File "./midas/mds_central_line_thumbs.py", line 118, in pbtOKClicked
self.process()
File "./midas/mds_central_line_thumbs.py", line 105, in process
cl_img=Image.open(str(cl_img_path))
File "C:\0adtoolsv2\libs\Python27\lib\site-packages\PIL\Image.py", line 1952, in open
fp = __builtin__.open(fp, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename: 'k:\\SBU_3\\USA\\PIO2015\\04-TEST-SAMPLES\\USCASFX1608\\D16MMDD\\B3\\Images\\051\\0151\\051_0151_00021466.jpg\n'

我希望我的第二个脚本在打开文件时不考虑“\n”(第一个脚本中必需的)

非常感谢你,纪尧姆。

最佳答案

读取该行时去掉“\n”怎么样?

line=str(line).strip()

或者加入路径时?

cl_img_path=os.path.normpath((os.path.join(Dirname, line.strip())))

或者打开图像时?

cl_img=Image.open(str(cl_img_path).strip())

关于python - 当路径由 os.path.join 创建时打开图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41567241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com