-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4caeb24
commit 88e805d
Showing
8 changed files
with
367 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
}; |