ImageTool.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import os
  2. from datetime import datetime
  3. import cv2
  4. from PIL import Image, UnidentifiedImageError
  5. from PySide6.QtCore import Qt
  6. from PySide6.QtGui import QImage, QPixmap
  7. from PySide6.QtWidgets import QLabel
  8. from logger import logger
  9. def rotate_image(path_file: str, gradus: int = -90) -> str:
  10. try:
  11. image = Image.open(path_file, 'r')
  12. rotate_path_image = os.path.join(
  13. os.path.dirname(image.filename),
  14. F"rotate_{os.path.split(image.filename)[1]}"
  15. )
  16. rotate_img = image.rotate(gradus, expand=True)
  17. rotate_img.save(rotate_path_image)
  18. return rotate_path_image
  19. except FileNotFoundError:
  20. logger.error(F"Файл не найден: {path_file=}")
  21. return None
  22. except UnidentifiedImageError:
  23. logger.error(F"Не правильный формат: {path_file=}")
  24. return None
  25. def cupture_face(path_photo: str, new_path: str) -> None:
  26. try:
  27. img = cv2.imread(path_photo)
  28. face_recog = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt_tree.xml')
  29. face_result = face_recog.detectMultiScale(img, scaleFactor=2, minNeighbors=3)
  30. if len(face_result) != 0:
  31. logger.info(F"Find face: {face_result}")
  32. x,y,w,h = face_result[-1]
  33. img = img[y-50:y+h+80,x-50:x+50+w]
  34. cv2.imwrite(new_path, img)
  35. except cv2.error as err:
  36. logger.error(err)
  37. def load_image(qlabel: QLabel, path_file: str) -> None:
  38. logger.info(F"Set image to label: {path_file}")
  39. qlabel.setPixmap(QPixmap(QImage(path_file)).scaled(
  40. qlabel.width()-4,
  41. qlabel.height(),
  42. Qt.AspectRatioMode.KeepAspectRatio,
  43. Qt.TransformationMode.FastTransformation
  44. ))