File size: 8,588 Bytes
8bc48e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python3
"""Reconstruct PrismLayersPro source RGB and full-canvas target RGBA images from Parquet files."""
from __future__ import annotations

import argparse
import concurrent.futures as futures
import gzip
import io
import json
from collections import defaultdict
from pathlib import Path
from typing import Any

import pyarrow.parquet as pq
from PIL import Image
from tqdm import tqdm


def _open_annotations(path: Path):
    if path.suffix == ".gz":
        return gzip.open(path, "rt", encoding="utf-8")
    return path.open("r", encoding="utf-8")


def _decode_parquet_image(value: Any, prism_dir: Path) -> Image.Image:
    if not isinstance(value, dict):
        raise ValueError("Expected a Parquet image struct with 'bytes' or 'path'.")
    if value.get("bytes"):
        source: Any = io.BytesIO(value["bytes"])
    elif value.get("path"):
        source = prism_dir / value["path"]
    else:
        raise ValueError("Missing image bytes/path in Parquet cell.")
    with Image.open(source) as img:
        img.load()
        return img.copy()


def _place_layer_on_canvas(
    layer_img: Image.Image,
    box_xyxy: list[int],
    canvas_size: tuple[int, int],
) -> Image.Image:
    x0, y0, x1, y1 = map(int, box_xyxy)
    width, height = x1 - x0, y1 - y0
    resized = layer_img.convert("RGBA").resize((width, height), Image.Resampling.BICUBIC)
    canvas = Image.new("RGBA", canvas_size, (0, 0, 0, 0))
    # Paste without a mask so straight RGBA alpha is preserved rather than squared.
    canvas.paste(resized, (x0, y0))
    return canvas


def _process_parquet(
    parquet_rel: str,
    row_specs: dict[int, dict[str, Any]],
    prism_dir: Path,
    output_dir: Path,
    *,
    download: bool,
    skip_existing: bool,
) -> tuple[int, int]:
    parquet_path = prism_dir / parquet_rel
    if not parquet_path.is_file():
        if not download:
            raise FileNotFoundError(
                f"Missing {parquet_path}. Download artplus/PrismLayersPro or pass --download."
            )
        from huggingface_hub import hf_hub_download

        hf_hub_download(
            repo_id="artplus/PrismLayersPro",
            repo_type="dataset",
            filename=parquet_rel,
            local_dir=str(prism_dir),
        )

    needed_layers = sorted({layer_idx for spec in row_specs.values() for layer_idx in spec["layers"]})
    columns = ["whole_image"] + [f"layer_{idx:02d}" for idx in needed_layers]

    pf = pq.ParquetFile(parquet_path)
    saved_sources = 0
    saved_layers = 0
    row_offset = 0

    for rg_idx in range(pf.num_row_groups):
        rg_rows = pf.metadata.row_group(rg_idx).num_rows
        rg_end = row_offset + rg_rows
        local_indices = [r for r in row_specs if row_offset <= r < rg_end]
        if not local_indices:
            row_offset = rg_end
            continue

        table = pf.read_row_group(rg_idx, columns=columns)
        for global_row in local_indices:
            spec = row_specs[global_row]
            local_row = global_row - row_offset
            canvas_size = (int(spec["canvas_width"]), int(spec["canvas_height"]))

            src_dst = output_dir / spec["condition_image"]
            if not (skip_existing and src_dst.is_file()):
                src_img = _decode_parquet_image(table["whole_image"][local_row].as_py(), prism_dir).convert("RGB")
                src_dst.parent.mkdir(parents=True, exist_ok=True)
                src_img.save(src_dst)
                saved_sources += 1

            for layer_idx, (target_rel, box_xyxy) in spec["layers"].items():
                tgt_dst = output_dir / target_rel
                if skip_existing and tgt_dst.is_file():
                    continue
                col_name = f"layer_{layer_idx:02d}"
                layer_img = _decode_parquet_image(table[col_name][local_row].as_py(), prism_dir)
                canvas = _place_layer_on_canvas(layer_img, box_xyxy, canvas_size)
                tgt_dst.parent.mkdir(parents=True, exist_ok=True)
                canvas.save(tgt_dst)
                saved_layers += 1

        row_offset = rg_end

    return saved_sources, saved_layers


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--annotations",
        type=Path,
        default=Path("data/train/prismlayerspro_annotations.jsonl"),
        help="Path to prismlayerspro_annotations.jsonl (or .jsonl.gz).",
    )
    parser.add_argument(
        "--prism-dir",
        type=Path,
        required=True,
        help="Local directory containing artplus/PrismLayersPro (with data/*.parquet).",
    )
    parser.add_argument(
        "--output-dir",
        type=Path,
        default=Path("data/train/prismlayerspro"),
        help="Directory to write extracted images and train.jsonl.",
    )
    parser.add_argument(
        "--download",
        action="store_true",
        help="Download missing Parquet files from artplus/PrismLayersPro on Hugging Face.",
    )
    parser.add_argument(
        "--num-workers",
        type=int,
        default=4,
        help="Number of Parquet files to process in parallel.",
    )
    parser.add_argument(
        "--overwrite",
        action="store_true",
        help="Overwrite already extracted PNG files.",
    )
    parser.add_argument(
        "--limit",
        type=int,
        default=None,
        help="Optional limit on the number of annotation records to process.",
    )
    args = parser.parse_args()

    by_parquet: dict[str, dict[int, dict[str, Any]]] = defaultdict(dict)
    manifest_rows: list[dict[str, Any]] = []

    with _open_annotations(args.annotations) as handle:
        for line in handle:
            if not line.strip():
                continue
            row = json.loads(line)
            parquet_rel = row["source_ref"]["parquet"]
            row_index = int(row["source_ref"]["row_index"])
            spec = by_parquet[parquet_rel].setdefault(
                row_index,
                {
                    "condition_image": row["condition_image"],
                    "canvas_width": int(row["canvas_width"]),
                    "canvas_height": int(row["canvas_height"]),
                    "layers": {},
                },
            )
            spec["layers"][int(row["layer_index"])] = (row["target_rgba"], row["box_xyxy"])
            manifest_rows.append(
                {
                    "id": row["id"],
                    "condition_image": row["condition_image"],
                    "target_rgba": row["target_rgba"],
                    "prompt": row["prompt"],
                    "canvas_width": int(row["canvas_width"]),
                    "canvas_height": int(row["canvas_height"]),
                }
            )
            if args.limit is not None and len(manifest_rows) >= args.limit:
                break

    args.output_dir.mkdir(parents=True, exist_ok=True)
    parquet_items = sorted(by_parquet.items())
    total_sources = 0
    total_layers = 0

    if args.num_workers <= 1:
        for parquet_rel, row_specs in tqdm(parquet_items, desc="Extracting PrismLayersPro"):
            s, l = _process_parquet(
                parquet_rel,
                row_specs,
                args.prism_dir,
                args.output_dir,
                download=args.download,
                skip_existing=not args.overwrite,
            )
            total_sources += s
            total_layers += l
    else:
        with futures.ProcessPoolExecutor(max_workers=args.num_workers) as pool:
            futs = [
                pool.submit(
                    _process_parquet,
                    parquet_rel,
                    row_specs,
                    args.prism_dir,
                    args.output_dir,
                    download=args.download,
                    skip_existing=not args.overwrite,
                )
                for parquet_rel, row_specs in parquet_items
            ]
            for fut in tqdm(futures.as_completed(futs), total=len(futs), desc="Extracting PrismLayersPro"):
                s, l = fut.result()
                total_sources += s
                total_layers += l

    manifest_path = args.output_dir / "train.jsonl"
    with manifest_path.open("w", encoding="utf-8") as handle:
        for row in manifest_rows:
            handle.write(json.dumps(row, ensure_ascii=False) + "\n")

    print(
        f"Done: wrote {total_sources} source images, {total_layers} target RGBA layers, "
        f"and {len(manifest_rows)} records to {manifest_path}"
    )


if __name__ == "__main__":
    main()