python

os创建路径

if not os.path.exists(cut_ann_root):
    os.makedirs(cut_ann_root)

os获取当前文件所在路径

os.path.dirname(__file__)

os.listdir()在linux系统下读取会乱序,需要排序:sorted(os.listdir(path)):
数字排序:

path_list.sort(key=lambda x:int(x[:-4]))
#1.jpg 2.jpg ..... 10.jpg....

os.path.splitext 去除扩展名

path1='asd/fgh/image.jpg'
print(os.path.splitext(path1))
#输出'asd/fgh/image'

os.path.basename 返回文件名

path1='asd/fgh/image.jpg'
print(os.path.basename(path1))
#输出'image.jpg'

拷贝文件

from shutil import copyfile
from sys import exit

source = 'D:\\testData\\123.png'  # 源路径
target = 'D:\\123.png'           # 目标路径 
# adding exception handling
try:
   copyfile(source, target)   # copyfile 函数调用
except:
   print('copy file faild')

去除字符串末尾空格和替换

str1 = str0.strip()
import re
str2 = re.sub(' ','',str0)

拼接字符串

a = '!@'.join(['Fusion', 'Sphere', 'Cloud'])
'Fusion!@Sphere!@Cloud'

读txt

with open(path, 'r') as f:
	self.annotations = f.read().split("\n")  #换行

	for line in f.readlines(): #遍历单行
    	print(line.strip()) # 把末尾的'\n'删掉

写txt

with open('train.txt','w',encoding='utf-8') as f:  #续写的话把'w'换成'a'
	f.write('hellow')
    f.write('\n')  #换行

转float32:

a = a.astype(np.float32)

list深拷贝

list2 = list1.copy()

Imgae.open
Image.open()函数只是保持了图像被读取的状态,但是图像的真实数据并未被读取,因此如果对需要操作图像每个元素,如输出某个像素的RGB值等,需要执行对象的load()方法读取数据。具体如下:

img = Image.open("lena.jpg") 
img = img.load() 
print(img[0,0])

io.imread和从cv.imread:前者是RGB,后者是BGR
PILImage的图像读取及通道拼接

img_c=Image.open(img_path1).resize((224,224))
img = Image.merge('RGB', (img_c, img_c, img_c))

PILImage通道分离、转numpy、reshape

r,g,b=img.split()
# 将r g b转换为一维数组
r_array=np.array(r).reshape(40000)

时间转为unix时间戳

import time
sys_time = int(time.mktime(time.strptime(date_str + " " + time_str, "%Y-%m-%d %H:%M:%S"))* 1e9 + int(det)*1e3)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值