《自拍教程71》Python mediainfo批量重命名圖片文件 AV專家必備!

案例故事: 大部分帶彩色屏幕的終端設備,不管是手機,車機,電視等等,都需要涉及圖片的顯示,
作爲一名專業的多媒體測試人員,我們需要一堆的規範化標準的的圖片測試文件
但是發現圖片資源名字命名的很隨意比如: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處理起來肯定很簡單的。
  3. 如果要進行批量重命名圖片,我們還是用輸入輸出文件架構,如下:

	+---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()  # 去除前後的空格
        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批處理腳本
跳轉到自拍教程官網下載
運行效果如下:

以上可以看出,輸入輸出文件架構的好處,
我只需要將不同名字不同字符的,待重命名的圖片文件整理好,
丟到Input_Image文件夾下,運行程序腳本後查看Output_Image輸出文件,
就可以測試腳本的運行是否正常,健壯性(容錯)是否符合要求,
從而對這個程序腳本實現了“灰盒測試”。

小提示:比如Android手機,Google推出了CDD(Compatibiltiy Definition Document兼容性定義文檔)
其第5部分,涉及了很多圖片編解碼格式的規定:

這就是Android最主要的圖片多媒體編解碼測試需求。

更多更好的原創文章,請訪問官方網站:www.zipython.com
自拍教程(自動化測試Python教程,武散人編著)
原文鏈接:https://www.zipython.com/#/detail?id=83bc5c1566104f5da17baa22f25390b6
也可關注“武散人”微信訂閱號,隨時接受文章推送。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章