python常用代码总结

python常用代码总结

一、常用

1、新建文件夹,并将特定的文件复制到文件夹下

# 遍历img_path下的图片,以图片名称新建文件夹,保存到path下,然后复制yaml_path,到每一个文件夹下
import os
import shutil
import numpy as np

img_path = 'I:/xj/Mask_RCNN_dataset_lane/ori'
path = 'I:/xj/Mask_RCNN_dataset_lane/json_To_dataset/'
yaml_path = 'I:/xj/Mask_RCNN_dataset_lane/info.yaml'

for filename in os.listdir(img_path):
    if filename.endswith(".png"):
        folder_path = path + filename[0:-4]
        os.makedirs(folder_path)
        shutil.copy(yaml_path, folder_path)# 将 .yaml 文件复制到 folder_path
        print (folder_path)

2、get_path获取路径的几种方式

2.1、使用使用os.walk()函数

import os
import glob
# 使用os.walk()函数
for root, dirs, files in os.walk("E:\\work_diary\\python_code\\get_path"):
    # for name in dirs:
    #     print(os.path.join(root, name))  #整个子文件夹的目录
    # for name in files:
        # print(os.path.join(root, name))  #整个子文件的目录
    for name in files:
        if os.path.splitext(name)[1] == '.txt':
            print(name)
            print(os.path.join(root,name))  #输出想要的特定文件目录

2.2、使用glob函数

--golb函数,只能查找当前文件夹,不能遍历子文件
for ori_imgfile in glob.glob("E:\\work_diary\\python_code\\get_path\\"+"*.txt"):
    print(ori_imgfile)

2.3、os.listdir()函数

for filename in os.listdir(annotations_dir):
    if filename.endswith(".txt"):
        filepath = os.path.join(annotations_dir,filename)  #指定.txt文档路径
        imgname = filename[0:-4] + ".jpg"
        imgpath = os.path.join(input_dir,imgname)  #获取对应的 .png图像路径
        image = cv2.imread(imgpath)  #读取图像

3、拆分字符串

roiname = "12234_L.jpg"
splitnum0 = roiname.rindex("L")
splitnum1 = roiname.rindex("_")
splitnum2 = roiname.rindex(("."))
anno_filename = roiname[0:splitnum0+1] + ".txt"

4、程序计时time

import time
start_time = time.time()
...
...
end_time = time.time()
print (end - start)

5、判断文件夹 文件是否存在

#判断文件夹
import os
dirs = '/Users/joseph/work/python/'

if not os.path.exists(dirs):
    os.makedirs(dirs)
#判断文件
import os
filename = '/Users/joseph/work/python/poem.txt'

if not os.path.exists(filename):
    os.system(r"touch {}".format(path))#调用系统命令行来创建文件

二、图像常用

1、findcontours/drawcontours函数

https://blog.csdn.net/hjxu2016/article/details/77833336

contours, hierarchy = cv2.findContours(img_create, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# cv2是两个输出结果,cv3好像是三个输出结果

2、python 读取图像,宽高,resize,零矩阵定义

# 图片处理
    img_ori = cv2.imread(path_img_ori) # 读取原图
    img_cabinet = cv2.imread(path_img_cabinet) # 读取cabinet图
    print("img_cabinet_size**********: ", img_cabinet.shape)
    height, width = img_cabinet.shape[0:2]
    img_cabinet_resize = cv2.resize(img_cabinet,(int(width/scale_size), int(height/scale_size)),interpolation=cv2.INTER_NEAREST) # 缩放原图
    img_create = np.zeros((int(height/scale_size), int(width/scale_size), 1), np.uint8) # 图片新建
    img_create.fill(255) # 填充特定像素值

3、遍历图像

RGB格式:

for row in range(img_cabinet_resize.shape[0]):
    for col in range(img_cabinet_resize.shape[1]):
        x = img_cabinet_resize[row][col][0]
        y = img_cabinet_resize[row][col][1]
        z = img_cabinet_resize[row][col][2]
        if (x==0 and y==220 and z==220):
            img_create[row][col] = 225

4、取轮廓的矩形框 rect

rect = cv2.boundingRect(contours[i])# 包含四个变量,分别为左上角坐标和宽高
xmin = rect[0] * scale_size
ymin = rect[1] * scale_size
xmax = xmin + rect[2] * scale_size
ymax = ymin + rect[3] * scale_size
cv2.rectangle(img_ori, (xmin, ymin), (xmax, ymax), (0, 255, 255), 2)

5、图片PIL格式转numpy格式

首先确定 PIL具体是那种格式:比如 mask 图片 mask.mode()
在这里插入图片描述

cabinet_mask = np.array(mask)
or
cabinet_mask = cv2.cvtColor(np.asarray(mask), cv2.COLOR_RGB2BGR)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值