Use argparse for crop.py.
This commit is contained in:
parent
cb84584202
commit
d5a59d9f9e
1 changed files with 41 additions and 17 deletions
56
crop.py
56
crop.py
|
@ -1,21 +1,45 @@
|
||||||
|
import argparse
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
import sys
|
||||||
|
|
||||||
filename = sys.argv[1]
|
from voussoirkit import winglob
|
||||||
(name, extension) = os.path.splitext(filename)
|
|
||||||
newname = '%s_cropped%s' % (name, extension)
|
|
||||||
|
|
||||||
crops = sys.argv[2:]
|
def crop(filename, crops, *, inplace=False):
|
||||||
crops = ' '.join(crops)
|
print(crops)
|
||||||
crops = crops.replace(',', ' ')
|
i = Image.open(filename)
|
||||||
crops = crops.replace(' ', ' ')
|
if len(crops) == 2:
|
||||||
crops = crops.split(' ')
|
|
||||||
crops = [int(x) for x in crops]
|
|
||||||
crops = list(crops)
|
|
||||||
print(crops)
|
|
||||||
i = Image.open(filename)
|
|
||||||
if len(crops) == 2:
|
|
||||||
crops.extend(i.size)
|
crops.extend(i.size)
|
||||||
i = i.crop(crops)
|
i = i.crop(crops)
|
||||||
i.save(newname, quality=100)
|
if inplace:
|
||||||
|
newname = filename
|
||||||
|
else:
|
||||||
|
suffix = '_'.join(str(x) for x in crops)
|
||||||
|
suffix = f'_{suffix}'
|
||||||
|
(base, extension) = os.path.splitext(filename)
|
||||||
|
newname = base + suffix + extension
|
||||||
|
i.save(newname, quality=100)
|
||||||
|
|
||||||
|
|
||||||
|
def crop_argparse(args):
|
||||||
|
filenames = winglob.glob(args.pattern)
|
||||||
|
for filename in filenames:
|
||||||
|
crop(
|
||||||
|
filename,
|
||||||
|
crops=args.crops,
|
||||||
|
inplace=args.inplace,
|
||||||
|
)
|
||||||
|
|
||||||
|
def main(argv):
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
|
||||||
|
parser.add_argument('pattern')
|
||||||
|
parser.add_argument('crops', nargs='+', type=int, default=None)
|
||||||
|
parser.add_argument('--inplace', dest='inplace', action='store_true')
|
||||||
|
parser.set_defaults(func=crop_argparse)
|
||||||
|
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
return args.func(args)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
raise SystemExit(main(sys.argv[1:]))
|
||||||
|
|
Loading…
Reference in a new issue