Please bookmark this page to avoid losing your image tool!

Image Black To White Color Changer

(Free & Supports Bulk Upload)

Drag & drop your images here or

The result will appear here...
You can edit the below JavaScript code to customize the image tool.
/**
 * Changes all black pixels in an image to white.
 * @param {HTMLImageElement} originalImg The original image object to process.
 * @returns {HTMLCanvasElement} A new canvas element with the black colors replaced by white.
 */
function processImage(originalImg) {
  // Create a new canvas element
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');

  // Set the canvas dimensions to match the original image
  // Use naturalWidth/Height to ensure we get the intrinsic size of the image
  canvas.width = originalImg.naturalWidth;
  canvas.height = originalImg.naturalHeight;

  // Draw the original image onto the canvas
  ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);

  // Get the pixel data from the canvas
  // This returns a flat array of RGBA values for each pixel
  const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  const data = imageData.data;

  // Iterate over each pixel in the image data array
  // Each pixel is represented by 4 consecutive values (R, G, B, A)
  for (let i = 0; i < data.length; i += 4) {
    const red = data[i];
    const green = data[i + 1];
    const blue = data[i + 2];

    // Check if the pixel color is black (R, G, and B are all 0)
    if (red === 0 && green === 0 && blue === 0) {
      // If it's black, change it to white (R=255, G=255, B=255)
      data[i] = 255;       // Red channel
      data[i + 1] = 255;   // Green channel
      data[i + 2] = 255;   // Blue channel
      // The alpha channel (data[i + 3]) is left unchanged to preserve transparency
    }
  }

  // Put the modified pixel data back onto the canvas
  ctx.putImageData(imageData, 0, 0);

  // Return the canvas element, which now displays the modified image
  return canvas;
}

Free Image Tool Creator

Can't find the image tool you're looking for?
Create one based on your own needs now!

Description

The Image Black To White Color Changer is a tool that allows users to transform images by converting all black pixels into white. This can be particularly useful for various applications, such as modifying graphic designs, enhancing illustrations, or creating high-contrast images. Users may find it handy in digital art, photography, or when preparing images for printing, where changing black elements to white is desired for stylistic effects or clarity.

Leave a Reply

Your email address will not be published. Required fields are marked *