Python基于mediainfo批量重命名圖片文件
案例故事:
大部分帶彩色屏幕的終端設(shè)備,不管是手機(jī),車機(jī),電視等等,都需要涉及圖片的顯示,
作為一名專業(yè)的多媒體測試人員,我們需要一堆的規(guī)范化標(biāo)準(zhǔn)的圖片測試文件,
但是現(xiàn)有的圖片資源名字命名的很隨意比如:IMG_20200325_161111.jpg,
以上命名不能看出圖片文件的具體圖片編碼格式,分辨率等信息,
測試經(jīng)理要求我進(jìn)行批量重命名工作,模板如下,
圖片編碼格式_分辨率_位深度_容器.容器, 例如:
JPEG_1920x1080_32bit_jpg.jpg
圖片編解碼基本知識
圖片編碼:將某各風(fēng)景畫面取景轉(zhuǎn)成圖片數(shù)據(jù)文件的過程,取景肯定涉及取景的范圍,
圖片解碼:將圖片數(shù)據(jù)文件顯示到屏幕上的過程。
主要涉及以下技術(shù)參數(shù):
| 圖片技術(shù)參數(shù) | 參數(shù)釋義 | 舉例 |
|---|---|---|
| 圖片編碼格式 (壓縮技術(shù)) |
即像素點(diǎn)壓縮的一類技術(shù), 不同的編碼格式, 其壓縮率與壓縮效果不一樣。 |
JPEG, PNG, GIF, BMP, Webp, RAW, Heic |
| 圖片分辨率 (單位:Pixel) |
圖片長像素點(diǎn)的數(shù)量*圖片寬像素點(diǎn)的數(shù)量 | 4096×2160(4K), 1920x1080, 1280x720,720×480, 640x480, 320x480等 甚至10億像素的圖片都存在的。 |
| 位深度 (單位:bit) |
每個像素點(diǎn)所包含的數(shù)據(jù)量的大小 | 8bit, 16bit, 32bit |
| 圖片容器 | 文件后綴,將圖片像素點(diǎn)封裝的一種文件格式 | .jpg; .png; .gif; .bmp; .heic; .webp等 |
我們碰到的任何圖片文件,都是數(shù)據(jù)的集合,
一般數(shù)據(jù)越大,其圖片越清晰。
準(zhǔn)備階段
- 確保mediainfo.exe 命令行工具已經(jīng)加入環(huán)境變量
- 以下是某個圖片文件的mediainfo信息, 都是文本,Python處理起來肯定很簡單的。

- 如果要進(jìn)行批量重命名圖片,我們還是用輸入輸出文件架構(gòu),如下:
+---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 #調(diào)用image_info.py并實(shí)現(xiàn)重名,可雙擊運(yùn)行
定義image_info.py模塊
由于涉及較復(fù)雜的代碼,建議直接用面向?qū)ο箢惖木幊谭绞綄?shí)現(xiàn):
# 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" # 防止程序因?yàn)楫惓6袛?
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" # 防止程序因?yàn)楫惓6袛?
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" # 防止程序因?yàn)楫惓6袛?
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() # 全部轉(zhuǎn)成小寫
return image_container
if __name__ == '__main__':
# 以下代碼塊,只是用來測試本模塊的,一般不建議直接在這里大面積調(diào)用本模塊'''
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)
調(diào)用image_info.py模塊并實(shí)現(xiàn)批量重命名
# 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這個文件夾,則創(chuàng)建這個文件夾
if not os.path.exists(output_image_path):
os.mkdir(output_image_path)
if filelist: # 如果文件列表不為空
for i in filelist: # 遍歷文件列表
# 以下代碼塊,只是用來測試本模塊的,一般不建議直接在這里大面積調(diào)用本模塊'''
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) # 復(fù)制文件
else:
print("It's a Empty folder, please input the image files which need to be renamed firstly!!!")
os.system("pause")
本案例練手素材下載
包含:mediainfo.exe(更建議丟到某個環(huán)境變量里去),
各種編碼格式的圖片文件,image_info.py模塊,rename_image.py批處理腳本
點(diǎn)我下載
運(yùn)行效果如下:

以上可以看出,輸入輸出文件架構(gòu)的好處, 我只需要將不同名字不同字符的,
待重命名的圖片丟到Input_Image文件夾下,運(yùn)行程序腳本后查看Output_Image輸出文件,
就可以測試腳本的運(yùn)行是否正常,健壯性(容錯)是否符合要求,從而對這個程序腳本實(shí)現(xiàn)了“灰盒測試”。
小提示:
比如Android手機(jī),Google推出了CDD(Compatibiltiy Definition Document兼容性定義文檔),
其第5部分,涉及了很多圖片編解碼格式的規(guī)定:

這就是Android最主要的圖片多媒體編解碼測試需求。
以上就是Python基于mediainfo批量重命名圖片文件的詳細(xì)內(nèi)容,更多關(guān)于python 批量重命名文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pycharm下查看python的變量類型和變量內(nèi)容的方法
今天小編就為大家分享一篇pycharm下查看python的變量類型和變量內(nèi)容的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Python安裝使用命令行交互模塊pexpect的基礎(chǔ)教程
Pexpect是一個純Python模塊,可以用來和ssh、ftp、passwd、telnet等命令行命令進(jìn)行交互使用,在Linux系統(tǒng)下尤其好用,下面我們就來具體來看一下Python安裝使用命令行交互模塊pexpect的基礎(chǔ)教程:2016-05-05
Python保留指定位數(shù)小數(shù)的5種方法總結(jié)
很多小伙伴在學(xué)習(xí)python的時候可能會遇到對數(shù)據(jù)進(jìn)行格式化輸出的需求,其中最常見的需求為保留幾位小數(shù),這篇文章主要給大家介紹了關(guān)于Python保留指定位數(shù)小數(shù)的5種方法,需要的朋友可以參考下2023-08-08
Pytorch實(shí)現(xiàn)LSTM案例總結(jié)學(xué)習(xí)
這篇文章主要介紹了Pytorch實(shí)現(xiàn)LSTM案例總結(jié)學(xué)習(xí),文章通過構(gòu)建網(wǎng)絡(luò)層、前向傳播forward()展開主題介紹,需要的小伙吧可以參考一下2022-07-07
python獲取天氣接口給指定微信好友發(fā)天氣預(yù)報
這篇文章主要介紹了python獲取天氣接口給指定微信好友發(fā)天氣預(yù)報的步驟,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-12-12
Python運(yùn)算符優(yōu)先級詳細(xì)整理
在一個表達(dá)式中可能包含多個有不同運(yùn)算符連接起來的、具有不同數(shù)據(jù)類型的數(shù)據(jù)對象,由于表達(dá)式有多種運(yùn)算,不同的運(yùn)算順序可能得出不同結(jié)果甚至出現(xiàn)錯誤運(yùn)算錯誤,下面這篇文章主要給大家介紹了關(guān)于Python運(yùn)算符優(yōu)先級的相關(guān)資料,需要的朋友可以參考下2023-01-01

