Python基于mediainfo批量重命名图片文件


Posted in Python onDecember 29, 2020

案例故事:

大部分带彩色屏幕的终端设备,不管是手机,车机,电视等等,都需要涉及图片的显示,

作为一名专业的多媒体测试人员,我们需要一堆的规范化标准的图片测试文件,
但是现有的图片资源名字命名的很随意比如:IMG_20200325_161111.jpg,
以上命名不能看出图片文件的具体图片编码格式,分辨率等信息,
测试经理要求我进行批量重命名工作,模板如下,
图片编码格式_分辨率_位深度_容器.容器, 例如:
JPEG_1920x1080_32bit_jpg.jpg

图片编解码基本知识

图片编码:将某各风景画面取景转成图片数据文件的过程,取景肯定涉及取景的范围,
图片解码:将图片数据文件显示到屏幕上的过程。

主要涉及以下技术参数:

图片技术参数 参数释义 举例
图片编码格式 (压缩技术) 即像素点压缩的一类技术, 不同的编码格式, 其压缩率与压缩效果不一样。 JPEG, PNG, GIF, BMP, Webp, RAW, Heic
图片分辨率 (单位:Pixel) 图片长像素点的数量*图片宽像素点的数量 4096×2160(4K), 1920x1080, 1280x720,720×480, 640x480, 320x480等 甚至10亿像素的图片都存在的。
位深度 (单位:bit) 每个像素点所包含的数据量的大小 8bit, 16bit, 32bit
图片容器 文件后缀,将图片像素点封装的一种文件格式 .jpg; .png; .gif; .bmp; .heic; .webp等

我们碰到的任何图片文件,都是数据的集合,
一般数据越大,其图片越清晰。

准备阶段

  1. 确保mediainfo.exe 命令行工具已经加入环境变量
  2. 以下是某个图片文件的mediainfo信息, 都是文本,Python处理起来肯定很简单的。

Python基于mediainfo批量重命名图片文件

  • 如果要进行批量重命名图片,我们还是用输入输出文件架构,如下:
+---Input_Image  #批量放入待命名的图片文件
	|    1.jpg
	|    2.png
	|    
	+---Output_Image  #批量输出已命名的图片文件
	|    JPEG_1920x1080_32bit_jpg.jpg
	|	PNG_1280x720_32bit_png.png
	|
    \image_info.py  # 获取图片文件info信息的模块,
	\rename_image.py #调用image_info.py并实现重名,可双击运行

定义image_info.py模块

由于涉及较复杂的代码,建议直接用面向对象类的编程方式实现:

# coding=utf-8
 
import os
import re
import subprocess
 
 
class ImageInfoGetter():
  '''获取图片文件的Formate, 分辨率,位深度'''
 
  def __init__(self, image_file):
    '''判断文件是否存在,如果存在获取其mediainfo信息'''
    if os.path.exists(image_file):
      self.image_file = image_file
      p_obj = subprocess.Popen('mediainfo "%s"' % self.image_file, shell=True, stdout=subprocess.PIPE,
                   stderr=subprocess.PIPE)
      self.info = p_obj.stdout.read().decode("utf-8") # 解决非英文字符的编码问题
    else:
      raise FileNotFoundError("Not this File!") # 如果多媒体文件路径不存在,必须中断
 
  def get_image_format(self):
    '''获取图片的格式,比如JPEG, PNG, BMP等'''
    try:
      image_codec = re.findall(r"Format\s+:\s(.*)", self.info)[-1] # 取第最后一个Format字段
      image_codec = image_codec.strip() # 去除前后的空格
      if image_codec == "RGB":
        image_codec = "BMP"
    except:
      image_codec = "undef" # 防止程序因为异常而中断
    return image_codec
 
  def get_image_resolution(self):
    '''获取图片的分辨率'''
    try:
      image_widget = re.findall(r'Width\s+:\s(.*)pixels', self.info)[-1]
      image_widget = image_widget.replace(" ", "")
      image_height = re.findall(r'Height\s+:\s(.*)pixels', self.info)[-1]
      image_height = image_height.replace(" ", "")
      image_resolution = image_widget + "x" + image_height
    except:
      image_resolution = "undef" # 防止程序因为异常而中断
    return image_resolution
 
  def get_image_bit_depth(self):
    '''获取图片的位深度'''
    try:
      image_bit_depth = re.findall(r"Bit depth\s+:\s(.*bit)s", self.info)[-1].strip()
      image_bit_depth = image_bit_depth.replace(" ", "") # 去空格
    except:
      image_bit_depth = "undef" # 防止程序因为异常而中断
    return image_bit_depth
 
  def get_image_container(self):
    '''获取图片容器,即文件后缀名'''
    _, image_container = os.path.splitext(self.image_file)
    if not image_container:
      raise NameError("This file no extension")
    image_container = image_container.replace(".", "")
    image_container = image_container.lower() # 全部转成小写
    return image_container
 
 
if __name__ == '__main__':
  # 以下代码块,只是用来测试本模块的,一般不建议直接在这里大面积调用本模块'''
  i_obj = ImageInfoGetter("C:\\img.jpg")
  image_format = i_obj.get_image_format()
  print(image_format)
  image_resolution = i_obj.get_image_resolution()
  print(image_resolution)
  image_bit_depth = i_obj.get_image_bit_depth()
  print(image_bit_depth)
  image_container = i_obj.get_image_container()
  print(image_container)

调用image_info.py模块并实现批量重命名

# coding=utf-8
 
import os
import image_info
from shutil import copyfile
 
curdir = os.getcwd()
 
# 输入文件夹,放入待重命名的图片
input_image_path = os.path.join(curdir, "Input_Image")
filelist = os.listdir(input_image_path) # 获取文件列表
 
# 输出文件夹,已命名的图片存放在这里
output_image_path = os.path.join(curdir, "Output_Image")
 
# 如果没有Output_Image这个文件夹,则创建这个文件夹
if not os.path.exists(output_image_path):
  os.mkdir(output_image_path)
 
if filelist: # 如果文件列表不为空
  for i in filelist: # 遍历文件列表
    # 以下代码块,只是用来测试本模块的,一般不建议直接在这里大面积调用本模块'''
    image_file = os.path.join(input_image_path, i)
    i_obj = image_info.ImageInfoGetter(image_file)
    image_format = i_obj.get_image_format()
    image_resolution = i_obj.get_image_resolution()
    image_bit_depth = i_obj.get_image_bit_depth()
    image_container = i_obj.get_image_container()
    new_image_name = image_format + "_" + image_resolution + "_" + image_bit_depth + "_" \
             + image_container + "." + image_container
    print(new_image_name)
    new_image_file = os.path.join(output_image_path, new_image_name)
    copyfile(image_file, new_image_file) # 复制文件
else:
  print("It's a Empty folder, please input the image files which need to be renamed firstly!!!")
os.system("pause")

本案例练手素材下载

包含:mediainfo.exe(更建议丢到某个环境变量里去),
各种编码格式的图片文件,image_info.py模块,rename_image.py批处理脚本
点我下载
运行效果如下:

Python基于mediainfo批量重命名图片文件

以上可以看出,输入输出文件架构的好处, 我只需要将不同名字不同字符的,
待重命名的图片丢到Input_Image文件夹下,运行程序脚本后查看Output_Image输出文件,
就可以测试脚本的运行是否正常,健壮性(容错)是否符合要求,从而对这个程序脚本实现了“灰盒测试”。

小提示:

比如Android手机,Google推出了CDD(Compatibiltiy Definition Document兼容性定义文档),

其第5部分,涉及了很多图片编解码格式的规定:

Python基于mediainfo批量重命名图片文件

这就是Android最主要的图片多媒体编解码测试需求。

以上就是Python基于mediainfo批量重命名图片文件的详细内容,更多关于python 批量重命名文件的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python实现中文输出的两种方法
May 09 Python
python实现的二叉树定义与遍历算法实例
Jun 30 Python
python pygame实现五子棋小游戏
Oct 26 Python
python 执行终端/控制台命令的例子
Jul 12 Python
python中selenium操作下拉滚动条的几种方法汇总
Jul 14 Python
python实现图片中文字分割效果
Jul 22 Python
Numpy将二维数组添加到空数组的实现
Dec 05 Python
Python 基于FIR实现Hilbert滤波器求信号包络详解
Feb 26 Python
简单了解python列表和元组的区别
May 14 Python
浅谈python量化 双均线策略(金叉死叉)
Jun 03 Python
python绘制高斯曲线
Feb 19 Python
请求模块urllib之PYTHON爬虫的基本使用
Apr 08 Python
用python批量下载apk
Dec 29 #Python
python如何调用php文件中的函数详解
Dec 29 #Python
详解Open Folder as PyCharm Project怎么添加的方法
Dec 29 #Python
pycharm如何设置官方中文(如何汉化)
Dec 29 #Python
详解Pycharm第三方库的安装及使用方法
Dec 29 #Python
Django2.1.7 查询数据返回json格式的实现
Dec 29 #Python
Python 获取异常(Exception)信息的几种方法
Dec 29 #Python
You might like
对Session和Cookie的区分与解释
2007/03/16 PHP
php使用qr生成二维码的示例分享
2014/01/20 PHP
laravel安装和配置教程
2014/10/29 PHP
PHP基于自定义函数实现的汉字转拼音功能实例
2017/09/30 PHP
php/JS实现的生成随机密码(验证码)功能示例
2019/06/06 PHP
找到一点可怜的关于dojo资料,谢谢作者!
2006/12/06 Javascript
innerHTML,outerHTML,innerTEXT三者之间的区别
2007/01/28 Javascript
可以把编码转换成 gb2312编码lib.UTF8toGB2312.js
2007/08/21 Javascript
web的各种前端打印方法之jquery打印插件jqprint实现网页打印
2013/01/09 Javascript
千分位数字格式化(用逗号隔开 代码已做了修改 支持0-9位逗号隔开)的JS代码
2013/12/05 Javascript
JS中判断null、undefined与NaN的方法
2014/03/24 Javascript
javascript移出节点removeChild()使用介绍
2014/04/03 Javascript
基于jQuery实现复选框的全选 全不选 反选功能
2014/11/24 Javascript
js+canvas绘制矩形的方法
2016/01/28 Javascript
Angular js 实现添加用户、修改密码、敏感字、下拉菜单的综合操作方法
2017/10/24 Javascript
vue-cli项目根据线上环境分别打出测试包和生产包
2018/05/23 Javascript
vue-cli3中vue.config.js配置教程详解
2019/05/29 Javascript
解决React在安装antd之后出现的Can't resolve './locale'问题(推荐)
2020/05/03 Javascript
Vue的自定义组件不能使用click方法的解决
2020/07/28 Javascript
[02:38]2018DOTA2亚洲邀请赛赛前采访-VGJ.T
2018/04/03 DOTA
[22:59]VGJ.S vs VG 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
python中assert用法实例分析
2015/04/30 Python
Python KMeans聚类问题分析
2018/02/23 Python
pycharm: 恢复(reset) 误删文件的方法
2018/10/22 Python
Python 3.8 新功能全解
2019/07/25 Python
Django处理Ajax发送的Get请求代码详解
2019/07/29 Python
瑞典灯具和照明网上商店:Lamp24.se
2018/03/17 全球购物
西班牙鞋子和箱包在线销售网站:zapatos.es
2020/02/17 全球购物
安全资料员岗位职责
2013/12/14 职场文书
一个大学生十年的职业规划
2014/01/17 职场文书
四年级科学教学反思
2014/02/10 职场文书
奉献爱心演讲稿
2014/09/04 职场文书
违反学校规则制度检讨书
2015/01/01 职场文书
银行优秀员工推荐信
2015/03/24 职场文书
WebWorker 封装 JavaScript 沙箱详情
2021/11/02 Javascript
Vue+TypeScript中处理computed方式
2022/04/02 Vue.js