ImageTool.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from PIL import Image, UnidentifiedImageError
  2. import os
  3. from logger import logger
  4. from datetime import datetime
  5. import cv2
  6. def rotate_image(path_file: str, gradus: int = -90) -> str:
  7. try:
  8. image = Image.open(path_file, 'r')
  9. rotate_path_image = os.path.join(
  10. os.path.dirname(image.filename),
  11. F"rotate_{os.path.split(image.filename)[1]}"
  12. )
  13. rotate_img = image.rotate(gradus, expand=True)
  14. rotate_img.save(rotate_path_image)
  15. return rotate_path_image
  16. except FileNotFoundError:
  17. logger.error(F"Файл не найден: {path_file=}")
  18. return None
  19. except UnidentifiedImageError:
  20. logger.error(F"Не правильный формат: {path_file=}")
  21. return None
  22. def cupture_face(path_photo: str, new_path: str) -> None:
  23. try:
  24. img = cv2.imread(path_photo)
  25. face_recog = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt_tree.xml')
  26. face_result = face_recog.detectMultiScale(img, scaleFactor=2, minNeighbors=3)
  27. if len(face_result) != 0:
  28. logger.info(F"Find face: {face_result}")
  29. x,y,w,h = face_result[-1]
  30. img = img[y-50:y+h+80,x-50:x+50+w]
  31. cv2.imwrite(new_path, img)
  32. except cv2.error as err:
  33. logger.error(err)