Created
October 12, 2022 16:07
-
-
Save dfaker/28550d22cc4f16ff274e61be54c355a0 to your computer and use it in GitHub Desktop.
Quick and dirty square cropper
This file contains hidden or 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 os | |
import numpy as np | |
import sys | |
print(""" | |
Pass a folder of images as a prameter: | |
script.py Z:\\somefolder\\somewhere | |
Space for next image. | |
Mousewheel for resizing the box | |
Click to capture the hilighted square | |
images are saved into \\outdir\\ | |
""") | |
if len(sys.argv)<2: | |
print('Pass in a source folder as a parameter') | |
exit() | |
source_folder = sys.argv[-1] | |
if not os.path.isdir(source_folder): | |
print('source_folder', source_folder, 'does not exist.') | |
exit() | |
print('source_folder', source_folder) | |
files = [] | |
for f in os.listdir(source_folder): | |
files.append(os.path.join(source_folder, f)) | |
cv2.namedWindow("imageWindow", cv2.WND_PROP_FULLSCREEN) | |
cv2.setWindowProperty("imageWindow",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN) | |
lastx, lasty = 0, 0 | |
dim = 512 | |
grabNow = False | |
def on_mouse(event, x, y, buttons, param): | |
global lastx, lasty, dim, grabNow | |
lastx, lasty = (x, y) | |
if event == cv2.EVENT_MOUSEWHEEL: | |
if buttons > 0: | |
dim += 10 | |
elif buttons < 0: | |
dim -= 10 | |
dim = max(10,dim) | |
elif event == cv2.EVENT_LBUTTONDOWN: | |
grabNow = True | |
cv2.setMouseCallback('imageWindow', on_mouse) | |
n = 0 | |
font = cv2.FONT_HERSHEY_SIMPLEX | |
for f in files: | |
im = cv2.imread(f) | |
if im is None: | |
continue | |
rect = cv2.getWindowImageRect('imageWindow') | |
_, _, w, h = rect | |
old_size = im.shape[:2] | |
target_size = min(w, h) | |
ratio = float(target_size)/max(old_size) | |
new_size = tuple([int(x*ratio) for x in old_size]) | |
im = cv2.resize(im, (new_size[1], new_size[0])) | |
delta_w = w - new_size[1] | |
delta_h = h - new_size[0] | |
top, bottom = delta_h//2, delta_h-(delta_h//2) | |
left, right = delta_w//2, delta_w-(delta_w//2) | |
color = [0, 0, 0] | |
padded_im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) | |
k = 0 | |
lastsave=None | |
while 1: | |
fg = padded_im.copy() | |
colour = (255, 255, 255) | |
if dim == 512: | |
colour = (0, 255, 0) | |
elif dim < 512: | |
colour = (0, 0, 255) | |
if lastsave is not None: | |
fg = cv2.putText(fg, 'Saved:{}'.format(lastsave), (0, 11), font, 0.4, (0, 0, 0), 2, cv2.LINE_AA) | |
fg = cv2.putText(fg, 'Saved:{}'.format(lastsave), (0, 11), font, 0.4, (255, 255, 255), 1, cv2.LINE_AA) | |
fg = cv2.rectangle(fg, (lastx-(dim//2), lasty-(dim//2)), (lastx+(dim//2), lasty+(dim//2)), colour, 1) | |
if grabNow: | |
os.path.exists('outdir') or os.mkdir('outdir') | |
fn = os.path.basename(f) | |
outfile = os.path.join('outdir', '{}_{}_out.png'.format(fn, n)) | |
cv2.imwrite(outfile, padded_im[lasty-(dim//2):lasty+(dim//2), lastx-(dim//2):lastx+(dim//2), :]) | |
lastsave = outfile | |
n += 1 | |
grabNow = False | |
cv2.imshow('imageWindow', fg) | |
k = cv2.waitKey(1) | |
if k != -1: | |
break | |
if k == ord('q'): | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment