python如何判断图片路径是否存在

834次阅读
没有评论

python如何判断图片路径是否存在

1、在向文件夹中保存数据前,先判断该文件夹(路径)是否存在。

save_path = '/root/.../image/result'
if not os.path.exists(save_path):
    os.makedirs(save_path)

本来路径里只有到image文件夹的,执行完后会自动在image下创建result文件夹。

2、在打开某些图片时,可以先判断该图片的路径是否存在。

if img == 'target7.jpg':
    img_path = os.path.join(dir_path, img)
    if imghdr.what(img_path):  # 判断是否能打开
        target_img = Image.open(img_path)
        target_img.save(os.path.join(root, 'trainB/{}_B.jpg'.format(count+6)))
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-06-22发表,共计451字。
新手QQ群:570568346,欢迎进群讨论 Python51学习