Skip to content

Commit 7c71596

Browse files
committed
[update] add scale checking
1 parent 5dcd31d commit 7c71596

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

main.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
if __name__ == '__main__':
66

7-
# Load config
7+
# load config
88
opt = Option()
99
img_paths = opt.img_paths
1010

11-
# Pick first one
11+
# pick first one
1212
raw_img = cv2.imread(opt.get_first_one().absolute().as_posix())
1313

14-
# Choose how to crop
14+
# choose how to crop
1515
cropper = Cropper(raw_img)
1616

1717
if cropper.cropped:
@@ -23,7 +23,7 @@
2323

2424
last_size = None
2525

26-
# List all images to crop
26+
# list all images to crop
2727
for img_path in img_paths:
2828

2929
img = cv2.imread(img_path.absolute().as_posix())
@@ -54,6 +54,6 @@
5454
else:
5555
last_size = size
5656

57-
# Cropping other images
57+
# cropping other images
5858
cropped_img = img[starty:endy, startx:endx, :]
5959
cv2.imwrite((opt.output_path / img_path.name).absolute().as_posix(), cropped_img)

utils/cropper.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
21
from tkinter import Tk
3-
42
import numpy as np
5-
63
import cv2
74

8-
95
class Cropper:
106

117
def __init__(self, img) -> None:
128

13-
self.ratio = 0.7
14-
15-
self.screen_width, self.screen_height = self.get_screen_info()
169
self.img = img
10+
self.screen_width, self.screen_height = self.get_screen_info()
1711

1812
height, width, _ = self.img.shape
13+
width_scale = width / self.screen_width
14+
height_scale = height / self.screen_height
1915

20-
if width / self.screen_width > self.ratio or height / self.screen_height > self.ratio:
16+
# check if image is too large, we need to resize it
17+
if width_scale > 1 or height_scale > 1:
18+
self.ratio = 1 / max(width_scale, height_scale)
2119
self.img = cv2.resize(self.img, None, fx=self.ratio, fy=self.ratio)
2220
else:
2321
self.ratio = 1.0

0 commit comments

Comments
 (0)