EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: ricko_uk on September 09, 2020, 09:10:21 pm

Title: B&W Image to 0's and 1's
Post by: ricko_uk on September 09, 2020, 09:10:21 pm
Hi,
I need to convert small black and white images to arrays of zeros and ones for printing.

What is the best way to do it?

Is there a freeware that does it?

Thank you
Riccardo
Title: Re: B&W Image to 0's and 1's
Post by: ledtester on September 09, 2020, 09:14:46 pm
Are the images in a particular file format -- i.e. GIF, JPEG, PNG?
Title: Re: B&W Image to 0's and 1's
Post by: ricko_uk on September 10, 2020, 03:15:27 am

no, I can convert them into any format is required before supplying them to whichever software convert them into an array of 0's and 1's.
Title: Re: B&W Image to 0's and 1's
Post by: ledtester on September 10, 2020, 03:47:49 am
Here's some C code to convert monochrome BMP files to 0's and 1's:

https://stackoverflow.com/questions/14597043/converting-1-bit-bmp-file-to-array-in-c-c

And here's an online tool to create monochrome BMPs from another file format:

https://image.online-convert.com/convert-to-bmp

Title: Re: B&W Image to 0's and 1's
Post by: ricko_uk on September 10, 2020, 11:49:14 am
Thank you Ledtester :)

I don't have experience in PC software, only embedded and I just need to copy and paste the array generated into my code.

I don't know how to make that code workable on a PC, that is why I am looking for a program that generates the array of 0's and 1's.

Any other solutions?

Thank you :)

Title: Re: B&W Image to 0's and 1's
Post by: oPossum on September 10, 2020, 12:34:24 pm
So you want to convert an image to a C array with 1 bpp monochrome data?
Title: Re: B&W Image to 0's and 1's
Post by: ledtester on September 10, 2020, 12:37:26 pm
Maybe this will work for you:

https://manytools.org/hacker-tools/image-to-byte-array/
Title: Re: B&W Image to 0's and 1's
Post by: ricko_uk on September 10, 2020, 02:55:41 pm
Thank you oPossum and Ledtester! :)

Both great suggestions, the "Bitmap To Hex Converter" standalone would be my preferred choice but when I googled for it I could not find it. Where can I download it from?

Thank you :)
Title: Re: B&W Image to 0's and 1's
Post by: oPossum on September 10, 2020, 03:18:35 pm
It is something I wrote and haven't made public. It is attached if you would like to give it a try. Requires a recent version of Windows 10.

Title: Re: B&W Image to 0's and 1's
Post by: RenThraysk on September 10, 2020, 06:03:41 pm
PBM format? It's pretty old but an ASCII based format using 0 and 1s for b&w images.

https://en.wikipedia.org/wiki/Netpbm#File_formats
Title: Re: B&W Image to 0's and 1's
Post by: golden_labels on September 10, 2020, 08:08:42 pm
I do not know any freeware software. But, if you can relax your licensing requirements a bit, see .GIMP (https://www.gimp.org/). It is both included in official repos of many Linux distros and available for Windows. Offers a rich selection of formats. It may export to C sources, HTML tables made to look like images, netpbm and X pixmaps.

But that seems a bit like an XY problem. This is a programming forum and you ask about a piece of software, not about how to write some code. On top of that most programming languages usable in 2020 for application programming have export to common image formats either in their standard libraries or among some popular 3rd party libraries.
Title: Re: B&W Image to 0's and 1's
Post by: T3sl4co1l on September 10, 2020, 08:19:06 pm
Hah, here's a goofy idea.  Convert it to 24-bit RGB.  Posterize, and adjust brightness and contrast until the red channel is either 48 or 49.  Set green to 44, and blue to 32, over the whole image.  Save as RAW.  Open in text editor. ;D

Tim
Title: Re: B&W Image to 0's and 1's
Post by: hamster_nz on September 11, 2020, 08:09:01 am
Have a google at 'halftone algorithms'.

A lot of them are a grid to threshold weightings, and others where errors are pushed forward into the neighbouring pixels.
Title: Re: B&W Image to 0's and 1's
Post by: 0db on September 11, 2020, 10:48:40 am
The best way, in my opinion, is exploiting builtin image algorithms in Visual Basic or FreePascal on Lazarus or GtK to manipulate images. For instance, getpixelcolor(x,y) does the job.
Title: Re: B&W Image to 0's and 1's
Post by: hamster_nz on September 11, 2020, 10:51:25 am
Here's a little hack I just wrote to convert a 24-bit colour PPM file into a 1-bit monochome image (still 24-bit colour, but either all 0xFFFFFF or 0x000000 pixels.

You can create and view PPM files by exporting or opening files in GIMP. I also used GIMP to convert the output back to a monochrome BMP file. Actually GIMP could do the entire task for you. unless you need to do it in software.

Code is pretty rough, but halftone() should give you the idea. You need to change WIDTH and HEIGHT, and the offset fseek() uses to skip the file header.

I've attached a zoomed in area of eye of the Tom Hanks photo I was using to test it. It is pretty hard to upload 1-bit files, as BMP format isn't supported.
Title: Re: B&W Image to 0's and 1's
Post by: Mechatrommer on September 11, 2020, 10:16:15 pm
well i'm glad you asked! this one never see the light except my light during my young iRC age long time ago... (unzip ascpic.zip to reveal installation package, run to install) if you are lucky it will run in Win7/Win10 (it was compiled in WinXP). few ascii tones selection, for your case is shown in gui.jpg below (BinaryTone F5) if you want inverted binary, you can always choose Custom Tone to pick whatever tone you like... if you want 1:1 conversion, enter Characters/Lines the same as horizontal dimension of the image (if Horizontally is ticked) or same as height (vertical  dimension of image if you tick Vertically), set width scale to 1 to avoid rescaling or stretching.. iirc, its been a long time i maybe forgotten that function, you'll need to experiment. Menu->Load Picture and then Menu->Save Ascii should do what you want. it even has a help file ;) when i was so enthusiastic and when my english was so broken. if it cant run or crashed in newer WIndows, feel free to contact me i can recompile and see what i can do to make it work again in newer Windows. fwiw... (edit: i realized the above installation cant run in Win10 due to outdated compilation. this is the updated version...)
Title: Re: B&W Image to 0's and 1's
Post by: ricko_uk on September 15, 2020, 12:15:17 am
Thank you all for all the infos, feedback and also the software, much appreciated!!! :)

Title: Re: B&W Image to 0's and 1's
Post by: SilverSolder on September 15, 2020, 12:46:10 am
[...] It is pretty hard to upload 1-bit files, as BMP format isn't supported.

PNG supports it.

Title: Re: B&W Image to 0's and 1's
Post by: Fixpoint on September 15, 2020, 07:54:50 am
I think GIMP will do that when you save an image as raw data, so you don't need any code or special software.
Title: Re: B&W Image to 0's and 1's
Post by: SiliconWizard on September 20, 2020, 06:38:53 pm
I use that for embedded GUIs: https://lcd-image-converter.riuson.com/en/about/
pretty flexible, and can deal with much more than just B&W. But it generates arrays in C or C++ code, and not as raw binaries as far as I know.
Title: Re: B&W Image to 0's and 1's
Post by: SilverSolder on September 21, 2020, 01:30:55 pm

ImageMagick may be able to do some magick here too.