Skip to content

Commit

Permalink
MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Sep 27, 2021
1 parent 4caeb24 commit 88e805d
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 0 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
# write-image
Write an Image

# install
```bash
npm install write-image
```

# usage
```js
import writeImage from 'write-image';

const result = writeImage({
// data can be structured in 1, 2 or 3 dimensional arrays
data: [
[123, 41, ...], // red band
[36, 32, ...], // green band
[46, 83, ...] // blue band
],

debug: false, // default false, set to true for increased logging

// format of the ouput
// "JPG" or "PNG"
format: "JPG",

// height of the input if you know it
// we'll try to figure it out if not
height: 768,

// width of the input if you know it
// we'll try to figure it out if not
width: 1024,

// quality of the output
// from 0 to 100
// only used for JPG
// default is 85
quality: 85
});
```
result is an object
```js
{
// height of the image
height: 768,

// width of the image
width: 1024,

data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 84 00 05 03 04 04 04 03 05 04 04 04 05 05 05 06 07 0c 08 07 07 07 07 0f 0b 0b 09 ... 697 more bytes>,
}
```
Binary file added data/flower.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/flower.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added jpg-to-png.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
229 changes: 229 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "write-image",
"version": "0.0.0",
"description": "Write an Image",
"main": "write-image.js",
"files": [
"write-image.js"
],
"scripts": {
"format": "npx prettier --arrow-parens=avoid --print-width=120 --trailing-comma=none --write *.js",
"test": "node test.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/DanielJDufour/write-image.git"
},
"keywords": [
"image",
"jpg",
"png"
],
"author": "Daniel J. Dufour",
"license": "CC0-1.0",
"bugs": {
"url": "https://github.com/DanielJDufour/write-image/issues"
},
"homepage": "https://github.com/DanielJDufour/write-image#readme",
"dependencies": {
"jpeg-js": "^0.4.3",
"pngjs": "^6.0.0",
"to-image-data": "^0.0.1"
},
"devDependencies": {
"find-and-read": "^1.0.0",
"flug": "^2.1.0",
"readim": "^0.0.2",
"xdim": "^1.2.1"
}
}
23 changes: 23 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require("fs");
const test = require("flug");
const findAndRead = require("find-and-read");
const toImageData = require("to-image-data");
const xdim = require("xdim");
const readim = require("readim");
const writeImage = require("./write-image");

test("jpg to png", async ({ eq }) => {
const jpg = findAndRead("flower.jpg");
const { height, width, pixels } = await readim({ data: jpg });
const png = await writeImage({ data: pixels, debug: true, format: "png", height, width });
console.log("png:", png);
fs.writeFileSync("jpg-to-png.png", png);
});

test("png to jpg", async ({ eq }) => {
const png = findAndRead("flower.png");
const { height, width, pixels } = await readim({ data: png });
const jpg = await writeImage({ data: pixels, debug: true, format: "jpg", height, width });
console.log("jpg:", jpg);
fs.writeFileSync("png-to-jpg.jpg", jpg);
});
25 changes: 25 additions & 0 deletions write-image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const jpeg = require("jpeg-js");
const { PNG } = require("pngjs");
const toImageData = require("to-image-data");

module.exports = async function writeImage({ data, debug = false, format, height, width, quality = 85 }) {
if (!format) throw new Error(`[write-image] please specify a format, "JPG", or "PNG`);

// normalize format
format = format.replace(/^\./, "").toUpperCase();
if (debug) console.log(`[write-image] format is "${format}"`);

const imageData = toImageData({ data, height, width });
if (debug) console.log(`[write-image] image data is`, imageData);

if (format === "PNG") {
const png = new PNG({ filterType: 4, height, width });
png.data = imageData.data;
if (debug) console.log(`[write-image] png is`, png);
const buffer = PNG.sync.write(png);
return { data: buffer, height, width };
} else if (format === "JPG") {
const { data, height, width } = jpeg.encode(imageData, quality).data;
return { data, height, width };
}
};

0 comments on commit 88e805d

Please sign in to comment.