From c5d8fbf6e85c7b88af0e26f5e5f25cc1652ddb96 Mon Sep 17 00:00:00 2001 From: Jeena Date: Thu, 26 Aug 2021 00:23:14 +0900 Subject: [PATCH] Add crop and resize functionality --- Pipfile | 1 + Pipfile.lock | 74 ++++++++++++++++++++++++++++++++++++++++--- edit.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ha.py | 20 +++++++++--- 4 files changed, 174 insertions(+), 9 deletions(-) create mode 100755 edit.py diff --git a/Pipfile b/Pipfile index 44c65b4..6dea604 100644 --- a/Pipfile +++ b/Pipfile @@ -7,6 +7,7 @@ name = "pypi" google-auth-oauthlib = "*" psycopg2-binary = "*" google-api-python-client = "*" +opencv-python = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index d565a89..73663b5 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "b35e976d9f0bbffebb2b0df18c4fb553d9ccf4cb3721df71dc60b46f44de7726" + "sha256": "4adb8a2006b657ec935e53c94bdbbd772d4460f29ea7e062a32b5c1685cb055b" }, "pipfile-spec": 6, "requires": { @@ -49,11 +49,11 @@ }, "google-api-python-client": { "hashes": [ - "sha256:20bc8f711883f9f8f96d96bc44b40ad0e2b0a9ef7df53e7c738948584fddd1c2", - "sha256:26402b51b21480e5f2de2650f4dc41d19db031ce28d26047f30dad0803fbf27d" + "sha256:6e990fc4d0419c2011f75ca5c2762efbb7eb4def67bbe2f1b98a8ccb73117bf5", + "sha256:a25661ec6cf4c159f41fe9c061c2bee31b2dddaf2ad787e23617048a25b53842" ], "index": "pypi", - "version": "==2.17.0" + "version": "==2.18.0" }, "google-auth": { "hashes": [ @@ -101,6 +101,42 @@ "markers": "python_version >= '3'", "version": "==3.2" }, + "numpy": { + "hashes": [ + "sha256:09858463db6dd9f78b2a1a05c93f3b33d4f65975771e90d2cf7aadb7c2f66edf", + "sha256:209666ce9d4a817e8a4597cd475b71b4878a85fa4b8db41d79fdb4fdee01dde2", + "sha256:298156f4d3d46815eaf0fcf0a03f9625fc7631692bd1ad851517ab93c3168fc6", + "sha256:30fc68307c0155d2a75ad19844224be0f2c6f06572d958db4e2053f816b859ad", + "sha256:423216d8afc5923b15df86037c6053bf030d15cc9e3224206ef868c2d63dd6dc", + "sha256:426a00b68b0d21f2deb2ace3c6d677e611ad5a612d2c76494e24a562a930c254", + "sha256:466e682264b14982012887e90346d33435c984b7fead7b85e634903795c8fdb0", + "sha256:51a7b9db0a2941434cd930dacaafe0fc9da8f3d6157f9d12f761bbde93f46218", + "sha256:52a664323273c08f3b473548bf87c8145b7513afd63e4ebba8496ecd3853df13", + "sha256:550564024dc5ceee9421a86fc0fb378aa9d222d4d0f858f6669eff7410c89bef", + "sha256:5de64950137f3a50b76ce93556db392e8f1f954c2d8207f78a92d1f79aa9f737", + "sha256:640c1ccfd56724f2955c237b6ccce2e5b8607c3bc1cc51d3933b8c48d1da3723", + "sha256:7fdc7689daf3b845934d67cb221ba8d250fdca20ac0334fea32f7091b93f00d3", + "sha256:805459ad8baaf815883d0d6f86e45b3b0b67d823a8f3fa39b1ed9c45eaf5edf1", + "sha256:92a0ab128b07799dd5b9077a9af075a63467d03ebac6f8a93e6440abfea4120d", + "sha256:9f2dc79c093f6c5113718d3d90c283f11463d77daa4e83aeeac088ec6a0bda52", + "sha256:a5109345f5ce7ddb3840f5970de71c34a0ff7fceb133c9441283bb8250f532a3", + "sha256:a55e4d81c4260386f71d22294795c87609164e22b28ba0d435850fbdf82fc0c5", + "sha256:a9da45b748caad72ea4a4ed57e9cd382089f33c5ec330a804eb420a496fa760f", + "sha256:b160b9a99ecc6559d9e6d461b95c8eec21461b332f80267ad2c10394b9503496", + "sha256:b342064e647d099ca765f19672696ad50c953cac95b566af1492fd142283580f", + "sha256:b5e8590b9245803c849e09bae070a8e1ff444f45e3f0bed558dd722119eea724", + "sha256:bf75d5825ef47aa51d669b03ce635ecb84d69311e05eccea083f31c7570c9931", + "sha256:c01b59b33c7c3ba90744f2c695be571a3bd40ab2ba7f3d169ffa6db3cfba614f", + "sha256:d96a6a7d74af56feb11e9a443150216578ea07b7450f7c05df40eec90af7f4a7", + "sha256:dd0e3651d210068d13e18503d75aaa45656eef51ef0b261f891788589db2cc38", + "sha256:e167b9805de54367dcb2043519382be541117503ce99e3291cc9b41ca0a83557", + "sha256:e42029e184008a5fd3d819323345e25e2337b0ac7f5c135b7623308530209d57", + "sha256:f545c082eeb09ae678dd451a1b1dbf17babd8a0d7adea02897a76e639afca310", + "sha256:fde50062d67d805bc96f1a9ecc0d37bfc2a8f02b937d2c50824d186aa91f2419" + ], + "markers": "python_version < '3.11' and python_version >= '3.7'", + "version": "==1.21.2" + }, "oauthlib": { "hashes": [ "sha256:42bf6354c2ed8c6acb54d971fce6f88193d97297e18602a3a886603f9d7730cc", @@ -109,6 +145,36 @@ "markers": "python_version >= '3.6'", "version": "==3.1.1" }, + "opencv-python": { + "hashes": [ + "sha256:05c5139d620e8d02f7ce0921796d55736fa19fa15e2ec00a388db2eb1ae1e9a1", + "sha256:085232718f28bddd265da480874c37db5c7354cb08f23f4a68a8639b16276a89", + "sha256:18a4a14015eee30d9cd514db8cdefbf594b1d5c234762d27abe512d62a333bc3", + "sha256:205a73adb29c37e42475645519e612e843a985475da993d10b4d5daa6afec36a", + "sha256:3c001d3feec7f3140f1fb78dfc52ca28122db8240826882d175a208a89d2731b", + "sha256:437f30e300725e1d1b3744dbfbc66a523a4744792b58f3dbe1e9140c8f4dfba5", + "sha256:5366fcd6eae4243add3c8c92142045850f1db8e464bcf0b75313e1596b2e3671", + "sha256:54c64e86a087841869901fd34462bb6bec01cd4652800fdf5d92fe7b0596c82f", + "sha256:6763729fcfee2a08e069aa1982c9a8c1abf55b9cdf2fb9640eda1d85bdece19a", + "sha256:68813b720b88e4951e84399b9a8a7b532d45a07a96ea8f539636242f862e32e0", + "sha256:7f41b97d84ac66bdf13cb4d9f4dad3e159525ba1e3f421e670c787ce536eb70a", + "sha256:831b92fe63ce18dd628f71104da7e60596658b75e2fa16b83aefa3eb10c115e2", + "sha256:881f3d85269500e0c7d72b140a6ebb5c14a089f8140fb9da7ce01f12a245858e", + "sha256:8852be06c0749fef0d9c58f532bbcb0570968c59e41cf56b90f5c92593c6e108", + "sha256:8b5bc61be7fc8565140b746288b370a4bfdb4edb9d680b66bb914e7690485db1", + "sha256:8d3282138f3a8646941089aae142684910ebe40776266448eab5f4bb609fc63f", + "sha256:9a78558b5ae848386edbb843c761e5fed5a8480be9af16274a5a78838529edeb", + "sha256:b42bbba9f5421865377c7960bd4f3dd881003b322a6bf46ed2302b89224d102b", + "sha256:c360cb76ad1ddbd5d2d3e730b42f2ff6e4be08ea6f4a6eefacca175d27467e8f", + "sha256:cdc3363c2911d7cfc6c9f55308c51c2841a7aecbf0bf5e791499d220ce89d880", + "sha256:e1f54736272830a1e895cedf7a4ee67737e31e966d380c82a81ef22515d043a3", + "sha256:e42c644a70d5c54f53a4b114dbd88b4eb83f42a9ca998f07bd5682f3f404efcc", + "sha256:f1bda4d144f5204e077ca4571453ebb2015e5748d5e0043386c92c2bbf7f52eb", + "sha256:f3ac2355217114a683f3f72a9c40a5890914a59c4a2df62e4083c66ff65c9cf9" + ], + "index": "pypi", + "version": "==4.5.3.56" + }, "protobuf": { "hashes": [ "sha256:13ee7be3c2d9a5d2b42a1030976f760f28755fcf5863c55b1460fd205e6cd637", diff --git a/edit.py b/edit.py new file mode 100755 index 0000000..804ffa3 --- /dev/null +++ b/edit.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 + +import cv2 +import sys + +def wait_with_check_closing(win_name): + """ + https://stackoverflow.com/questions/35003476/" + "opencv-python-how-to-detect-if-a-window-is-closed/37881722 + """ + while True: + keyCode = cv2.waitKey(50) + if keyCode != -1: + break + win_prop = cv2.getWindowProperty(win_name, cv2.WND_PROP_VISIBLE) + if win_prop <= 0: + break + +class Image: + def __init__(self, path, w=1920, h=1080): + self.w = w + self.h = h + self.path = path + self.image = cv2.imread(self.path) + + def crop(self): + oh, ow, z = self.image.shape + w = self.w + h = self.h + vertical = False + if oh/ow < 1: + # horizontal + if ow <= self.w: + w = ow + h = oh + else: + # need resizing to smaller + if self.w/ow < self.h/oh: + # hight priority + w = self.h/oh * ow + h = self.h + else: + w = self.w + h = self.w/ow * oh + else: + # vertical + vertical = True + if oh <= self.h: + w = ow + h = oh + else: + # need resizing to smaller + w = self.h/oh * ow + h = self.h + if vertical: + self.image = cv2.resize(self.image, (int(w), int(h))) + self.image = self.image[0:self.h, 0:self.w] + bg_image = cv2.resize(self.image.copy(), (self.w, self.h)) + bg_image = cv2.blur(bg_image, (200, 200)) + x_offset = int(self.image.shape[0] / 2) + y_offset = 0 + bg_image[y_offset:y_offset+self.image.shape[0], x_offset:x_offset+self.image.shape[1]] = self.image + self.image = bg_image + else: + self.image = cv2.resize(self.image, (int(w), int(h))) + # center zoom + x = self.image.shape[1]/2 - self.w/2 + y = self.image.shape[0]/2 - self.h/2 + self.image = self.image[int(y):int(y+self.h), int(x):int(x+self.w)] + + def safe(self, new_path): + cv2.imwrite(new_path, self.image) + + def show(self): + title = "Image" + cv2.imshow(title, self.image) + wait_with_check_closing(title) + cv2.destroyAllWindows() + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: edit.py path/to/picture.jpeg") + else: + img_path = sys.argv[1] + img = Image(img_path) + img.crop() + img.show() + img.safe("/home/jeena/Downloads/test.jpg") diff --git a/ha.py b/ha.py index 04f629b..d60ef2d 100755 --- a/ha.py +++ b/ha.py @@ -1,20 +1,30 @@ #!/usr/bin/env python3 import synology -# from wand.image import Image import os import shutil +import edit def prepare_photo(i, path): # sudo apt-get install ufraw-batch # convert 13895032967_642e23af42_o.jpg -adaptive-resize 1920x1080\> -size 1920x1080 xc:black +swap -gravity center -composite 01.jpg # filename, file_extension = os.path.splitext(path) - new_path = os.path.join(os.path.dirname(path), str(i) + '.jpg') - cmd = 'convert "' + path + '" -adaptive-resize 1920x1080\> -size 1920x1080 xc:black +swap -gravity center -composite ' + new_path + n_path = new_path(i, path) + cmd = 'convert "' + path + '" -adaptive-resize 1920x1080\> -size 1920x1080 xc:black +swap -gravity center -composite ' + n_path print(cmd) os.system(cmd) # os.rename(path, new_path) - return new_path + return n_path + +def new_path(i, path): + return os.path.join(os.path.dirname(path), str(i) + '.jpg') + +def resize_and_crop(i, path): + n_path = new_path(i, path) + img = edit.Image(path) + img.crop() + img.safe(n_path) + return n_path def upload_photo(remotepath, path): cmd = 'scp ' + path + ' ' + remotepath @@ -32,6 +42,6 @@ if __name__ == "__main__": with os.scandir(dirpath) as dirs: for i, entry in enumerate(dirs): - upload_photo(ha_path, prepare_photo(i, os.path.join(dirpath, entry.name))) + upload_photo(ha_path, resize_and_crop(i, os.path.join(dirpath, entry.name))) shutil.rmtree(dirpath)