fix png compression
This commit is contained in:
@@ -22,12 +22,13 @@ Scrollsmith is a Node.js tool for generating Dungeons & Dragons one-page dungeon
|
||||
|
||||
- Node.js 22+
|
||||
- Ollama server running and accessible
|
||||
- Nextcloud (optional) for PDF uploads
|
||||
- Gitea Releases (optional) for PDF uploads
|
||||
- `.env` file with:
|
||||
|
||||
```env
|
||||
OLLAMA_API_URL=http://localhost:3000/api/chat/completions
|
||||
OLLAMA_API_KEY=your_api_key_here
|
||||
COMFYUI_URL=http://192.168.1.124:8188
|
||||
````
|
||||
|
||||
---
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import fs from 'fs/promises';
|
||||
import UPNG from 'upng-js';
|
||||
|
||||
const countUniqueColors = (data) => {
|
||||
const uniqueColors = new Set();
|
||||
for (let i = 0; i < data.length; i += 4) {
|
||||
uniqueColors.add(`${data[i]},${data[i + 1]},${data[i + 2]},${data[i + 3]}`);
|
||||
if (uniqueColors.size > 256) {
|
||||
return uniqueColors.size;
|
||||
}
|
||||
}
|
||||
return uniqueColors.size;
|
||||
};
|
||||
|
||||
export async function compressPng (filePath) {
|
||||
const buffer = await fs.readFile(filePath);
|
||||
const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
||||
const img = UPNG.decode(arrayBuffer);
|
||||
const rgba = new Uint8Array(img.data);
|
||||
const frameData = new Uint8Array(img.width * img.height * 4);
|
||||
frameData.set(rgba.subarray(0, frameData.length));
|
||||
const cnum = countUniqueColors(rgba) <= 256 ? 256 : 0;
|
||||
const optimizedArrayBuffer = UPNG.encode([frameData.buffer], img.width, img.height, cnum, img.depth);
|
||||
const optimizedBuffer = Buffer.from(optimizedArrayBuffer);
|
||||
await fs.writeFile(filePath, optimizedBuffer);
|
||||
return filePath;
|
||||
};
|
||||
@@ -2,7 +2,6 @@ import path from "path";
|
||||
import { mkdir, writeFile } from "fs/promises";
|
||||
import { fileURLToPath } from "url";
|
||||
import { callOllama } from "./ollamaClient.js";
|
||||
import { compressPng } from "./compressPng.js";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const COMFYUI_URL = process.env.COMFYUI_URL || "http://localhost:8188";
|
||||
@@ -182,9 +181,6 @@ async function generateImageViaComfyUI(prompt, filename) {
|
||||
|
||||
console.log("Downloading image...");
|
||||
const filepath = await downloadImage(comfyFilename, filename);
|
||||
|
||||
console.log("Compressing PNG...");
|
||||
await compressPng(filepath);
|
||||
return filepath;
|
||||
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user