Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING CHANGE] Future policy and two functions (extraction and conversion) #3

Merged
merged 10 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.git/
.github/
test/
deno.jsonc
logo/
src/
examples/
Expand Down
91 changes: 43 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,44 @@

This library provides functionality to extract information stored within PNG images generated by Stable Diffusion Web UI.

## Usage
It also provides functions to mutually convert between infotext and JSON formats.

## Usage(extract infotext in PNG images)

You can use `getInfotext` or `getInfotextJson` to extract infotext embedded in PNG images.

Support `CJS/ESM/UMD`

### CommonJS

```javascript
const { getPngInfo } = require('chilled-lemon');
const { getInfotext, getInfotextJson } = require('chilled-lemon');
const { readFile } = require('node:fs/promises');

(async () => {
const buf = await readFile('./test.png');
const jsonOutput = await getPngInfo(buf);
console.log(jsonOutput);
const textOutput = await getPngInfo(buf, { format: 'text' });
console.log(textOutput);

const infotext = await getInfotext(buf);
console.log(infotext);

const json = await getInfotextJson(buf);
console.log(json);
})();
```

### ES Modules

```javascript
import { getPngInfo } from 'chilled-lemon';
import { getInfotext, getInfotextJson } from 'chilled-lemon';
import { readFile } from 'node:fs/promises';

const buf = await readFile('./test.png');
const jsonOutput = await getPngInfo(buf);
console.log(jsonOutput);
const textOutput = await getPngInfo(buf, { format: 'text' });
console.log(textOutput);

const infotext = await getInfotext(buf);
console.log(infotext);

const json = await getInfotextJson(buf);
console.log(json);
```


Expand All @@ -56,7 +64,7 @@ console.log(textOutput);
<pre id="output"></pre>

<script>
const { getPngInfo } = window.ChilledLemon;
const { getInfotext, getInfotextJson } = window.ChilledLemon;

async function getPngInfoFromFile() {
const fileInput = document.getElementById('inputFile');
Expand All @@ -66,11 +74,11 @@ console.log(textOutput);
try {
const outputElement = document.getElementById('output');

const pngInfoObj = await getPngInfo(arrayBuffer);
outputElement.textContent = '===PNG INFO (JSON)===\n' + JSON.stringify(pngInfoObj);
const infotext = await getInfotext(arrayBuffer);
outputElement.textContent += '\n\n===PNG INFO (infotext format)===\n' + infotext;

const textOutput = await getPngInfo(arrayBuffer, { format: 'text' });
outputElement.textContent += '\n\n===PNG INFO===\n' + textOutput;
const json = await getInfotextJson(arrayBuffer);
outputElement.textContent += '\n\n===PNG INFO (JSON format)===\n' + JSON.stringify(json);
} catch (err) {
console.error(`Error reading file: ${err.message}`);
}
Expand All @@ -81,49 +89,36 @@ console.log(textOutput);
</html>
```

## Methods and Types

```typescript
import { getPngInfo, getOriginalKeyNames, PngInfoObject, OriginalKeyPngInfoObject, LoraHash } from 'chilled-lemon';
```

### Methods

### getPngInfo(arrayBuffer: ArrayBuffer, options?: Options): Promise<PngInfoObject | string>
## Usage (Conversion between infotext and JSON)

Extracts the information from a PNG image represented by an ArrayBuffer. The options argument allows you to specify the format in which the extracted data should be returned: 'json' (default) or 'text'.
You can use `convertInfotextToJson` and `convertJsonToInfotext` to convert between infotext and JSON.

#### Parameters
* `arrayBuffer` An ArrayBuffer representation of the PNG image to extract information from.
* `options` (Optional) An object specifying options for the function. Available options are:
* `format` The format in which the function should return the extracted information. This can either be 'json' or 'text'. If not specified, 'json' is used by default.

#### Returns
A promise which resolves to either an object representing the information extracted from the PNG image (PngInfoObject) if the format option is set to 'json', or a string if the format option is set to 'text'.

### getOriginalKeyNames(obj: PngInfoObject): OriginalKeyPngInfoObject

Converts the keys of a PngInfoObject from the library's internal naming scheme back to their original names.

#### Parameters
* `obj` A PngInfoObject whose keys should be converted.
```javascript
import { getInfotext, getInfotextJson, convertInfotextToJson, convertJsonToInfotext } from 'chilled-lemon';
import { readFile } from 'node:fs/promises';

#### Returns
An OriginalKeyPngInfoObject whose keys have been converted back to their original names.
const buf = await readFile('./test.png');

### Types
const infotext = await getInfotext(buf);
const convertedJson = await convertInfotextToJson(infotext);
console.log(convertedJson)

### PngInfoObject
const json = await getInfotextJson(buf);
const convertedInfotext = await convertJsonToInfotext(json);
console.log(convertedInfotext);
```

An object representing the information extracted from a PNG image. The keys of this object correspond to the different pieces of information that can be stored in a PNG image by Stable Diffusion Web UI.
## Development

### OriginalKeyPngInfoObject
### Code formatter

A version of PngInfoObject where the keys have been converted back to their original names.
We are currently utilizing "deno fmt" as our code formatter. In order to use this, developers will need to have Deno installed on their PC.

At the moment, I am the sole developer working on this project, which is why I chose "deno fmt".
it allows for easy code formatting without the necessity to include dependent packages in the project.

### LoraHash
An object representing a set of Lora hashes. This object's keys are the names of the Lora hashes, and its values are the corresponding hash values.
In the future, should we expand our team and if other developers express a preference for a different formatter, I am open to discussions and would appreciate if the suggestions are raised as issues for consideration.


## inspired
Expand Down
13 changes: 13 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"fmt": {
"include": [
"src",
"test",
"deno.jsonc",
"package.json",
"tsconfig.json",
"vite.config.js"
],
"exclude": ["node_modules"]
}
}
77 changes: 39 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
{
"name": "chilled-lemon",
"version": "0.0.4",
"description": "This library is used to read the PNG Info values of images generated by Stable Diffusion web UI.",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
"unpkg": "dist/index.umd.js",
"types": "dist/index.d.ts",
"scripts": {
"build:types": "tsc",
"build:vite": "vite build",
"build": "npm run build:types && npm run build:vite && mv temp/*.d.ts dist/",
"prepublishOnly": "npm run build",
"test": "vitest"
},
"repository": {
"type": "git",
"url": "https://github.com/shinshin86/chilled-lemon.git"
},
"keywords": [
"stable diffusion web ui",
"sd webui",
"stable diffusion",
"pnginfo",
"png",
"info"
],
"author": "shinshin86 <shinshin86npm@gmail.com> (https://github.com/shinshin86)",
"license": "MIT",
"bugs": {
"url": "https://github.com/shinshin86/chilled-lemon/issues"
},
"homepage": "https://github.com/shinshin86/chilled-lemon#readme",
"devDependencies": {
"@types/node": "^20.4.1",
"typescript": "^5.1.6",
"vite": "^4.4.2",
"vitest": "^0.34.4"
}
"name": "chilled-lemon",
"version": "0.0.4",
"description": "This library is used to read the PNG Info values of images generated by Stable Diffusion web UI.",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
"unpkg": "dist/index.umd.js",
"types": "dist/index.d.ts",
"scripts": {
"build:types": "tsc",
"build:vite": "vite build",
"build": "npm run build:types && npm run build:vite && mv temp/*.d.ts dist/",
"prepublishOnly": "npm run build",
"test": "vitest",
"fmt": "deno fmt"
},
"repository": {
"type": "git",
"url": "https://github.com/shinshin86/chilled-lemon.git"
},
"keywords": [
"stable diffusion web ui",
"sd webui",
"stable diffusion",
"pnginfo",
"png",
"info"
],
"author": "shinshin86 <shinshin86npm@gmail.com> (https://github.com/shinshin86)",
"license": "MIT",
"bugs": {
"url": "https://github.com/shinshin86/chilled-lemon/issues"
},
"homepage": "https://github.com/shinshin86/chilled-lemon#readme",
"devDependencies": {
"@types/node": "^20.4.1",
"typescript": "^5.1.6",
"vite": "^4.4.2",
"vitest": "^0.34.4"
}
}
Loading
Loading