|
@@ -6,51 +6,39 @@ import multiprocessing as mp
|
|
import os
|
|
import os
|
|
import shutil
|
|
import shutil
|
|
|
|
|
|
-from PIL import Image
|
|
|
|
|
|
+from cvargparse import Arg
|
|
from cvargparse import BaseParser
|
|
from cvargparse import BaseParser
|
|
-from tqdm.auto import tqdm
|
|
|
|
-from cvargparse import cvdataclass
|
|
|
|
from functools import partial
|
|
from functools import partial
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
|
|
+from tqdm.auto import tqdm
|
|
|
|
|
|
-@cvdataclass
|
|
|
|
-class Args:
|
|
|
|
- source: str = "source"
|
|
|
|
- destination: str = "dest"
|
|
|
|
- n_jobs: int = -1
|
|
|
|
- extensions: tuple = (".jpg", ".jpeg", ".png")
|
|
|
|
-
|
|
|
|
- size: int = 1000
|
|
|
|
- fit_short: bool = False
|
|
|
|
-
|
|
|
|
- remove_existing: bool = False
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+from cvdatasets.utils.image import read_image
|
|
|
|
|
|
def resize(name, *, source: Path, dest: Path, size: int, fit_short: bool = False):
|
|
def resize(name, *, source: Path, dest: Path, size: int, fit_short: bool = False):
|
|
src = source / name
|
|
src = source / name
|
|
dst = dest / name
|
|
dst = dest / name
|
|
|
|
|
|
|
|
+ im = read_image(src)
|
|
|
|
+ w, h = im.size
|
|
|
|
|
|
- with Image.open(src) as im:
|
|
|
|
- w, h = im.size
|
|
|
|
-
|
|
|
|
- if fit_short:
|
|
|
|
- if w > h:
|
|
|
|
- W, H = (int(size * w / h), size)
|
|
|
|
- else:
|
|
|
|
- W, H = (size, int(size * h / w))
|
|
|
|
|
|
+ if fit_short:
|
|
|
|
+ if w > h:
|
|
|
|
+ W, H = (int(size * w / h), size)
|
|
|
|
+ else:
|
|
|
|
+ W, H = (size, int(size * h / w))
|
|
|
|
|
|
|
|
+ else:
|
|
|
|
+ if w > h:
|
|
|
|
+ W, H = (size, int(size * h / w))
|
|
else:
|
|
else:
|
|
- if w > h:
|
|
|
|
- W, H = (size, int(size * h / w))
|
|
|
|
- else:
|
|
|
|
- W, H = (int(size * w / h), size)
|
|
|
|
|
|
+ W, H = (int(size * w / h), size)
|
|
|
|
+
|
|
|
|
+ dst.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
+ im.resize((W, H)).save(dst)
|
|
|
|
|
|
- dst.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
- im.resize((W, H)).save(dst)
|
|
|
|
|
|
|
|
-def main(args: Args):
|
|
|
|
|
|
+def main(args):
|
|
|
|
+
|
|
source = Path(args.source)
|
|
source = Path(args.source)
|
|
destination = Path(args.destination).resolve()
|
|
destination = Path(args.destination).resolve()
|
|
assert source.exists(), \
|
|
assert source.exists(), \
|
|
@@ -93,5 +81,26 @@ def main(args: Args):
|
|
work(imname)
|
|
work(imname)
|
|
|
|
|
|
|
|
|
|
-args = BaseParser(Args).parse_args()
|
|
|
|
-main(args)
|
|
|
|
|
|
+
|
|
|
|
+parser = BaseParser([
|
|
|
|
+ Arg("source"),
|
|
|
|
+
|
|
|
|
+ Arg("destination"),
|
|
|
|
+
|
|
|
|
+ Arg.int("--n_jobs", "-j", default=-1,
|
|
|
|
+ help="number of concurrent resize processes"),
|
|
|
|
+
|
|
|
|
+ Arg("--extensions", "-ext", nargs="+", default=[".jpg", ".jpeg", ".png"],
|
|
|
|
+ help="file extensions to processs"),
|
|
|
|
+
|
|
|
|
+ Arg.int("--size", "-s", default=1000,
|
|
|
|
+ help="size in pixels"),
|
|
|
|
+
|
|
|
|
+ Arg.flag("--fit_short",
|
|
|
|
+ help="resize to the given size the short size or the long size (default)."),
|
|
|
|
+
|
|
|
|
+ Arg.flag("--remove_existing",
|
|
|
|
+ help="remove existing images."),
|
|
|
|
+])
|
|
|
|
+
|
|
|
|
+main(parser.parse_args())
|