none
How to convert an image to BLACK AND WHITE (not grayscale)

    Question

  • How i can convert an image to black & white.

    I dont want grayscale. I searched on too many forums about black and white all of them are grayscale.

    I just want two colors. BLACK AND WHITE. Not grayscale.

    Friday, July 18, 2014 4:00 PM

All replies

  • I'd guess it depends on the file format for the image e.g. JPEG, TIFF, BMP, PNG etc.

    In the good old days (i.e. under DOS and CPM) you could do this by looping through e.g. a bit map and changing every byte with values above some cut off to 255 and those less than cut off to 0. But I guess that's not possible in your case.

    I assume you want to do this in code rather than using a program such as Paint or Photoshop.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Friday, July 18, 2014 5:22 PM
  • If you're using System.Drawing then you could do something like this:

    var color = new Bitmap("file.jpg");
    var bw = color.Clone(new Rectangle(0, 0, color.Width, color.Height), PixelFormat.Format1bppIndexed);
    

    Friday, July 18, 2014 7:44 PM
    Moderator
  • Do you know how this method decides which pixels should be white and which should be black?

    I was thinking that one could convert the image to grayscale first and then for each pixel with a brightness >127 would be changed to white and <= 127 would be black, or something similar.  I don't know how that would look.

    Friday, July 18, 2014 8:05 PM
  • I'd guess it depends on the file format for the image e.g. JPEG, TIFF, BMP, PNG etc.

    In the good old days (i.e. under DOS and CPM) you could do this by looping through e.g. a bit map and changing every byte with values above some cut off to 255 and those less than cut off to 0. But I guess that's not possible in your case.

    I assume you want to do this in code rather than using a program such as Paint or Photoshop.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    I can use any format. Like if this only works on jpeg i will use this.

    About if i can use another program of course i can. Its not necessary to use it at c# or any coding languange but i just dont know how to do it with programs like photoshop/paint. Any help would be great.

    Friday, July 18, 2014 9:39 PM
  • "but i just dont know how to do it with programs like photoshop/paint. Any help would be great."

    Maybe you should ask that in a photoshop/paint forum?!

    "Do you know how this method decides which pixels should be white and which should be black?"

    I don't know exactly what System.Drawing does but your guess seems reasonable. After all, "grayscale" and "black and white" are pretty much the same thing, "black and white" is simply "grayscale" with only 2 shades of gray.

    Saturday, July 19, 2014 1:46 PM
    Moderator