龙空技术网

「python脚本」批量修改图片尺寸&视频安帧提取

独一无二的Python君 172

前言:

现在朋友们对“python3批量替换16进制数”大约比较讲究,姐妹们都想要了解一些“python3批量替换16进制数”的相关资讯。那么小编在网络上网罗了一些有关“python3批量替换16进制数””的相关资讯,希望小伙伴们能喜欢,姐妹们快快来学习一下吧!

【python脚本】批量修改图片尺寸

# -*- coding: utf-8 -*-"""Created on Thu Aug 23 16:06:35 2018@author: libo"""from PIL import Imageimport os    def image_resize(image_path, new_path):           # 统一图片尺寸    print('============>>修改图片尺寸')    for img_name in os.listdir(image_path):        img_path = image_path + "/" + img_name    # 获取该图片全称        image = Image.open(img_path)              # 打开特定一张图片        image = image.resize((3840, 2160))          # 设置需要转换的图片大小        # process the 1 channel image        image.save(new_path + '/'+ img_name)    print("end the processing!")    if __name__ == '__main__':    print("ready for ::::::::  ")    ori_path = r"/home/yjq/Research_data/wurenji_putao/test"                # 输入图片的文件夹路径    new_path = '/home/yjq/Research_data/wurenji_putao/test/resize'                   # resize之后的文件夹路径    image_resize(ori_path, new_path)复制代码
【python脚本】python代码按帧提取提取视频保存为图片

代码一:

import cv2import os#要提取视频的文件名,隐藏后缀sourceFileName='../wurenji_putao/DJI_0009'#在这里把后缀接上video_path = os.path.join("", "", sourceFileName+'.MP4')times=0#提取视频的频率,每25帧提取一个frameFrequency=25#输出图片到当前目录vedio文件夹下outPutDirName=''+sourceFileName+'/'if not os.path.exists(outPutDirName):    #如果文件目录不存在则创建目录    os.makedirs(outPutDirName) camera = cv2.VideoCapture(video_path)while True:    times+=1    res, image = camera.read()    if not res:        print('not res , not image')        break    if times%frameFrequency==0:        cv2.imwrite(outPutDirName + str(times)+'.jpg', image)        print(outPutDirName + str(times)+'.jpg')print('图片提取结束')camera.release()复制代码

代码二:

import cv2vidcap = cv2.VideoCapture('../wureji_putao/DJI_0009.MP4')success,img = vidcap.read()count = 0 success = Truewhile success :    success,image = vidcap.read()    cv2.imwrite("frame%d.jpg" % count, image)    if cv2.waitKey(10) == 27:        break    count += 1 复制代码

代码三:

# import sys# sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages') import cv2vc = cv2.VideoCapture('../wurenji_putao/DJI_0010.MP4')  # 读入视频文件,命名cvn = 1  # 计数  if vc.isOpened():  # 判断是否正常打开    rval, frame = vc.read()else:    rval = False  timeF = 10  # 视频帧计数间隔频率  i = 259while rval:  # 循环读取视频帧    rval, frame = vc.read()    if (n % timeF == 0):  # 每隔timeF帧进行存储操作        i += 1        print(i)        cv2.imwrite('../wurenji_putao/DJI_0010/{}.jpg'.format(i), frame)  # 存储为图像    n = n + 1    cv2.waitKey(1)vc.release()

作者:放羊Wa

链接:

来源:稀土掘金

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

标签: #python3批量替换16进制数 #python获取图片长宽