Add --inplace argument to overwrite input file.
This commit is contained in:
parent
17afe6a9b9
commit
cb84584202
1 changed files with 18 additions and 7 deletions
19
resize.py
19
resize.py
|
@ -6,9 +6,15 @@ import sys
|
|||
from voussoirkit import imagetools
|
||||
from voussoirkit import winglob
|
||||
|
||||
filenames = sys.argv[1]
|
||||
|
||||
def resize(filename, new_x=None, new_y=None, scale=None, nearest_neighbor=False):
|
||||
def resize(
|
||||
filename,
|
||||
new_x=None,
|
||||
new_y=None,
|
||||
*,
|
||||
inplace=False,
|
||||
nearest_neighbor=False,
|
||||
scale=None,
|
||||
):
|
||||
i = Image.open(filename)
|
||||
|
||||
(image_width, image_height) = i.size
|
||||
|
@ -18,7 +24,6 @@ def resize(filename, new_x=None, new_y=None, scale=None, nearest_neighbor=False)
|
|||
elif scale:
|
||||
new_x = int(image_width * scale)
|
||||
new_y = int(image_height * scale)
|
||||
print(new_x, new_y)
|
||||
|
||||
if new_x == 0:
|
||||
(new_x, new_y) = imagetools.fit_into_bounds(image_width, image_height, 10000000, new_y)
|
||||
|
@ -30,6 +35,10 @@ def resize(filename, new_x=None, new_y=None, scale=None, nearest_neighbor=False)
|
|||
i = i.resize( (new_x, new_y), Image.NEAREST)
|
||||
else:
|
||||
i = i.resize( (new_x, new_y), Image.ANTIALIAS)
|
||||
|
||||
if inplace:
|
||||
newname = filename
|
||||
else:
|
||||
suffix = '_{width}x{height}'.format(width=new_x, height=new_y)
|
||||
(base, extension) = os.path.splitext(filename)
|
||||
newname = base + suffix + extension
|
||||
|
@ -45,6 +54,7 @@ def resize_argparse(args):
|
|||
args.new_y,
|
||||
scale=args.scale,
|
||||
nearest_neighbor=args.nearest_neighbor,
|
||||
inplace=args.inplace,
|
||||
)
|
||||
|
||||
def main(argv):
|
||||
|
@ -55,6 +65,7 @@ def main(argv):
|
|||
parser.add_argument('new_y', nargs='?', type=int, default=None)
|
||||
parser.add_argument('--scale', dest='scale', type=float, default=None)
|
||||
parser.add_argument('--nearest', dest='nearest_neighbor', action='store_true')
|
||||
parser.add_argument('--inplace', dest='inplace', action='store_true')
|
||||
parser.set_defaults(func=resize_argparse)
|
||||
|
||||
args = parser.parse_args(argv)
|
||||
|
|
Loading…
Reference in a new issue