Skip to content
Related Articles

Related Articles

Tensorflow.js tf.browser.fromPixels() Function

Improve Article
Save Article
  • Last Updated : 21 May, 2021
Improve Article
Save Article

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.

The tf.browser.fromPixels() function is used to creates a Tensor of pixels values of an specified image. 

Syntax:

tf.browser.fromPixels (pixels, numChannels)

Parameters: This function accepts two parameters which are illustrated below:

  • pixels: It is the pixels of the input image from which the Tensor is going to be constructed. The supported image types are all 4-channel.
  • numchannels: It is the number of channels of the output Tensor. It’s default value is 3 and the upper limit is up to 4.

Return Value: This function returns the created Tensor of pixels values of the specified image. 

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating a image from some specified
// pixel values
const image = new ImageData(2, 2);
image.data[0] = 5;
image.data[1] = 10;
image.data[2] = 15;
image.data[3] = 20;
  
// Calling the .fromPixels() function 
// over the above image as its parameter
// without using numChannels value,so
// it prints only 3 pixels value as
// the default value of numchannels 
// parameter is 3
tf.browser.fromPixels(image).print();


Output:

Tensor
   [[[5, 10, 15],
     [0, 0 , 0 ]],

    [[0, 0 , 0 ],
     [0, 0 , 0 ]]]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating a image from some specified
// pixels values
const image = new ImageData(1, 1);
image.data[0] = 5;
image.data[1] = 10;
image.data[2] = 15;
image.data[3] = 20;
  
// Calling the .fromPixels() function 
// over the above image as its parameter
// along with 4 value for numChannels parameter
tf.browser.fromPixels(image, 4).print();


Output:

Tensor
    [ [[5, 10, 15, 20],]]

Reference:https://js.tensorflow.org/api/1.0.0/#browser.fromPixels


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!