ImageTool.py 905 B

1234567891011121314151617181920212223242526272829
  1. from PIL import Image, UnidentifiedImageError
  2. import os
  3. from logger import logger
  4. from datetime import datetime
  5. def rotate_image(path_file: str, gradus: int = -90) -> str:
  6. try:
  7. image = Image.open(path_file, 'r')
  8. rotate_path_image = os.path.join(
  9. os.path.dirname(image.filename),
  10. F"rotate_{os.path.split(image.filename)[1]}"
  11. )
  12. rotate_img = image.rotate(gradus, expand=True)
  13. rotate_img.save(rotate_path_image)
  14. return rotate_path_image
  15. except FileNotFoundError:
  16. logger.error(F"Файл не найден: {path_file=}")
  17. return None
  18. except UnidentifiedImageError:
  19. logger.error(F"Не правильный формат: {path_file=}")
  20. return None
  21. def create_filename() -> str:
  22. return os.path.join(os.environ.get('PHOTO_DIR'), F"propusk_{datetime.now().timestamp()}.jpg")