最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年12月29日 11:02:13   作者:武散人  
這篇文章主要介紹了Python基于mediainfo批量重命名圖片文件的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

案例故事:

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

作為一名專業(yè)的多媒體測試人員,我們需要一堆的規(guī)范化標準的圖片測試文件,
但是現(xiàn)有的圖片資源名字命名的很隨意比如: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 命令行工具已經加入環(huán)境變量
  2. 以下是某個圖片文件的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并實現(xiàn)重名,可雙擊運行

定義image_info.py模塊

由于涉及較復雜的代碼,建議直接用面向對象類的編程方式實現(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" # 防止程序因為異常而中斷
    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模塊并實現(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: # 遍歷文件列表
    # 以下代碼塊,只是用來測試本模塊的,一般不建議直接在這里大面積調用本模塊'''
    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(更建議丟到某個環(huán)境變量里去),
各種編碼格式的圖片文件,image_info.py模塊,rename_image.py批處理腳本
點我下載
運行效果如下:

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

小提示:

比如Android手機,Google推出了CDD(Compatibiltiy Definition Document兼容性定義文檔),

其第5部分,涉及了很多圖片編解碼格式的規(guī)定:

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

以上就是Python基于mediainfo批量重命名圖片文件的詳細內容,更多關于python 批量重命名文件的資料請關注腳本之家其它相關文章!

相關文章

最新評論

凤翔县| 涟水县| 平凉市| 松江区| 太保市| 丰都县| 三明市| 永顺县| 棋牌| 雅江县| 突泉县| 搜索| 三河市| 道真| 西峡县| 神木县| 邓州市| 遂宁市| 天镇县| 淮南市| 黄石市| 兰考县| 宁南县| 育儿| 三明市| 桐柏县| 江都市| 鄂州市| 仙居县| 清流县| 繁峙县| 东阳市| 台北市| 沧源| 靖远县| 滦平县| 阳泉市| 高要市| 韶关市| 高邮市| 瓮安县|