"""CPU-only sizing shared by validation and the reference-image UI.""" from PIL import Image from image_sizes import aspect_ratio REFERENCE_NOTICE = ( "参照画像がある場合は編集モード。先頭画像のサイズを継承します" "(縦横は32の倍数に丸めます)。参照画像なしの場合はサイズ一覧を使います。" ) def reference_dimensions(path): try: with Image.open(path) as image: width, height = image.size # Match ImageOps.exif_transpose in the GPU input path, without decoding pixels here. if image.getexif().get(274) in (5, 6, 7, 8): width, height = height, width except (OSError, ValueError, Image.DecompressionBombError) as error: raise ValueError("参照画像のサイズを読み取れません。画像を選び直してください。") from error return width, height, max(32, round(width / 32) * 32), max(32, round(height / 32) * 32) def reference_size_status(references): if not references: return True, REFERENCE_NOTICE try: width, height, target_width, target_height = reference_dimensions(references[0]) except ValueError as error: return False, str(error) return False, ( f"**画像編集 · 予定出力サイズ:{target_width} × {target_height} px({aspect_ratio(target_width, target_height)})** \n" f"先頭の参照画像:{width} × {height} px({aspect_ratio(width, height)}、EXIF回転を反映)。" "サイズ選択は使用しません。縦横は32の倍数に丸め、約1MPへの縮小はしません。" "大きな参照画像はメモリ不足になる場合があります。" )