Saturday, October 25, 2025
HomeLanguagesJavascriptTensorflow.js tf.browser.fromPixelsAsync() Function

Tensorflow.js tf.browser.fromPixelsAsync() Function

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.fromPixelsAsync() function is used to create a Tensor of pixel values of a specified image in an async way.

Syntax:

tf.browser.fromPixelsAsync (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




// Creating a image from some specified
// pixels 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 .fromPixelsAsync() function
// over the above image as its parameter
// without using numChannels value so
// it print only 3 pixels value as
// the default value of numchannels
// parameter is 3
(await tf.browser.fromPixelsAsync(image)).print();


Output:

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

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

Example 2:

Javascript




// 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 .fromPixelsAsync() function
// over the above image as its parameter
// along with 4 value for numChannels parameter
(await tf.browser.fromPixelsAsync(image, 4)).print();


Output:

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

Reference:https://js.tensorflow.org/api/latest/#browser.fromPixelsAsync
 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS