Guhongying

今天在学python时,在模仿一个为图片加上图标并移动到指定文件夹的程序时遇到“FileNotFoundError: [Errno 2] No such file or directory: \'1528712892362.jpg\'”这个问题。

讲讲解决方案,因为我要处理的十几张图片是位于 ‘F:\图片’ 这个路径下的,但是读取到具体的某一个文件时忘记使用绝对路径,如果没有为文件加上前面的绝对路径的话,我的理解是默认在程序所在的路径作为当前路径来查找的,所以就会报错说找不到文件。

后来加上代码中的第10行,并在16行中稍作修改就行了。

 1 import os
 2 from PIL import Image
 3 SQUARE_FIT_SIZE=500
 4 LOGO_FILENAME=\'catlogo.png\'
 5 logoIm=Image.open(LOGO_FILENAME)
 6 width1,height1=logoIm.size
 7 logoIm1=logoIm.resize((int(width1/5),int(height1/5)))
 8 logoWidth,logoHeight=logoIm1.size
 9 logoIm=logoIm1
10 root=\'F:/图片/\'
11 os.makedirs(\'withLogo\',exist_ok=True)
12 for filename in os.listdir(\'F:\图片\'):
13     print(filename)
14     if not (filename.endswith(\'.png\') or filename.endswith(\'.jpg\')) or filename==LOGO_FILENAME:
15         continue
16     im=Image.open(root+filename)
17     width,height=im.size
18     print(\'Resizing %s...\'%(filename))
19     im=im.resize((width,height))
20     print(\'Adding logo to %s...\'%(filename)) 
21     im.paste(logoIm,(width-logoWidth,height-logoHeight),logoIm)
22     im.save(os.path.join(\'withLogo\',filename))

 

分类:

技术点:

相关文章: