test01.py 816 B

123456789101112131415161718192021222324252627282930313233343536
  1. from PIL import Image
  2. img = Image.open(r"C:\Code\ML\Image\Card\b2.jpg")
  3. min_rect = [
  4. [
  5. 1616.630615234375,
  6. 2060.99365234375
  7. ],
  8. [
  9. 1752.767822265625,
  10. 1715.654296875
  11. ],
  12. -62.34006881713867
  13. ]
  14. img_w, img_h = img.size
  15. (center_x, center_y), (rect_w, rect_h), angle = min_rect
  16. center_x = int(center_x)
  17. center_y = int(center_y)
  18. rect_w = int(rect_w)
  19. rect_h = int(rect_h)
  20. # 缩放比例, 根据倾斜角度来
  21. resize_scale = 1.5 - abs(abs(angle % 90) - 45) / 90
  22. side_length = max(max(rect_w, rect_h) * resize_scale, 100)
  23. half_side = side_length / 2
  24. print()
  25. left, top = max(0, center_x - half_side), max(0, center_y - half_side)
  26. right, bottom = min(img_w, center_x + half_side), min(img_h, center_y + half_side)
  27. cropped_img = img.crop((left, top, right, bottom))
  28. print()