Полностью переделан модуль resize.py
Тесты
This commit is contained in:
62
resize.py
62
resize.py
@ -4,43 +4,51 @@ import os, sys
|
||||
from PIL import Image
|
||||
from PIL import ImageEnhance
|
||||
import argparse
|
||||
CROP_DEFAULT = 'default'
|
||||
CROP_WHITESPACE = 'whitespace'
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--width", dest="width", type=int, required=True)
|
||||
parser.add_argument("--height", dest="height", type=int, required=True)
|
||||
parser.add_argument("--crop", action="store_true", required=False)
|
||||
parser.add_argument("--keep-aspect-ratio", dest="keep_aspect_ratio", action="store_true", required=False)
|
||||
parser.add_argument("--width", type=int)
|
||||
parser.add_argument("--height", type=int)
|
||||
parser.add_argument("--crop", choices=[CROP_WHITESPACE, CROP_DEFAULT])
|
||||
parser.add_argument("--enhance", action="store_true")
|
||||
parser.add_argument("source", type=str)
|
||||
parser.add_argument("destination", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
size = args.width, args.height
|
||||
infile = args.source
|
||||
outfile = args.destination
|
||||
try:
|
||||
im = Image.open(infile)
|
||||
im = im.convert()
|
||||
if args.crop:
|
||||
width_crop = im.size[0]
|
||||
height_crop = int(im.size[0]/(args.width/args.height))
|
||||
width_padding = 0
|
||||
height_padding = int((im.size[1]-height_crop)/2)
|
||||
if height_crop > im.size[1]:
|
||||
width_crop = int(im.size[1]/(args.height/args.width))
|
||||
height_crop = im.size[1]
|
||||
width_padding = int((im.size[0]-width_crop)/2)
|
||||
height_padding = 0
|
||||
print((width_padding, height_padding, width_crop+width_padding, height_crop+height_padding))
|
||||
im_crop = im.crop((width_padding, height_padding, width_crop+width_padding, height_crop+height_padding))
|
||||
im_result = im_crop.resize(size, Image.ANTIALIAS)
|
||||
else:
|
||||
im.thumbnail(size, Image.ANTIALIAS)
|
||||
if args.keep_aspect_ratio:
|
||||
im_result = im
|
||||
if args.width is not None or args.height is not None:
|
||||
if args.width is None:
|
||||
size = (int(im.size[0] * args.height / im.size[1]), args.height)
|
||||
elif args.height is None:
|
||||
size = (args.width, int(im.size[1] * args.width / im.size[0]))
|
||||
else:
|
||||
im_result = Image.new('RGB', (args.width, args.height), (255, 255, 255))
|
||||
im_result.paste(im, ((args.width - im.size[0]) / 2, (args.height - im.size[1]) / 2))
|
||||
enhancer = ImageEnhance.Contrast(im_result)
|
||||
im_result = enhancer.enhance(1.05)
|
||||
im_result.save(outfile, "JPEG", quality=100)
|
||||
size = (args.width, args.height)
|
||||
if args.crop:
|
||||
if args.crop == CROP_DEFAULT:
|
||||
width_crop = im.size[0]
|
||||
height_crop = int(im.size[0]/(size[0]/size[1]))
|
||||
x = 0
|
||||
y = int((im.size[1]-height_crop)/2)
|
||||
if height_crop > im.size[1]:
|
||||
width_crop = int(im.size[1]/(size[1]/size[0]))
|
||||
height_crop = im.size[1]
|
||||
x = int((im.size[0]-width_crop)/2)
|
||||
y = 0
|
||||
im = im.crop((x, y, width_crop+x, height_crop+y))
|
||||
im.thumbnail(size, Image.ANTIALIAS)
|
||||
if args.crop == CROP_WHITESPACE:
|
||||
im_whitespace = Image.new('RGB', (size[0], size[1]), (255, 255, 255))
|
||||
im_whitespace.paste(im, ((size[0] - im.size[0]) // 2, (size[1] - im.size[1]) // 2))
|
||||
im = im_whitespace
|
||||
else:
|
||||
im.thumbnail(size, Image.ANTIALIAS)
|
||||
if args.enhance:
|
||||
enhancer = ImageEnhance.Contrast(im)
|
||||
im = enhancer.enhance(1.05)
|
||||
im.save(outfile, "JPEG", quality=100)
|
||||
except IOError:
|
||||
print("cannot create thumbnail for '%s'" % infile)
|
||||
|
13
square.py
13
square.py
@ -1,13 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("square", type=int,
|
||||
help="display a square of a given number")
|
||||
parser.add_argument("-v", "--verbose", action="store_true",
|
||||
help="increase output verbosity")
|
||||
args = parser.parse_args()
|
||||
answer = args.square**2
|
||||
if args.verbose:
|
||||
print("the square of {} equals {}".format(args.square, answer))
|
||||
else:
|
||||
print(answer)
|
11
test_resize.sh
Executable file
11
test_resize.sh
Executable file
@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
./resize.py --width=100 origin.jpg resize_w100.jpg
|
||||
./resize.py --width=100 --crop default origin.jpg resize_w100_crop.jpg
|
||||
./resize.py --width=100 --crop whitespace origin.jpg resize_w100_crop_whitespace.jpg
|
||||
./resize.py --height=100 origin.jpg resize_h100.jpg
|
||||
./resize.py --height=100 --crop default origin.jpg resize_h100_crop.jpg
|
||||
./resize.py --height=100 --crop whitespace origin.jpg resize_h100_crop_whitespace.jpg
|
||||
./resize.py --width=100 --height=100 origin.jpg resize_w100_h100.jpg
|
||||
./resize.py --width=100 --height=100 --crop default origin.jpg resize_w100_h100_crop.jpg
|
||||
./resize.py --width=100 --height=100 --crop whitespace origin.jpg resize_w100_h100_crop_whitespace.jpg
|
||||
./resize.py --enhance origin.jpg enhance.jpg
|
BIN
thumbnail.jpg
BIN
thumbnail.jpg
Binary file not shown.
Before Width: | Height: | Size: 10 KiB |
Reference in New Issue
Block a user