| 123456789101112131415161718192021222324252627282930313233343536 |
- from PIL import Image
- img = Image.open(r"C:\Code\ML\Image\Card\b2.jpg")
- min_rect = [
- [
- 1616.630615234375,
- 2060.99365234375
- ],
- [
- 1752.767822265625,
- 1715.654296875
- ],
- -62.34006881713867
- ]
- img_w, img_h = img.size
- (center_x, center_y), (rect_w, rect_h), angle = min_rect
- center_x = int(center_x)
- center_y = int(center_y)
- rect_w = int(rect_w)
- rect_h = int(rect_h)
- # 缩放比例, 根据倾斜角度来
- resize_scale = 1.5 - abs(abs(angle % 90) - 45) / 90
- side_length = max(max(rect_w, rect_h) * resize_scale, 100)
- half_side = side_length / 2
- print()
- left, top = max(0, center_x - half_side), max(0, center_y - half_side)
- right, bottom = min(img_w, center_x + half_side), min(img_h, center_y + half_side)
- cropped_img = img.crop((left, top, right, bottom))
- print()
|