亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:2日期:2022-07-01 08:27:44
案例故事:

大部分帶彩色屏幕的終端設備,不管是手機,車機,電視等等,都需要涉及圖片的顯示,

作為一名專業的多媒體測試人員,我們需要一堆的規范化標準的圖片測試文件,但是現有的圖片資源名字命名的很隨意比如: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等

我們碰到的任何圖片文件,都是數據的集合,一般數據越大,其圖片越清晰。

準備階段 確保mediainfo.exe 命令行工具已經加入環境變量 以下是某個圖片文件的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 osimport reimport 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'Formats+: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’Widths+:s(.*)pixels’, self.info)[-1] image_widget = image_widget.replace(' ', '') image_height = re.findall(r’Heights+: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 depths+: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 osimport image_infofrom 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 編程
相關文章:
主站蜘蛛池模板: 国产成人a毛片 | 在线成人综合色一区 | 国产精品臀控福利在线观看 | 曰批免费动漫视频播放免费 | 国产在线精品一区二区高清不卡 | 成人区视频 | 国内精品一区二区三区最新 | 亚洲精品视频久久久 | 日韩特级片 | 特黄特色大片免费高清视频 | 欧美日韩黄色 | 97国产大学生情侣11在线视频 | 精品国产福利在线 | 欧美亚洲国产一区二区三区 | 久久国产成人精品国产成人亚洲 | 欧美黄三级在线观看 | 日本a毛片在线播放 | 色精品一区二区三区 | 黄色免费观看 | 青草娱乐极品免费视频 | 国产网站免费在线观看 | 国产 日韩 欧美 高清 | 欧美日韩亚洲精品一区二区三区 | 欧美夜夜精品一级爽 | 亚洲视频国产精品 | 777狠狠| 国产亚洲欧美日韩在线一区 | 国产性大片免费播放网站 | 1024国产 | 久久青草18免费观看网站 | 土耳其毛片 | 91视频日本 | 亚洲第一视频 | 国产在线91观看免费观看 | 免费黄色看片网站 | 一级女人18毛片免费 | 国产美女无遮挡免费视频网站 | 一级毛片私人影院 | 色综合久久手机在线 | 国产成人高清亚洲一区久久 | 欧美一级特黄特色大片 |