ffmpeg+x265精简压缩图片-LMLPHP

可以看到700M的图片可以压缩到只有9M,并且模型推理精度只会下降0.07.有很多种压缩方式,经过大量实验qb36是最好的,默认使用这个。

ffmpeg+x265精简压缩图片-LMLPHP

@input_images:原始图片。

@mkv_files\qb36:中转文件,也就是说原始图片要先转换为.mkv文件,然后继而转换为被压缩后的图片。

@output_images:不用多说,压缩后的图片。

NOTE:只需安装ffmpeg。 

 👑核心代码:

import os
import glob
from tqdm import tqdm

class x265_encode_decode_Tool(object):
    def __init__(self, quality, mkv_path, input_img_path, output_img_path) -> None:
        self.quality = quality
        self.mkv_path = mkv_path
        self.input_img_path = input_img_path
        self.output_img_path = output_img_path
        self.mkvFile = ""

        if not os.path.exists(self.mkv_path):
            os.makedirs(self.mkv_path)

        if not os.path.exists(self.output_img_path):
            os.makedirs(self.output_img_path)

    def run_x265_encode(self):
        label_list=os.listdir(self.input_img_path)
        for label in tqdm(label_list):    #xxx.jpg
            '''
                jpg to mkv with qpxx
            '''
            os.chdir(self.input_img_path)  # .../JPEGImages/
            cmd_jpg_to_mkv="ffmpeg -i " + label + " -c:v libx265 -x265-params qp="\
                + self.quality[2:] + " "+ self.mkv_path + "/"+\
                label[:len(label)-4]+".mkv"
            # print(cmd_jpg_to_mkv)
            os.system(cmd_jpg_to_mkv)

    def run_x265_decode(self):
        # '''
        #    mkv to jpg
        # '''
        label_list=os.listdir(self.input_img_path)
        for label in tqdm(label_list):    #xxx.jpg
            os.chdir(self.mkv_path + "/" + self.mkvFile) #.../x265_VOC2007/
            cmd_mkv_to_jpg = "ffmpeg -i  "+label[:len(label)-4]+".mkv -r 1 \
                            -f image2 "+ self.output_img_path + "/" + self.mkvFile + "/" + label
            # print(cmd_mkv_to_jpg)
            os.system(cmd_mkv_to_jpg)

    def run_x265_encode_and_decode(self):
        label_list=os.listdir(self.input_img_path)
        for label in tqdm(label_list):    #xxx.jpg
            '''
            jpg to mkv with qpxx
            '''
            os.chdir(self.input_img_path)  # .../JPEGImages/
            cmd_jpg_to_mkv="ffmpeg -i " + label + " -c:v libx265 -x265-params qp="\
                        + self.quality[2:] + " "+ self.mkv_path + "/"+\
                        label[:len(label)-4]+".mkv"
            # print(cmd_jpg_to_mkv)
            os.system(cmd_jpg_to_mkv)

            # '''
            #     mkv to jpg
            # '''
            os.chdir(self.mkv_path + "/" + self.mkvFile) #.../x265_VOC2007/
            cmd_mkv_to_jpg = "ffmpeg -i  "+label[:len(label)-4]+".mkv -r 1 \
            				-f image2 "+ self.output_img_path + "/" + self.mkvFile + "/" + label
            # print(cmd_mkv_to_jpg)
            os.system(cmd_mkv_to_jpg)

👑调用接口:

from x265_encode_and_decode import x265_encode_decode_Tool

if __name__ == "__main__":
  
  quality = 'qb36'
  mkv_path = '/data2/personal/jiangziliang/projects/x265_imageCompressor/mkv_files/%s' % quality
  input_img_path = r'/data2/personal/jiangziliang/projects/x265_imageCompressor/input_images'
  output_img_path = r'/data2/personal/jiangziliang/projects/x265_imageCompressor/output_images'
  
  x265Tools = x265_encode_decode_Tool(
    quality, mkv_path, input_img_path, output_img_path)
  '''
    jpeg -> mkv
  '''
  # x265Tools.run_x265_encode()
  '''
    mkv -> jpeg
  '''
  # x265Tools.run_x265_decode()
  '''
    jpeg -> mkv -> jpeg
  '''
  # x265Tools.run_x265_encode_and_decode()
01-08 08:14