案例故事: 大部分帶彩色螢屏的終端設備,不管是手機,車機,電視等等,都需要涉及圖片的顯示,
作為一名專業的多媒體測驗人員,我們需要一堆的規范化標準的的圖片測驗檔案,
但是發現圖片資源名字命名的很隨意比如:IMG_20200325_161111.jpg,
以上命名不能看出圖片檔案的具體圖片編碼格式,解析度等資訊,
測驗經理要求我進行批量重命名作業,模板如下,
圖片編碼格式_解析度_位深度_容器.容器, 例如:
JPEG_1920x1080_32bit_jpg.jpg
圖片編解碼基本知識
圖片編碼:將某各風景畫面取景轉成圖片資料檔案的程序,取景肯定涉及取景的范圍,
圖片解碼:將圖片資料檔案顯示到螢屏上的程序,
主要涉及以下技術引數:
<style type="text/css">table.dataframe { font-family: verdana, arial, sans-serif; font-size: 13px; color: rgba(51, 51, 51, 1); border-width: 1px; border-color: rgba(153, 153, 153, 1); border-collapse: collapse; width: 80% } table.dataframe th { background-color: rgba(195, 221, 224, 1); border: 1px solid rgba(169, 198, 201, 1); padding: 8px; text-align: center } table.dataframe tr { } table.dataframe td { border: 1px solid rgba(169, 198, 201, 1); padding: 8px; text-align: left }</style>| 圖片技術引數 | 引數釋義 | 舉例 |
|---|---|---|
| 圖片編碼格式 (壓縮技術) |
即像素點壓縮的一類技術, 不同的編碼格式, 其壓縮率與壓縮效果不一樣, |
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等 |
我們碰到的任何圖片檔案,都是資料的集合,
一般資料越大,其圖片越清晰,
準備階段
- 確保mediainfo.exe 命令列工具已經加入環境變數,查看其具體功能方法,
- 以下是某個圖片檔案的mediainfo資訊, 都是文本,Python處理起來肯定很簡單的,
- 如果要進行批量重命名圖片,我們還是用輸入輸出檔案架構,如下:
+---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
也可關注“武散人”微信訂閱號,隨時接受文章推送,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/124164.html
標籤:Python
上一篇:hook框架-frida使用-APP在模擬器無法打開,用鉤子去除限制
下一篇:03.DRF-設計方法
