Given a jpeg image with all black and white pixels, I want to convert all black pixels to 1's and all white pixels to 0's, and have these 1's and 0's output to a text file. Is there any program that does this?

I've done some Googling but all I'm getting are results relating to the "compiled application" type of "binary image".

Preferably the program can be run on Linux from the terminal.

  • No, probably not. you will have to write your own. doing this with a jpeg would be rather hard, but would be SOOOO trivial with a .bmp. are you married to compressed formats like jpg/png? Either way, a jpegs binary data is not pixel data, so you would have to compose the image, and then read it in a pixelated form. Apr 24, 2014 at 12:31
  • 1
    "Questions seeking product, service, or learning material recommendations are off-topic because they become outdated quickly and attract opinion-based answers. Instead, describe your situation and the specific problem you're trying to solve. Share your research." Apr 24, 2014 at 17:35

1 Answer 1


ImageMagick (Open Source) has a great set of command line utilities for doing just this sort of thing.


convert {filename.jpg} -colors 2 -compress none {filename.pbm}

This will give you a plaintext portable bitmap file that starts

{width} {height}

and then contains the binary bitmap data.

  • The .pbm file format is exactly what I needed, thanks. I was also able to use -threshold 50% to convert directly from color images to 2-color images. Apr 25, 2014 at 0:36
  • Despite being off topic for asking for a program recommendation, the results are near enough to pure programming (getting a raw data format) that I found this question and answer useful. I wonder how much this rule against program recommends can be bent? Nov 8, 2015 at 2:46

Not the answer you're looking for? Browse other questions tagged .