Created
May 18, 2022 22:04
-
-
Save austinbhale/8faeeac4b5bf81716f4cc54dabe71060 to your computer and use it in GitHub Desktop.
Resize any image into a square logo using OpenCV & NumPy!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
logo_name = "original-logo.png" | |
logo = cv2.imread(logo_name, cv2.IMREAD_UNCHANGED) | |
assert(logo.any()) | |
h, w, s = logo.shape | |
if w > h: | |
out_img = np.zeros((w, w, s)) | |
yoff = round((w-h)/2) | |
out_img[yoff:yoff+h, :w] = logo | |
else: | |
out_img = np.zeros((h, h, s)) | |
xoff = round((h-w)/2) | |
out_img[:h, xoff:xoff+h] = logo | |
resize_square = 150 | |
if resize_square: | |
out_img = cv2.resize(out_img, (resize_square, resize_square), interpolation=cv2.INTER_AREA) | |
h, w, _ = out_img.shape | |
cv2.imwrite("{}_{}_{}".format(w, h, logo_name), out_img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment