Skip to content

Commit 50d0f0e

Browse files
committed
fix #111: embed generation metadata in picture files.
Problems: 1. The pip package python-xmp-toolkit isn't supported on Windows, and doesn't automatically install the required C++-based dependency libexempi3. See https://python-xmp-toolkit.readthedocs.io/en/latest/installation.html for manual installation steps. 2. The metadata is just a dump of argparse's parsed options, which could leak private information in arguments such as outdir. A todo is to build a dictionary from the argparse namespace, but exclude fields that could contain sensitive information. this might work
1 parent c56b493 commit 50d0f0e

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

Diff for: environment.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies:
2424
- transformers==4.19.2
2525
- torchmetrics==0.6.0
2626
- kornia==0.6
27+
- python-xmp-toolkit==2.0.1
2728
- -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers
2829
- -e git+https://github.com/openai/CLIP.git@main#egg=clip
2930
- -e .

Diff for: optimizedSD/optimized_txt2img.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from ldm.util import instantiate_from_config
1616
from optimUtils import split_weighted_subprompts, logger
1717
from transformers import logging
18+
from libxmp import XMPFiles, consts, XMPMeta # apt install exempi; pip install python-xmp-toolkit
19+
1820
# from samplers import CompVisDenoiser
1921
logging.set_verbosity_error()
2022

@@ -33,6 +35,22 @@ def load_model_from_config(ckpt, verbose=False):
3335
return sd
3436

3537

38+
def add_metadata(filename, opt):
39+
if opt.skip_metadata:
40+
return
41+
42+
metadata = str(opt)
43+
44+
xmpfile = XMPFiles(file_path=filename, open_forupdate=True)
45+
xmp = xmpfile.get_xmp()
46+
if xmp is None:
47+
xmp = XMPMeta()
48+
xmp.append_array_item(consts.XMP_NS_DC, 'description', metadata,
49+
{'prop_array_is_ordered':True, 'prop_value_is_array':True})
50+
xmpfile.put_xmp(xmp)
51+
xmpfile.close_file()
52+
53+
3654
config = "optimizedSD/v1-inference.yaml"
3755
ckpt = "models/ldm/stable-diffusion-v1/model.ckpt"
3856

@@ -167,6 +185,11 @@ def load_model_from_config(ckpt, verbose=False):
167185
choices=["ddim", "plms"],
168186
default="plms",
169187
)
188+
parser.add_argument(
189+
"--skip_metadata",
190+
action='store_true',
191+
help="do not add generation metadata to image file.",
192+
)
170193
opt = parser.parse_args()
171194

172195
tic = time.time()
@@ -309,9 +332,9 @@ def load_model_from_config(ckpt, verbose=False):
309332
x_samples_ddim = modelFS.decode_first_stage(samples_ddim[i].unsqueeze(0))
310333
x_sample = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
311334
x_sample = 255.0 * rearrange(x_sample[0].cpu().numpy(), "c h w -> h w c")
312-
Image.fromarray(x_sample.astype(np.uint8)).save(
313-
os.path.join(sample_path, "seed_" + str(opt.seed) + "_" + f"{base_count:05}.{opt.format}")
314-
)
335+
filename = os.path.join(sample_path, "seed_" + str(opt.seed) + "_" + f"{base_count:05}.{opt.format}")
336+
Image.fromarray(x_sample.astype(np.uint8)).save(filename)
337+
add_metadata(filename, opt)
315338
seeds += str(opt.seed) + ","
316339
opt.seed += 1
317340
base_count += 1

Diff for: scripts/txt2img.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,7 @@ def main():
251251
if not opt.skip_save:
252252
for x_sample in x_samples_ddim:
253253
x_sample = 255. * rearrange(x_sample.cpu().numpy(), 'c h w -> h w c')
254-
Image.fromarray(x_sample.astype(np.uint8)).save(
255-
os.path.join(sample_path, f"{base_count:05}.png"))
254+
Image.fromarray(x_sample.astype(np.uint8)).save(os.path.join(sample_path, f"{base_count:05}.png"))
256255
base_count += 1
257256

258257
if not opt.skip_grid:

0 commit comments

Comments
 (0)