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

Function writeImageTensorToFile

visualize-convnet/utils.js:64–94  ·  view source on GitHub ↗

* Write an image tensor to a image file. * * @param {tf.Tensor} imageTensor The image tensor to write to file. * Assumed to be an int32-type tensor with value in the range 0-255. * @param {string} filePath Destination file path.

(imageTensor, filePath)

Source from the content-addressed store, hash-verified

62 * @param {string} filePath Destination file path.
63 */
64async function writeImageTensorToFile(imageTensor, filePath) {
65 const imageH = imageTensor.shape[1];
66 const imageW = imageTensor.shape[2];
67 const imageData = imageTensor.dataSync();
68
69 const bufferLen = imageH * imageW * 4;
70 const buffer = new Uint8Array(bufferLen);
71 let index = 0;
72 for (let i = 0; i < imageH; ++i) {
73 for (let j = 0; j < imageW; ++j) {
74 const inIndex = 3 * (i * imageW + j);
75 buffer.set([Math.floor(imageData[inIndex])], index++);
76 buffer.set([Math.floor(imageData[inIndex + 1])], index++);
77 buffer.set([Math.floor(imageData[inIndex + 2])], index++);
78 buffer.set([255], index++);
79 }
80 }
81
82 return new Promise((resolve, reject) => {
83 new jimp(
84 {data: new Buffer(buffer), width: imageW, height: imageH},
85 (err, img) => {
86 if (err) {
87 reject(err);
88 } else {
89 img.write(filePath);
90 resolve();
91 }
92 });
93 });
94}
95
96// 64 x 3 RGB colormap.
97// This is used to convert a 1-channel (grayscale) image into a color

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected