Files
photohub/src/picture.py
2025-07-07 23:44:28 +02:00

76 lines
2.6 KiB
Python

from __future__ import annotations
from PIL import Image
import thread_manager
from path import Path
from typing import TYPE_CHECKING
import config
if TYPE_CHECKING:
from bulk_page import BulkPage
from bulk_category import BulkCategory
class Picture():
def __init__(self, picture_path: Path, id: int, page: BulkPage = None, raw: Path|None = None, is_repertoried: bool = True):
self._large: Path = picture_path
self._small: Path = Path(picture_path.get_absolute_path()[:-4] + "_small.jpg")
self._thumb: Path = Path(picture_path.get_absolute_path()[:-4] + "_thumb.jpg")
self._profile_file: Path = Path(picture_path.get_absolute_path() + ".out.pp3")
self._categories_file: Path = Path(picture_path.get_absolute_path()[:-4] + "_categories.txt")
self._raw: Path|None = raw
self.id: int = id
self._page: BulkPage = page
self._categories_name: list[str] = []
self.categories: list[BulkCategory] = []
if self._categories_file.exist():
with open(self._categories_file.get_absolute_path(), "r+") as f:
categories_name: list[str] = f.read()
if (len(categories_name)):
self._categories_name += categories_name.split("\n")
else:
self._categories_file.touch()
if (config.CREATE_GENERAL_CATEGORY):
self._categories_name.append("general")
self._is_reperoried: bool = is_repertoried
self.get_small()
self.get_thumb()
def get_page(self):
return self._page
def get_categories_name(self):
return self._categories_name
def get_small(self):
if not self._small.exist():
self.gen_small()
return self._small
def get_thumb(self):
if not self._thumb.exist():
self.gen_thumb()
return self._thumb
def get_large(self):
return self._large
def get_profile_file(self):
return self._profile_file
def gen_thumb(self):
def _(_large: Path, _thumb: Path):
im = Image.open(_large.get_absolute_path())
im.thumbnail(config.THUMB_DIMENSION)
im.save(_thumb.get_absolute_path(), "JPEG")
thread_manager.add_to_queu(_, (self._large, self._thumb))
def gen_small(self):
def _(_large: Path, _small: Path):
im = Image.open(_large.get_absolute_path()).convert("RGB")
im.save(_small.get_absolute_path(), quality=95, optimize=True)
thread_manager.add_to_queu(_, (self._large, self._small))