| 12
 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
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 
 | import json, datetime, requests, os, time, random, getopt, sysfrom PIL import Image
 from PIL import ImageFile
 import libimagequant as liq
 
 
 
 
 def compress_images(image_input_folder = '', image_output_folder = False):
 
 if not image_input_folder:
 print("No Image Folder Given, Exit!")
 return
 
 
 if not os.path.exists(image_input_folder):
 print("Input Folder doesn't exists! ")
 
 
 if not os.path.exists(image_output_folder):
 print("Output Folder doesn't exists! ")
 
 total_size = 0
 compress_size = 0
 for parent, _, file_names in os.walk(image_input_folder):
 for file_name in file_names:
 output_file_name = image_output_folder + file_name
 file_name = os.path.join(parent, file_name)
 
 if file_name.endswith(('jpg','bmp')):
 image_info = compress_single_image(file_name, output_file_name)
 if image_info:
 total_size += image_info['original_size']
 compress_size += image_info['compress_size']
 elif file_name.endswith(('png')):
 image_info = compress_png_image( file_name, output_file_name)
 if image_info:
 total_size += image_info['original_size']
 compress_size += image_info['compress_size']
 
 print('Total original size : ' + str(total_size))
 print('Total compress size : ' + str(compress_size))
 
 
 def to_liq( image, attr):
 if image.mode != 'RGBA':
 image = image.convert('RGBA')
 
 return attr.create_rgba(image.tobytes(), image.width, image.height, image.info.get('gamma', 0))
 
 def from_liq(result, image):
 out_img = Image.frombytes('P', (image.width, image.height), result.remap_image(image))
 
 palette_data = []
 for color in result.get_palette():
 palette_data.extend(color[:3])
 out_img.putpalette(palette_data)
 
 return out_img
 
 
 def compress_png_image( image_path, output_file_name = False, quality = 80):
 
 c_obj = {}
 
 original_size = os.path.getsize( image_path )
 c_obj['original_size'] = original_size
 
 try:
 im = Image.open(image_path)
 x, y = im.size
 c_obj['img_x'] = x
 c_obj['img_y'] = y
 
 attr = liq.Attr()
 liq_image = to_liq(im, attr)
 result = liq_image.quantize(attr)
 pil_image = from_liq(result, liq_image)
 pil_image.save(output_file_name, optimize = True, quality = quality)
 c_obj['compress_size'] = os.path.getsize(output_file_name)
 
 
 print(image_path)
 print(c_obj)
 
 
 return c_obj
 except Exception as r:
 print("Compress Png Image Error : " + image_path)
 print("Unknow Error : %s" %(r))
 return False
 
 
 
 
 def compress_single_image( image_path, output_file_name = False, in_replace = True, quality = 80, size_scale = 0.8 ):
 image_x_threshold = 1024
 image_size_threshold = 10240
 
 
 c_obj = {}
 small_x = 0
 
 original_size = os.path.getsize( image_path )
 c_obj['original_size'] = original_size
 
 ImageFile.LOAD_TRUNCATED_IMAGES = True
 
 try:
 im = Image.open(image_path)
 x, y = im.size
 c_obj['img_x'] = x
 c_obj['img_y'] = y
 
 
 if original_size < image_size_threshold:
 return False
 
 
 while x > image_x_threshold:
 small_x = int(x * size_scale)
 small_y = int(y * small_x / x)
 c_obj['s_x'] = small_x
 c_obj['s_y'] = small_y
 
 x = small_x
 y = small_y
 
 if small_x > 0:
 
 im = im.resize( (small_x, small_y), Image.ANTIALIAS)
 
 if output_file_name:
 
 
 
 im.save(output_file_name, optimize = True, quality = quality)
 c_obj['compress_size'] = os.path.getsize(output_file_name)
 else:
 im.save(image_path, optimize = True, quality = quality)
 c_obj['compress_size'] = os.path.getsize(image_path)
 
 print(image_path)
 print(c_obj)
 
 
 return c_obj
 except:
 print("Image Read Error : " + image_path)
 return False
 
 
 if __name__ == '__main__':
 opts, args = getopt.getopt(sys.argv[1:], "m:h:f:o:", ["mode", "help", "folder", "output"])
 IMAGE_FOLDER = ''
 OUTPUT_FOLDER = False
 
 for user_option, user_parameter in opts:
 if user_option in ("-h", "--help"):
 print("Usage: python3 image_tools.py -m method -f image_folder_path [-o output_folder]")
 if user_option in ("-m", "--method"):
 PROCESS_METHOD = user_parameter
 if user_option in ("-f", "--folder"):
 IMAGE_FOLDER = user_parameter
 if user_option in ("-o", "--output"):
 OUTPUT_FOLDER = user_parameter
 
 if PROCESS_METHOD == 'compress':
 compress_images(IMAGE_FOLDER, OUTPUT_FOLDER)
 
 |