From d7f1e499eae314e1042edffc574ce6e9af763376 Mon Sep 17 00:00:00 2001 From: Shengjie Xu Date: Sat, 3 Jan 2026 18:29:53 +0800 Subject: [PATCH] [Core] enable creating patch from base and target image from commandline --- src/preppipe/util/imagepack.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/preppipe/util/imagepack.py b/src/preppipe/util/imagepack.py index c22f928..d4fefa6 100644 --- a/src/preppipe/util/imagepack.py +++ b/src/preppipe/util/imagepack.py @@ -2120,6 +2120,10 @@ def _get_psd_composite_image_from_diff(psd : psd_tools.PSDImage, info : dict) -> target_layers = ImagePack._convert_psd_layername_expr(target_layers_str) base_image = ImagePack._get_psd_composite_image_from_layers(psd, base_layers) target_image = ImagePack._get_psd_composite_image_from_layers(psd, target_layers) + return ImagePack._get_diff_image_patch(base_image, target_image) + + @staticmethod + def _get_diff_image_patch(base_image: PIL.Image.Image, target_image : PIL.Image.Image) -> PIL.Image.Image: if base_image.size != target_image.size: raise PPInternalError("Base and target images have different sizes in diff export") @@ -2133,7 +2137,7 @@ def _get_psd_composite_image_from_diff(psd : psd_tools.PSDImage, info : dict) -> max(base_bbox[2], target_bbox[2]), max(base_bbox[3], target_bbox[3]), ) - all_bbox = (0, 0, psd.width, psd.height) + all_bbox = (0, 0, base_image.width, base_image.height) # 将两张图裁剪至公共区域再计算差分以节省时间 if common_bbox != all_bbox: base_image = base_image.crop(common_bbox) @@ -2144,7 +2148,7 @@ def _get_psd_composite_image_from_diff(psd : psd_tools.PSDImage, info : dict) -> raise PPInternalError("Computed patch image is empty in diff export (something went wrong?)") if common_bbox != all_bbox: # 将差分图扩展回原始大小 - full_diff_image = PIL.Image.new("RGBA", (psd.width, psd.height), (0, 0, 0, 0)) + full_diff_image = PIL.Image.new("RGBA", (all_bbox[2], all_bbox[3]), (0, 0, 0, 0)) full_diff_image.paste(diff_image, (common_bbox[0], common_bbox[1])) diff_image = full_diff_image return diff_image @@ -2295,6 +2299,14 @@ def dump_asset_info_json(self, name : str) -> dict: result["descriptor"] = descriptor.dump_asset_info_json() return result + @staticmethod + def _util_command_create_diff_image(base : str, target : str, output : str) -> int: + base_image = PIL.Image.open(base) + target_image = PIL.Image.open(target) + diff_image = ImagePack._get_diff_image_patch(base_image, target_image) + diff_image.save(output) + return 0 + @staticmethod def tool_main(args : list[str] | None = None): # 创建一个有以下参数的 argument parser: [--debug] [--create | --load | --asset ] [--save ] [--fork [args]] [--export ] @@ -2309,6 +2321,10 @@ def tool_main(args : list[str] | None = None): parser.add_argument("--export", metavar="", help="Export the image pack to a directory") parser.add_argument("--export-overview", metavar="", help="Export a single overview image to the specified path") parser.add_argument("--export-overview-html", metavar="", help="Export an interactive HTML to the specified path; require --asset and --export-overview") + subparsers = parser.add_subparsers(dest="subparser") + util_subparser = subparsers.add_parser("util", help="Util commands that does not use image pack") + util_subparser.add_argument("--create-diff-image", nargs=2, metavar="", help="Create a diff image from two image files") + util_subparser.add_argument("--output", metavar="", help="Output path for util commands") if args is None: args = sys.argv[1:] parsed_args = parser.parse_args(args) @@ -2316,6 +2332,14 @@ def tool_main(args : list[str] | None = None): if parsed_args.debug: ImagePack._debug = True + match parsed_args.subparser: + case "util": + if parsed_args.create_diff_image is not None: + return ImagePack._util_command_create_diff_image(parsed_args.create_diff_image[0], parsed_args.create_diff_image[1], parsed_args.output) + parser.error("Unknown util command") + case _: + pass + num_input_spec = 0 if parsed_args.create is not None: num_input_spec += 1