跳至主要內容
opencv video 相关参数
    cv2.VideoCapture.get(0) 视频文件的当前位置(播放)以毫秒为单位
    cv2.VideoCapture.get(1) 基于以0开始的被捕获或解码的帧索引
    cv2.VideoCapture.get(2) 视频文件的相对位置(播放):0=电影开始,1=影片的结尾。
    cv2.VideoCapture.get(3) 在视频流的帧的宽度
    cv2.VideoCapture.get(4) 在视频流的帧的高度
    cv2.VideoCapture.get(5) 帧速率
    cv2.VideoCapture.get(6) 编解码的4字-字符代码
    cv2.VideoCapture.get(7) 视频文件中的帧数
    cv2.VideoCapture.get(8) 返回对象的格式
    cv2.VideoCapture.get(9) 返回后端特定的值,该值指示当前捕获模式
    cv2.VideoCapture.get(10) 图像的亮度(仅适用于照相机)
    cv2.VideoCapture.get(11) 图像的对比度(仅适用于照相机)
    cv2.VideoCapture.get(12) 图像的饱和度(仅适用于照相机)
    cv2.VideoCapture.get(13) 色调图像(仅适用于照相机)
    cv2.VideoCapture.get(14) 图像增益(仅适用于照相机)(Gain在摄影中表示白平衡提升)
    cv2.VideoCapture.get(15) 曝光(仅适用于照相机)
    cv2.VideoCapture.get(16) 指示是否应将图像转换为RGB布尔标志
    cv2.VideoCapture.get(17) × 暂时不支持
    cv2.VideoCapture.get(18) 立体摄像机的矫正标注(目前只有DC1394 v.2.x后端支持这个功能

爱喝水的木子...大约 1 分钟pythoncvpython
opencv 画图相关

opencv 提供了绘制直线、圆形、矩形等基本绘图的功能

1、绘直线

cv2.line(画布,起点坐标,终点坐标,颜色,宽度) 例如: cv2.line(image,(20,60),(300,400),(0,0,255),2)

2、绘矩形

cv2.rectange(画布,起点,终点,颜色,宽度) 若宽度大于0,标识边线宽度;如果小于0,表示画实心矩形 cv2.rectange(image,(20,60),(300,400),(255,0,0),-1)

3、绘圆形

cv2.circle(画布,圆心坐标,半径,颜色,宽度) 若宽度大于0,标识边线宽度;如果小于0,表示画实心圆行 cv2.circle(image,(300,300),40,(0,255,0),2)


爱喝水的木子...大约 1 分钟pythonopencvpython
python常见的加解密

urlencode加密

# urlencode加密
import urllib.parse

text = "我爱吃鸡腿"
s = urllib.parse.quote(text)
print(s) # %E6%88%91%E7%88%B1%E5%90%83%E9%B8%A1%E8%85%BF
u = urllib.parse.unquote(s)
print(u) #我爱吃鸡腿

爱喝水的木子...大约 8 分钟pythonaespython
使用cv统计视频时长
import cv2,os
# 统计视频
def count_video_duration(file_path):
    cap = cv2.VideoCapture(file_path)
    if cap.isOpened():  # 当成功打开视频时cap.isOpened()返回True,否则返回False
        # get方法参数按顺序对应下表(从0开始编号)
        rate = cap.get(5)  # 帧速率
        FrameNumber = cap.get(7)  # 视频文件的帧数
        duration = FrameNumber / rate
        return int(duration)
    else:
        return 0

# 递归获取视频路径进行统计时长
def count_video_duration_recursion(file_path):
    global duration_video
    if os.path.isdir(file_path):
        for file in os.listdir(file_path):
            count_video_duration_recursion(os.path.join(file_path, file))
    else:
        if file_path.endswith('.mp4') or file_path.endswith('.avi')or file_path.endswith('.3gp'):
            duration = count_video_duration(file_path)
            duration_video+=duration
            print(file_path, duration)
duration_video = 0
path = "路径"
count_video_duration_recursion(path)
print(duration_video)


爱喝水的木子...小于 1 分钟pythonpythoncv文件视频
图片旋转摆正根据图片的exif信息
from PIL import Image
import os


# 图片旋转
def image_route(in_file, out_file):
    img = Image.open(in_file)
    dict_exif = img.getexif()
    try:
        res = dict_exif[274]
        if res == 1:
            img = img.rotate(0, expand=True)
        elif res == 8:
            print("向右", in_file)
            img = img.rotate(90, expand=True)
        elif res == 6:
            print("向左", in_file)
            img = img.rotate(-90, expand=True)
        elif res == 3:
            print("翻转", in_file)
            img = img.rotate(180, expand=True)
        else:
            print(res, "不正常", in_file)
            img = img.rotate(0, expand=True)
        img.save(out_file)
    except:
        print("error:", in_file)
        img = img.rotate(0, expand=True)
        img.save(os.path.join(_save, i))


if __name__ == '__main__':
    # 要调整的路径
    path = r"文件路径"
    # 调整之后保存的路径
    _save = r"文件路径"
    for i in os.listdir(path):
        in_file = os.path.join(path, i)
        out_file = os.path.join(_save, i)
        image_route(in_file, out_file)


爱喝水的木子...小于 1 分钟pythonpython图片旋转exif
对比两个txt获取共同的字
import chardet


# 获取文件编码
def get_file_encode(file_path):
    """
    获取文件编码
    :param file_path: 文件路径
    :return: 文件编码
    """
    with open(file_path, 'rb') as f:
        return chardet.detect(f.read())['encoding']


# 获取文件内容
def get_file_content(file_path):
    """
    获取文件内容
    :param file_path: 文件路径
    :return: 文件内容
    """
    # 临时存储列表
    list_data = []
    with open(file_path, 'r', encoding=get_file_encode(file_path)) as f:
        for line in f.readlines():
            for word in line.replace("\n", ''):
                if word not in list_data:
                    list_data.append(word)
    return list_data


# 对比两个列表找到相同的值
def compare_list(list_1, list_2):
    """
    对比两个列表找到相同的值
    :param list_1: 列表1
    :param list_2: 列表2
    :return: 相同的值
    """
    list_3 = []
    for i in list_1:
        if i in list_2:
            list_3.append(i)
    return list_3


# 将列表数据写入到结果中
def write_result(list_data, save_result):
    """
    将列表数据写入到结果中
    :param list_data: 对比的列表文件
    :param save_result:保存结果
    """
    for word in list_data:
        with open(save_result, 'a', encoding='utf-8') as f:
            f.write(word + "\n")


txt1 = "../question1/随机汉字_1.txt"
txt2 = "../question1/随机汉字_2.txt"
save_txt = "../question1/结果.txt"

list_1 = get_file_content(txt1)
list_2 = get_file_content(txt2)
list_3 = compare_list(list_1, list_2)
write_result(list_3, save_txt)

爱喝水的木子...小于 1 分钟pythonpython
python
import asyncio
import aiohttp

# 目标网站的URL列表
urls = ['http://yourwebsite.com' + str(i) for i in range(10000)]

# 异步获取函数
async def fetch(url, session):
    async with session.get(url) as response:
        return response.status

# 主函数
async def main():
    async with aiohttp.ClientSession() as session:
        tasks = [fetch(url, session) for url in urls]
        responses = await asyncio.gather(*tasks)
        for url, response in zip(urls, responses):
            print(f'URL: {url}, Status Code: {response.status}')

# 运行主函数
asyncio.run(main())

爱喝水的木子...小于 1 分钟pythonpython
筛选文件大小是0Kb的
import os
import shutil
from multiprocessing.pool import ThreadPool
import json

# 移动文件夹
def move_data(src, dst):
    shutil.move(src, dst)


def get_size(file):
    # 获取文件大小:KB
    size = os.path.getsize(file)
    return size / 1024


# 读取文件
def read_dir_data(in_file, out_file):
    th = ThreadPool(8)
    for i in os.listdir(in_file):
        t = os.path.join(in_file, i)

        # 判断json内容是否为空
        with open(t,"r",encoding="utf-8")as f:
            data=json.loads(f.read())
        if len(data["shapes"])==0:
            dst = os.path.join(out_file, i)
            print("move:{}>>>{}".format(t, dst))
            th.apply_async(move_data, args=(t, dst))

    th.close()
    th.join()
if __name__ == '__main__':
    # 筛选文件夹
    in_file=r"C:\Users\DM\Desktop\新建文件夹 (2)\1"
    # 筛选结果文件夹
    out_file=r"C:\Users\DM\Desktop\新建文件夹 (2)\2"
    read_dir_data(in_file,out_file)


爱喝水的木子...小于 1 分钟pythonpython筛选文件
统计一个文件夹中所有音频的时间(python)
import os

try:
    from pydub.utils import mediainfo
except:
    os.system("pip3 install pydub")
    from pydub.utils import mediainfo


def count_time(file_path):
    song = mediainfo(file_path)
    return song['duration']


# 递归统计文件夹下所有文件数量
def count_file_time(path):
    global all_time
    file_list = os.listdir(path)
    for file in file_list:
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            count_file_time(file_path)
        else:
            if file_path.split(".")[-1] in file_type:
                print(file_path, count_time(file_path))
                all_time += float(count_time(file_path))


# 放文件路径
path = r""
file_type = ["wav", "mp3"]
all_time = 0
count_file_time(path)
print("共:", all_time, "秒")


爱喝水的木子...小于 1 分钟pythonpythonpydub音频文件