当前位置:网站首页>Use opencv and OS or pathlib Path gets the path name and image name, and saves the image to the specified path
Use opencv and OS or pathlib Path gets the path name and image name, and saves the image to the specified path
2022-07-22 11:34:00 【Flying Bulldog】
os.path and pathlib.Path be similar , But the latter simplifies many operations , There are many advantages that the former does not have .
(1)os library
os Module provides a very rich way to handle files and directories .
Python Basic course , Link directly to :Python OS file / Catalog method | Novice tutorial (runoob.com)
among ,os.path() The module is used most frequently , It is usually used to get the attributes of files , There are many functions in it , This article only introduces :
- os.path.join() # Path synthesis
- os.path.split() # Path segmentation
(2)pathlib.Path modular
This module provides classes that represent file system paths , These classes have features that apply to different operating systems (Window、Linux、macOS) The semantics of the .
Get absolute path or relative path Part of the :
- # .name file name , Contains the suffix , If it is a directory, get the directory name
- # .stem file name , No suffix
- # .suffix Image suffix ( Format )
- # .parent Parent directory
- # .parents All parent directories
- # .anchor anchor , The front part of the directory , Absolute path (C:\) Or relative path (/)
(3) Code example ( With comments )
import os
import cv2
from pathlib import Path
files_path = "demo1"
out_path = "demo_out/"
# Traverse all files
files = os.listdir(files_path)
for i, file in enumerate(files):
print(file)
img = os.path.join(files_path, file) # Combine directory and filename into a path
image = cv2.imread(img)
# image = cv2.imread("{}/{}".format(files_path, str(file)))
cv2.imwrite(out_path + file, image)
image_path = r"G:\CSDN\python\Office_skills\path\demo1\1.png"
# Divide the path into dirname and basename, Returns a tuple
split_path = os.path.split(image_path)
print(split_path)
# .name file name , Contains the suffix , If it is a directory, get the directory name
# .stem file name , No suffix
# .suffix Image suffix ( Format )
# .parent Parent directory
# .parents All parent directories
# .anchor anchor , The front part of the directory , Absolute path (C:\) Or relative path (/)
path = Path(image_path)
print(type(path))
print(path)
print(path.name)
print(path.stem)
print(path.suffix)
print(list(path.parents)) # Tabular form
print(path.anchor)
# Read and save images
image1 = cv2.imread(image_path)
save_path = out_path + path.name
cv2.imwrite(save_path, image1)
>>> Path structure :
>>> If you have any questions , Welcome to the comments section to discuss
边栏推荐
- 利用Kaggle的API对数据集进行下载
- 推荐必读:测试人员如何快速熟悉新业务?
- Double pointer (I)
- 使用 Opencv and OS or pathlib.Path 获取路径名称和图像名称,并保存图像到指定路径
- BGP-边界网关协议
- MySQL insert ignore and replace into
- 双指针(一)
- systemd 管理 process-exporter
- Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection
- 【学习笔记】带你从0开始学习 01Trie
猜你喜欢
grafana 监控 node
Based on pytorch deep learning, remote sensing image feature classification and target detection, segmentation and remote sensing image problem deep learning optimization
Android interview question: tell me the difference between pendingintent and intent
调用“抱抱脸团队打造的Transformers pipeline API” && 通过预训练模型,快速训练和微调自己的模型
Example interview - Zeng yuluo: gain experience from lectures
Redis的主从复制
敲黑板!Kubernetes 架构核心知识点就这么多
How do we do full link grayscale on the database?
自定义类型:结构体(二)位段实现
Pyinstaller打包 && 关于Enum34的问题解决 && 降低版本
随机推荐
Is it safe to open an account on flush? How to buy REITs fund
标签平滑(LabelSmoothing)介绍与代码实现
Search for matching files or directories under folders glob ()
网络IP地址子网划分学习
TX2显存与内存之间数据传递原理
1million people are waiting in line! The public beta version of Dall · e is also charged
Paste in ckeditor does not work (your browser does not support pasting through the toolbar or right-click menu, please press ctrl+v to paste)
PHP利用Redis 事务实现商品秒杀
See and use & math operations & pytoch official document summary & Notes (V)
Routing policy-
Introduction aux noeuds
Time magazine's blockbuster cover: the meta universe era will change the world
利用Kaggle的API对数据集进行下载
grafana 监控 node
After the clothing ERP goes online, these problems must be paid attention to
同花顺的账户安全吗 中信证券开户佣金是多少
SYSTEMd manages redis exporter Linux
_TensorBase(45个张量基础后置函数总结) && Pytorch官方文件 && NOTEBOOK(NINE)
我擦\那些测试员面试中的“潜规则”,千万不要踩坑
BGP border gateway protocol