added webp conversion
This commit is contained in:
@@ -14,11 +14,15 @@ def compressor(file, final_path, quality, resize, size):
|
|||||||
|
|
||||||
# open image
|
# open image
|
||||||
with Image.open(filepath) as image:
|
with Image.open(filepath) as image:
|
||||||
# resize and compress landscape images
|
cover = image
|
||||||
if resize and image.width >= size and image.width >= image.height:
|
|
||||||
# resize
|
# resize
|
||||||
|
if resize:
|
||||||
print('resizing:' + file)
|
print('resizing:' + file)
|
||||||
cover = resizeimage.resize_width(image, size)
|
if cover.width >= size and cover.width >= cover.height:
|
||||||
|
cover = resizeimage.resize_width(cover, size)
|
||||||
|
elif cover.height >= size and cover.height >= cover.width:
|
||||||
|
cover = resizeimage.resize_height(cover, size)
|
||||||
|
|
||||||
# convert to 8 bit (if possible)
|
# convert to 8 bit (if possible)
|
||||||
print('converting to 8 bit: ' + file)
|
print('converting to 8 bit: ' + file)
|
||||||
if cover.format in ['JPG', 'JPEG']:
|
if cover.format in ['JPG', 'JPEG']:
|
||||||
@@ -26,6 +30,7 @@ def compressor(file, final_path, quality, resize, size):
|
|||||||
cover = cover.quantize(colors=256, method=2).convert("RGB")
|
cover = cover.quantize(colors=256, method=2).convert("RGB")
|
||||||
else:
|
else:
|
||||||
cover = cover.quantize(colors=256, method=2)
|
cover = cover.quantize(colors=256, method=2)
|
||||||
|
|
||||||
# compress and save
|
# compress and save
|
||||||
print('compressing: ' + file)
|
print('compressing: ' + file)
|
||||||
cover.save(
|
cover.save(
|
||||||
@@ -34,42 +39,16 @@ def compressor(file, final_path, quality, resize, size):
|
|||||||
optimize=True,
|
optimize=True,
|
||||||
quality=quality,
|
quality=quality,
|
||||||
)
|
)
|
||||||
# resize and compress portrait images
|
|
||||||
elif resize and image.height >= size and image.height >= image.width:
|
|
||||||
# resize
|
def convert_to_webp(file, final_path):
|
||||||
print('resizing: ' + file)
|
filepath = os.path.join(final_path, file)
|
||||||
cover = resizeimage.resize_height(image, size)
|
|
||||||
# convert to 8 bit (if possible)
|
with Image.open(filepath) as image:
|
||||||
print('converting to 8 bit: ' + file)
|
print('converting to webp: ' + file)
|
||||||
if cover.format in ['JPG', 'JPEG']:
|
|
||||||
# TODO: JPG + JPEG are not converted to 8 bit
|
|
||||||
cover = cover.quantize(colors=256, method=2).convert("RGB")
|
|
||||||
else:
|
|
||||||
cover = cover.quantize(colors=256, method=2)
|
|
||||||
# compress and save
|
|
||||||
print('compressing: ' + file)
|
|
||||||
cover.save(
|
|
||||||
str(final_path) + '/' + 'min-' + file,
|
|
||||||
image.format,
|
|
||||||
optimize=True,
|
|
||||||
quality=quality,
|
|
||||||
)
|
|
||||||
# compress images
|
|
||||||
else:
|
|
||||||
# convert to 8 bit (if possible)
|
|
||||||
print('converting to 8 bit: ' + file)
|
|
||||||
if image.format in ['JPG', 'JPEG']:
|
|
||||||
# TODO: JPG + JPEG are not converted to 8 bit
|
|
||||||
image = image.quantize(colors=256, method=2).convert("RGB")
|
|
||||||
else:
|
|
||||||
image = image.quantize(colors=256, method=2)
|
|
||||||
# compress and save
|
|
||||||
print('compress: ' + file)
|
|
||||||
image.save(
|
image.save(
|
||||||
str(final_path) + "/" + "min-" + file,
|
str(final_path) + '/' + os.path.splitext(file)[0] + '.webp',
|
||||||
image.format,
|
format="WEBP"
|
||||||
optimize=True,
|
|
||||||
quality=quality,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -78,6 +57,7 @@ def usage():
|
|||||||
print('Compress and optionally resize all images (jpg|jpeg|png|webp|gif) in the current folder')
|
print('Compress and optionally resize all images (jpg|jpeg|png|webp|gif) in the current folder')
|
||||||
print(' -h | --help Prints this help')
|
print(' -h | --help Prints this help')
|
||||||
print(' -r | --resize Set this to resize the images')
|
print(' -r | --resize Set this to resize the images')
|
||||||
|
print(' -c | --convert Set this to convert images to WEBP')
|
||||||
print(' -q | --quality QUALITY Specify the quality (1-100, default 80)')
|
print(' -q | --quality QUALITY Specify the quality (1-100, default 80)')
|
||||||
print(' -s | --size SIZE Specify the size of the long side (default 900)')
|
print(' -s | --size SIZE Specify the size of the long side (default 900)')
|
||||||
|
|
||||||
@@ -85,7 +65,7 @@ def usage():
|
|||||||
def main(argv):
|
def main(argv):
|
||||||
# save arguments and options to variables
|
# save arguments and options to variables
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(argv, 'hrq:s:', ['help', 'resize', 'quality=', 'size='])
|
opts, args = getopt.getopt(argv, 'hrcq:s:', ['help', 'resize', 'convert', 'quality=', 'size='])
|
||||||
except getopt.GetoptError as err:
|
except getopt.GetoptError as err:
|
||||||
print(err)
|
print(err)
|
||||||
usage()
|
usage()
|
||||||
@@ -94,6 +74,7 @@ def main(argv):
|
|||||||
# default values
|
# default values
|
||||||
quality_img = 80
|
quality_img = 80
|
||||||
resize = False
|
resize = False
|
||||||
|
convert = False
|
||||||
size = 900
|
size = 900
|
||||||
|
|
||||||
# parse arguments
|
# parse arguments
|
||||||
@@ -102,13 +83,12 @@ def main(argv):
|
|||||||
usage()
|
usage()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif o in ('-r', '--resize'):
|
elif o in ('-r', '--resize'):
|
||||||
print(o)
|
|
||||||
resize = True
|
resize = True
|
||||||
|
elif o in ('-c', '--convert'):
|
||||||
|
convert = True
|
||||||
elif o in ('-q', '--quality'):
|
elif o in ('-q', '--quality'):
|
||||||
print(o, a)
|
|
||||||
quality_img = int(a)
|
quality_img = int(a)
|
||||||
elif o in ('-s', '--size'):
|
elif o in ('-s', '--size'):
|
||||||
print(o, a)
|
|
||||||
size = int(a)
|
size = int(a)
|
||||||
else:
|
else:
|
||||||
assert False, 'unhandled option'
|
assert False, 'unhandled option'
|
||||||
@@ -120,7 +100,7 @@ def main(argv):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# if save directory exists
|
# if save directory exists
|
||||||
if 'compress' in os.listdir():
|
if directory_name in os.listdir():
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
# if save directory does not exist
|
# if save directory does not exist
|
||||||
@@ -129,16 +109,19 @@ def main(argv):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
# only support these 4 formats
|
|
||||||
formats = ('.jpg', '.jpeg', '.png', '.webp', '.gif')
|
|
||||||
|
|
||||||
# get all files
|
# get all files
|
||||||
for file in os.listdir(os.getcwd()):
|
for file in os.listdir(os.getcwd()):
|
||||||
# check file and compress file
|
# check file and compress file
|
||||||
if os.path.splitext(file)[1].lower() in formats:
|
if os.path.splitext(file)[1].lower() in ('.jpg', '.jpeg', '.png', '.webp', '.gif'):
|
||||||
# call compress function
|
# call compress function
|
||||||
compressor(file, final_path, quality_img, resize, size)
|
compressor(file, final_path, quality_img, resize, size)
|
||||||
|
|
||||||
|
# convert to webp
|
||||||
|
if convert:
|
||||||
|
for file in os.listdir(final_path):
|
||||||
|
if os.path.splitext(file)[1].lower() in ('.jpg', '.jpeg', '.png', '.gif'):
|
||||||
|
convert_to_webp(file, final_path)
|
||||||
|
|
||||||
print("Done")
|
print("Done")
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user