img_tools.py 715 B

12345678910111213141516171819202122
  1. import cv2
  2. def img_resize(img_path):
  3. img = cv2.imread(img_path)
  4. width = img.shape[1]
  5. height = img.shape[0]
  6. if height != 48:
  7. ratio = 35 / height
  8. height = 35
  9. width = int(width * ratio)
  10. dim = (width, height)
  11. resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
  12. width = resized.shape[1]
  13. height = resized.shape[0]
  14. if width < 960:
  15. left = int((960 - width) / 2)
  16. top = int((48 - height) / 2)
  17. ret = cv2.copyMakeBorder(resized, top, 48 - height - top, left, 960 - width - left, cv2.BORDER_CONSTANT,
  18. value=(255, 255, 255))
  19. cv2.imwrite(img_path, ret)