chore: finish icons builder
- finish icons builder in scripts.
This commit is contained in:
@ -1,51 +1,53 @@
|
||||
import logging
|
||||
import logging, os
|
||||
from pathlib import Path
|
||||
import common
|
||||
from common import AssetKind
|
||||
import PIL, PIL.Image
|
||||
|
||||
# the config for thumbnail
|
||||
# The HW size of thumbnail
|
||||
THUMBNAIL_SIZE: int = 16
|
||||
|
||||
class ThumbnailBuilder():
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
def _create_thumbnail(src_file: Path, dst_file: Path) -> None:
|
||||
# open image
|
||||
src_image: PIL.Image.Image = PIL.Image.open(src_file)
|
||||
# create thumbnail
|
||||
src_image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE))
|
||||
# save to new file
|
||||
src_image.save(dst_file)
|
||||
|
||||
def build_thumbnails(self) -> None:
|
||||
# get folder path
|
||||
root_folder = common.get_root_folder()
|
||||
|
||||
# prepare handler
|
||||
def folder_handler(rel_name: str, src_folder: Path, dst_folder: Path) -> None:
|
||||
# just create folder
|
||||
logging.info(f'Creating Folder: {src_folder} -> {dst_folder}')
|
||||
dst_folder.mkdir(parents=False, exist_ok=True)
|
||||
def file_handler(rel_name: str, src_file: Path, dst_file: Path) -> None:
|
||||
# skip non-image
|
||||
if src_file.suffix != '.png': return
|
||||
# call thumbnail func
|
||||
logging.info(f'Building Thumbnail: {src_file} -> {dst_file}')
|
||||
self.__resize_image(src_file, dst_file)
|
||||
def build_icons() -> None:
|
||||
raw_icons_dir = common.get_raw_assets_folder(AssetKind.Icons)
|
||||
plg_icons_dir = common.get_plugin_assets_folder(AssetKind.Icons)
|
||||
|
||||
# call common processor
|
||||
common.common_file_migrator(
|
||||
root_folder / 'raw_icons',
|
||||
root_folder / 'icons',
|
||||
folder_handler,
|
||||
file_handler
|
||||
)
|
||||
# TODO: If we have Python 3.12, use Path.walk instead of current polyfill.
|
||||
|
||||
logging.info('Building thumbnail done.')
|
||||
# Icon assets has subdirectory, so we need use another way to process.
|
||||
for root, dirs, files in os.walk(raw_icons_dir):
|
||||
root = Path(root)
|
||||
|
||||
# Iterate folders
|
||||
for name in dirs:
|
||||
# Fetch directory path
|
||||
raw_icon_subdir = root / name
|
||||
plg_icon_subdir = plg_icons_dir / raw_icon_subdir.relative_to(raw_icons_dir)
|
||||
# Show message
|
||||
logging.info(f'Creating Folder: {raw_icon_subdir} -> {plg_icon_subdir}')
|
||||
# Create directory
|
||||
plg_icon_subdir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Iterate files
|
||||
for name in files:
|
||||
# Fetch file path
|
||||
raw_icon_file = root / name
|
||||
plg_icon_file = plg_icons_dir / raw_icon_file.relative_to(raw_icons_dir)
|
||||
# Show message
|
||||
logging.info(f'Building Thumbnail: {raw_icon_file} -> {plg_icon_file}')
|
||||
# Create thumbnail
|
||||
_create_thumbnail(raw_icon_file, plg_icon_file)
|
||||
|
||||
def __resize_image(self, src_file: Path, dst_file: Path) -> None:
|
||||
# open image
|
||||
src_image: PIL.Image.Image = PIL.Image.open(src_file)
|
||||
# create thumbnail
|
||||
src_image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE))
|
||||
# save to new file
|
||||
src_image.save(dst_file)
|
||||
|
||||
if __name__ == '__main__':
|
||||
common.setup_logging()
|
||||
thumbnail_builder = ThumbnailBuilder()
|
||||
thumbnail_builder.build_thumbnails()
|
||||
build_icons()
|
||||
|
Reference in New Issue
Block a user