@enslo/sd-metadata 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -151,7 +151,7 @@ if (result.status === 'success') {
151
151
  > For production use, pin to a specific version instead of `@latest`:
152
152
  >
153
153
  > ```text
154
- > https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@1.0.2/dist/index.js
154
+ > https://cdn.jsdelivr.net/npm/@enslo/sd-metadata@1.2.0/dist/index.js
155
155
  > ```
156
156
 
157
157
  ### Format Conversion
@@ -247,6 +247,66 @@ if (result.ok) {
247
247
  }
248
248
  ```
249
249
 
250
+ ### Writing Metadata in WebUI Format
251
+
252
+ Create and embed custom metadata in SD WebUI (A1111) format:
253
+
254
+ ```typescript
255
+ import { writeAsWebUI } from 'sd-metadata';
256
+
257
+ // Create custom metadata from scratch
258
+ const metadata = {
259
+ software: 'sd-webui',
260
+ prompt: 'masterpiece, best quality, 1girl',
261
+ negativePrompt: 'lowres, bad quality',
262
+ width: 512,
263
+ height: 768,
264
+ sampling: {
265
+ steps: 20,
266
+ sampler: 'Euler a',
267
+ cfg: 7,
268
+ seed: 12345,
269
+ },
270
+ model: { name: 'model.safetensors' },
271
+ };
272
+
273
+ // Write to any image format (PNG, JPEG, WebP)
274
+ const result = writeAsWebUI(imageData, metadata);
275
+ if (result.ok) {
276
+ writeFileSync('output.png', result.value);
277
+ }
278
+ ```
279
+
280
+ > [!TIP]
281
+ > `writeAsWebUI` is particularly useful when:
282
+ >
283
+ > - Creating images programmatically and want to embed generation parameters
284
+ > - Converting metadata from proprietary formats to WebUI-compatible format
285
+ > - Building tools that need to output WebUI-readable metadata
286
+
287
+ ### Formatting Metadata for Display
288
+
289
+ Convert metadata to human-readable text in WebUI format:
290
+
291
+ ```typescript
292
+ import { read, formatAsWebUI } from 'sd-metadata';
293
+
294
+ const result = read(imageData);
295
+ if (result.status === 'success') {
296
+ // Convert to WebUI format text
297
+ const text = formatAsWebUI(result.metadata);
298
+ console.log(text);
299
+
300
+ // Output example:
301
+ // masterpiece, best quality, 1girl
302
+ // Negative prompt: lowres, bad quality
303
+ // Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 12345, Size: 512x768, Model: model.safetensors
304
+ }
305
+ ```
306
+
307
+ > [!NOTE]
308
+ > `formatAsWebUI` provides a tool-agnostic standard format for displaying generation metadata. It works with metadata from any supported tool (NovelAI, ComfyUI, etc.) and formats it consistently.
309
+
250
310
  ## API Reference
251
311
 
252
312
  ### `read(data: Uint8Array): ParseResult`
@@ -283,6 +343,56 @@ Writes metadata to an image file.
283
343
  - `{ ok: false, error: { type: string, message?: string } }` - Failed
284
344
  - `type`: `'unsupportedFormat'`, `'conversionFailed'`, or `'writeFailed'`
285
345
 
346
+ ### `writeAsWebUI(data: Uint8Array, metadata: GenerationMetadata): WriteResult`
347
+
348
+ Writes metadata to an image in SD WebUI (A1111) format.
349
+
350
+ **Parameters:**
351
+
352
+ - `data` - Target image file data (PNG, JPEG, or WebP)
353
+ - `metadata` - Generation metadata to embed
354
+ - Can be from any tool or custom-created
355
+ - Automatically converted to WebUI format
356
+
357
+ **Returns:**
358
+
359
+ - `{ ok: true, value: Uint8Array }` - Successfully written (returns new image data)
360
+ - `{ ok: false, error: { type: string, message?: string } }` - Failed
361
+ - `type`: `'unsupportedFormat'` or `'writeFailed'`
362
+
363
+ **Use cases:**
364
+
365
+ - Creating custom metadata for programmatically generated images
366
+ - Converting metadata from other tools to WebUI-compatible format
367
+ - Building applications that output WebUI-readable metadata
368
+
369
+ ### `formatAsWebUI(metadata: GenerationMetadata): string`
370
+
371
+ Formats metadata as human-readable text in SD WebUI (A1111) format.
372
+
373
+ **Parameters:**
374
+
375
+ - `metadata` - Generation metadata from any tool
376
+
377
+ **Returns:**
378
+
379
+ - Human-readable string in WebUI format (plain text)
380
+
381
+ **Output format:**
382
+
383
+ ```text
384
+ positive prompt
385
+ [character prompts for NovelAI]
386
+ Negative prompt: negative prompt
387
+ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 12345, Size: 512x768, ...
388
+ ```
389
+
390
+ **Use cases:**
391
+
392
+ - Displaying metadata to users in a consistent format
393
+ - Copying generation parameters as text
394
+ - Logging or debugging generation settings
395
+
286
396
  ## Type Reference
287
397
 
288
398
  This section provides an overview of the main types. For complete type definitions, see [Type Documentation](./docs/types.md).
package/dist/index.d.ts CHANGED
@@ -305,7 +305,28 @@ type ParseResult = {
305
305
  };
306
306
 
307
307
  /**
308
- * sd-metadata - Read and write AI-generated image metadata
308
+ * Read API for sd-metadata
309
+ *
310
+ * Handles reading and parsing metadata from images.
311
+ * Automatically detects image format and extracts embedded generation metadata.
312
+ */
313
+
314
+ /**
315
+ * Read and parse metadata from an image
316
+ *
317
+ * Automatically detects the image format (PNG, JPEG, WebP) and parses
318
+ * any embedded generation metadata.
319
+ *
320
+ * @param data - Image file data
321
+ * @returns Parse result containing metadata and raw data
322
+ */
323
+ declare function read(data: Uint8Array): ParseResult;
324
+
325
+ /**
326
+ * Write API for sd-metadata
327
+ *
328
+ * Handles writing metadata to images with automatic format conversion.
329
+ * Supports PNG, JPEG, and WebP formats.
309
330
  */
310
331
 
311
332
  /**
@@ -337,16 +358,6 @@ interface WriteOptions {
337
358
  */
338
359
  force?: boolean;
339
360
  }
340
- /**
341
- * Read and parse metadata from an image
342
- *
343
- * Automatically detects the image format (PNG, JPEG, WebP) and parses
344
- * any embedded generation metadata.
345
- *
346
- * @param data - Image file data
347
- * @returns Parse result containing metadata and raw data
348
- */
349
- declare function read(data: Uint8Array): ParseResult;
350
361
  /**
351
362
  * Write metadata to an image
352
363
  *
@@ -360,4 +371,93 @@ declare function read(data: Uint8Array): ParseResult;
360
371
  */
361
372
  declare function write(data: Uint8Array, metadata: ParseResult, options?: WriteOptions): WriteResult;
362
373
 
363
- export { type CharacterPrompt, type GenerationMetadata, type HiresSettings, type ITXtChunk, type MetadataSegment, type MetadataSegmentSource, type ModelSettings, type ParseResult, type PngTextChunk, type RawMetadata, type SamplingSettings, type TExtChunk, type UpscaleSettings, type WriteOptions, type WriteResult, read, write };
374
+ /**
375
+ * WebUI (A1111) format writer for sd-metadata
376
+ *
377
+ * Converts any GenerationMetadata to SD WebUI (A1111) plain text format
378
+ * and writes it to PNG, JPEG, or WebP images.
379
+ */
380
+
381
+ /**
382
+ * Write metadata to an image in SD WebUI format
383
+ *
384
+ * Converts the provided GenerationMetadata to SD WebUI (A1111) plain text
385
+ * format and embeds it into the image. This allows you to:
386
+ * - Create custom metadata from scratch
387
+ * - Modify existing metadata
388
+ * - Convert metadata from any tool to SD WebUI-compatible format
389
+ *
390
+ * The metadata is stored differently based on image format:
391
+ * - PNG: `parameters` tEXt/iTXt chunk (encoding auto-selected based on content)
392
+ * - JPEG/WebP: Exif UserComment field
393
+ *
394
+ * @param data - Target image file data (PNG, JPEG, or WebP)
395
+ * @param metadata - Generation metadata to embed
396
+ * @returns New image data with embedded metadata, or error
397
+ *
398
+ * @example
399
+ * ```typescript
400
+ * import { writeAsWebUI } from 'sd-metadata';
401
+ *
402
+ * // Create custom metadata
403
+ * const metadata = {
404
+ * software: 'sd-webui',
405
+ * prompt: 'masterpiece, 1girl',
406
+ * negativePrompt: 'lowres, bad quality',
407
+ * width: 512,
408
+ * height: 768,
409
+ * sampling: { steps: 20, sampler: 'Euler a', cfg: 7, seed: 12345 },
410
+ * model: { name: 'model.safetensors' },
411
+ * };
412
+ *
413
+ * // Embed into image
414
+ * const result = writeAsWebUI(imageData, metadata);
415
+ * if (result.ok) {
416
+ * writeFileSync('output.png', result.value);
417
+ * }
418
+ * ```
419
+ */
420
+ declare function writeAsWebUI(data: Uint8Array, metadata: GenerationMetadata): WriteResult;
421
+
422
+ /**
423
+ * A1111-format metadata serialization utilities
424
+ *
425
+ * Converts GenerationMetadata to A1111 (SD WebUI) plain text format.
426
+ */
427
+
428
+ /**
429
+ * Format metadata as SD WebUI (A1111) plain text
430
+ *
431
+ * Converts GenerationMetadata to human-readable text in the SD WebUI format.
432
+ * This provides a standard, tool-agnostic way to display generation metadata
433
+ * without needing to manually read individual properties.
434
+ *
435
+ * The output format follows the A1111/SD WebUI convention:
436
+ * ```
437
+ * positive prompt
438
+ * [character prompts for NovelAI]
439
+ * Negative prompt: negative prompt
440
+ * Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 12345, ...
441
+ * ```
442
+ *
443
+ * @param metadata - Generation metadata from any tool
444
+ * @returns Human-readable text in SD WebUI format
445
+ *
446
+ * @example
447
+ * ```typescript
448
+ * import { read, formatAsWebUI } from 'sd-metadata';
449
+ *
450
+ * const result = read(imageData);
451
+ * if (result.status === 'success') {
452
+ * const text = formatAsWebUI(result.metadata);
453
+ * console.log(text);
454
+ * // Output:
455
+ * // masterpiece, 1girl
456
+ * // Negative prompt: low quality, bad anatomy
457
+ * // Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 12345, Size: 512x768, Model: model.safetensors
458
+ * }
459
+ * ```
460
+ */
461
+ declare function formatAsWebUI(metadata: GenerationMetadata): string;
462
+
463
+ export { type CharacterPrompt, type GenerationMetadata, type HiresSettings, type ITXtChunk, type MetadataSegment, type MetadataSegmentSource, type ModelSettings, type ParseResult, type PngTextChunk, type RawMetadata, type SamplingSettings, type TExtChunk, type UpscaleSettings, type WriteOptions, type WriteResult, formatAsWebUI, read, write, writeAsWebUI };