Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8239473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:09:39+00:00 2026-06-07T20:09:39+00:00

If I load an image, how can I loop through all its pixels and

  • 0

If I load an image, how can I loop through all its pixels and turn the white ones (or which ever color I specify) to be turned transparent?

I have an idea on how to do this, but the looping process should be like a 2d array, so it would involve two for loops.

I was thinking I would start on the top row first pixel, iterating to the right, if its a white pixel, then i turn it transparent, and move 1 pixel to the right, if its not white, then I stop. Then in the same row, I start from the left most pixel, and check, if white, I turn it transparent, then move 1 pixel to the left, etc, etc…

Then I move 1 row down and repeat the whole process..

This way I don’t remove any white pixels in the actual image.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:09:40+00:00Added an answer on June 7, 2026 at 8:09 pm

    Its pretty simple to do using getImageData and putImageData just note you can take a pretty significant hit on performance the larger the image gets. You just need to determine if the current pixel is white, then change its alpha to 0.

    Live Demo

    var canvas = document.getElementById("canvas"),
        ctx = canvas.getContext("2d"),
        image = document.getElementById("testImage");
    
    canvas.height = canvas.width = 135;
    ctx.drawImage(image,0,0);
    
    var imgd = ctx.getImageData(0, 0, 135, 135),
        pix = imgd.data,
        newColor = {r:0,g:0,b:0, a:0};
    
    for (var i = 0, n = pix.length; i <n; i += 4) {
        var r = pix[i],
                g = pix[i+1],
                b = pix[i+2];
    
            // If its white then change it
            if(r == 255 && g == 255 && b == 255){ 
                // Change the white to whatever.
                pix[i] = newColor.r;
                pix[i+1] = newColor.g;
                pix[i+2] = newColor.b;
                pix[i+3] = newColor.a;
            }
    }
    
    ctx.putImageData(imgd, 0, 0);​
    

    It was also asked if you could make the values sort of fuzzy to check. Its really easy, just check if its in a certain range. The following will turn off white to pure white transparent.

     // If its white or close then change it
        if(r >=230 && g >= 230 && b >= 230){ 
            // Change the white to whatever.
            pix[i] = newColor.r;
            pix[i+1] = newColor.g;
            pix[i+2] = newColor.b;
            pix[i+3] = newColor.a;
        }
    

    More resources

    • Pixel manipulation with canvas
    • Further info on pixel manipulation with canvas
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I need a function to test if an image can load or not.
I am new to phonegap. How can we load an image on html5 using
I'm playing around with the SVG tutorial here , and can't load an image.
I can't seem to figure out how to load a pictureBox image from a
I don't know what's make wrong, my map can load, but the image on
I'm trying to load image from a plist to a custom cell. It all
I need to find/create a library that can load hdr images in many formats
Hi i want to load png images and jpeg images. can anyone help me?
I Want to load Image size 2550X3300 (i.e 1.7 Mb size), i have loaded
I found difficulty to load image from url in every 30 seconds in android

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.