- Client/Scripts/novelai.py +23 -2
Client/Scripts/novelai.py
CHANGED
|
@@ -4,7 +4,7 @@ import os
|
|
| 4 |
import sys
|
| 5 |
|
| 6 |
import numpy as np
|
| 7 |
-
from PIL import Image
|
| 8 |
|
| 9 |
|
| 10 |
def byteize(alpha):
|
|
@@ -54,6 +54,7 @@ def extract_stealth_png(path):
|
|
| 54 |
try:
|
| 55 |
json_data = json.loads(json_bytes.decode("utf-8"))
|
| 56 |
if "Comment" in json_data and isinstance(json_data["Comment"], str):
|
|
|
|
| 57 |
json_data["Comment"] = json.loads(json_data["Comment"])
|
| 58 |
except json.JSONDecodeError:
|
| 59 |
return {"WebUI": json_bytes.decode("utf-8", errors="replace")}
|
|
@@ -68,9 +69,29 @@ def main() -> int:
|
|
| 68 |
|
| 69 |
data = extract_stealth_png(path)
|
| 70 |
print(json.dumps(data, indent=2, ensure_ascii=True, sort_keys=True))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
input()
|
| 72 |
return 0
|
| 73 |
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
raise SystemExit(main())
|
|
|
|
| 4 |
import sys
|
| 5 |
|
| 6 |
import numpy as np
|
| 7 |
+
from PIL import Image, PngImagePlugin
|
| 8 |
|
| 9 |
|
| 10 |
def byteize(alpha):
|
|
|
|
| 54 |
try:
|
| 55 |
json_data = json.loads(json_bytes.decode("utf-8"))
|
| 56 |
if "Comment" in json_data and isinstance(json_data["Comment"], str):
|
| 57 |
+
json_data["_CommentRaw"] = json_data["Comment"]
|
| 58 |
json_data["Comment"] = json.loads(json_data["Comment"])
|
| 59 |
except json.JSONDecodeError:
|
| 60 |
return {"WebUI": json_bytes.decode("utf-8", errors="replace")}
|
|
|
|
| 69 |
|
| 70 |
data = extract_stealth_png(path)
|
| 71 |
print(json.dumps(data, indent=2, ensure_ascii=True, sort_keys=True))
|
| 72 |
+
if isinstance(data, dict) and "Comment" in data:
|
| 73 |
+
comment_json = data.get("_CommentRaw")
|
| 74 |
+
if not isinstance(comment_json, str):
|
| 75 |
+
if isinstance(data["Comment"], str):
|
| 76 |
+
comment_json = data["Comment"]
|
| 77 |
+
else:
|
| 78 |
+
comment_json = json.dumps(
|
| 79 |
+
data["Comment"],
|
| 80 |
+
ensure_ascii=True,
|
| 81 |
+
separators=(",", ":"),
|
| 82 |
+
)
|
| 83 |
+
reply = input("Create 1x1 metadata PNG? [y/N] ").strip().lower()
|
| 84 |
+
if reply in ("y", "yes"):
|
| 85 |
+
base, _ = os.path.splitext(path)
|
| 86 |
+
out_path = f"{base}_meta.png"
|
| 87 |
+
img = Image.new("RGBA", (1, 1), (0, 0, 0, 0))
|
| 88 |
+
meta = PngImagePlugin.PngInfo()
|
| 89 |
+
meta.add_text("Comment", comment_json)
|
| 90 |
+
img.save(out_path, pnginfo=meta)
|
| 91 |
+
print(f"Wrote {out_path}")
|
| 92 |
input()
|
| 93 |
return 0
|
| 94 |
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
+
raise SystemExit(main())
|