1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
from __future__ import print_function
import cv2 import time import os import operator import numpy as np import argparse from PIL import Image
__author__ = 'zj'
image_formats = ['jpg', 'JPG', 'jpeg', 'JPEG', 'png', 'PNG']
def is_ppm_file(in_path): if not os.path.isfile(in_path): return False if in_path is not str and not in_path.lower().endswith('.ppm'): return False return True
def convert_ppm_by_PIL(in_path, out_path): if not is_ppm_file(in_path): raise Exception("%s 不是一个PPM文件" % in_path) im = Image.open(in_path) im.save(out_path)
def convert_ppm_P6(in_path, out_path): """ 将ppm文件转换成其它图像格式 读取二进制文件,先读取幻数,再读取宽和高,以及最大值 :param in_path: 输入ppm文件路径 :param out_path: 输出文件路径 """ if not is_ppm_file(in_path): raise Exception("%s 不是一个PPM文件" % in_path) with open(in_path, 'rb') as f: magic_number = f.readline().strip().decode('utf-8') if not operator.eq(magic_number, "P6"): raise Exception("该图像有误") width, height = f.readline().strip().decode('utf-8').split(' ') width = int(width) height = int(height) maxval = f.readline().strip() byte_array = np.array(list(f.readline())) img = byte_array.reshape((height, width, 3)).astype(np.uint8) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) cv2.imwrite(out_path, img) print('%s save ok' % out_path)
def convert_ppm_P6_batch(in_dir, out_dir, res_format): """ 批量转换PPM文件 :param in_dir: ppm文件夹路径 :param out_dir: 输出文件夹路径 :param res_format: 结果图像格式 """ if not os.path.isdir(in_dir): raise Exception('%s 不是路径' % in_dir) if not os.path.isdir(out_dir): raise Exception('%s 不是路径' % out_dir) if not res_format in image_formats: raise Exception('%s 暂不支持' % res_format) file_list = os.listdir(in_dir) for file_name in file_list: file_path = os.path.join(in_dir, file_name) if is_ppm_file(file_path): file_out_path = os.path.join(out_dir, os.path.splitext(file_name)[0] + '.' + res_format) convert_ppm_P6(file_path, file_out_path) elif os.path.isdir(file_path): file_out_dir = os.path.join(out_dir, file_name) if not os.path.exists(file_out_dir): os.mkdir(file_out_dir) convert_ppm_P6_batch(file_path, file_out_dir, res_format) else: pass print('batch operation over')
if __name__ == '__main__': script_start_time = time.time()
parser = argparse.ArgumentParser(description='Format Converter - PPM')
parser.add_argument('-i', '--input', type=str, help='Path to the ppm file') parser.add_argument('-o', '--output', type=str, help='Path to the result file') parser.add_argument('--input_dir', type=str, help='Dir to the ppm files') parser.add_argument('--output_dir', type=str, help='Dir to the result files') parser.add_argument('-f', '--format', default='png', type=str, help='result image format') parser.add_argument('-b', '--batch', action="store_true", default=False, help='Batch processing')
args = vars(parser.parse_args()) in_path = args['input'] out_path = args['output']
isbatch = args['batch'] in_dir = args['input_dir'] out_dir = args['output_dir'] res_format = args['format']
if in_path is not None and out_path is not None: convert_ppm_P6(in_path, out_path) elif isbatch: convert_ppm_P6_batch(in_dir, out_dir, res_format) else: print('请输入相应参数')
print('Script took %s seconds.' % (time.time() - script_start_time,))
|