MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / arrayToJimp

Function arrayToJimp

fashion-mnist-vae/data.js:128–155  ·  view source on GitHub ↗

* Convert an image represented as a typed array to a JIMP object. * * @param {Float32Array} imageData * * @returns {Promise[Jimp]} Jimp object representing image.

(imageData)

Source from the content-addressed store, hash-verified

126 * @returns {Promise[Jimp]} Jimp object representing image.
127 */
128async function arrayToJimp(imageData) {
129 const bufferLen = IMAGE_HEIGHT * IMAGE_WIDTH * 4;
130 const buffer = new Uint8Array(bufferLen);
131
132 let index = 0;
133 for (let i = 0; i < IMAGE_HEIGHT; ++i) {
134 for (let j = 0; j < IMAGE_WIDTH; ++j) {
135 const inIndex = (i * IMAGE_WIDTH + j);
136 const val = imageData[inIndex] * 255;
137 buffer.set([Math.floor(val)], index++);
138 buffer.set([Math.floor(val)], index++);
139 buffer.set([Math.floor(val)], index++);
140 buffer.set([255], index++);
141 }
142 }
143
144 return new Promise((resolve, reject) => {
145 new jimp(
146 {data: buffer, width: IMAGE_WIDTH, height: IMAGE_HEIGHT},
147 (err, img) => {
148 if (err) {
149 reject(err);
150 } else {
151 resolve(img);
152 }
153 });
154 });
155}
156
157/**
158 * Preview an image on the console.

Callers 1

previewImageFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected