ImageTool.py 700 B

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