B&W Image to 0's and 1's - Page 1
Forum section: electronics / general

author: ricko_uk
author_rank: Super Contributor
timestamp: September 09, 2020, 09:10:21 pm
content: 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 youRiccardo

author: ledtester
author_rank: Super Contributor
timestamp: September 09, 2020, 09:14:46 pm
content: Are the images in a particular file format -- i.e. GIF, JPEG, PNG?

author: ricko_uk
author_rank: Super Contributor
timestamp: September 10, 2020, 03:15:27 am
content: 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.

author: ledtester
author_rank: Super Contributor
timestamp: September 10, 2020, 03:47:49 am
content: 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-cAnd here's an online tool to create monochrome BMPs from another file format:https://image.online-convert.com/convert-to-bmp

author: ricko_uk
author_rank: Super Contributor
timestamp: September 10, 2020, 11:49:14 am
content: Thank you LedtesterI 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

author: oPossum
author_rank: Super Contributor
timestamp: September 10, 2020, 12:34:24 pm
content: So you want to convert an image to a C array with 1 bpp monochrome data?

author: ledtester
author_rank: Super Contributor
timestamp: September 10, 2020, 12:37:26 pm
content: Maybe this will work for you:https://manytools.org/hacker-tools/image-to-byte-array/

author: ricko_uk
author_rank: Super Contributor
timestamp: September 10, 2020, 02:55:41 pm
content: 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

author: oPossum
author_rank: Super Contributor
timestamp: September 10, 2020, 03:18:35 pm
content: 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.

author: RenThraysk
author_rank: Regular Contributor
timestamp: September 10, 2020, 06:03:41 pm
content: 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

author: golden_labels
author_rank: Super Contributor
timestamp: September 10, 2020, 08:08:42 pm
content: I do not know any freeware software. But, if you can relax your licensing requirements a bit, see .GIMP. 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.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: September 10, 2020, 08:19:06 pm
content: 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.Tim

author: hamster_nz
author_rank: Super Contributor
timestamp: September 11, 2020, 08:09:01 am
content: 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.

author: 0db
author_rank: Frequent Contributor
timestamp: September 11, 2020, 10:48:40 am
content: 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.

author: hamster_nz
author_rank: Super Contributor
timestamp: September 11, 2020, 10:51:25 am
content: 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.

author: Mechatrommer
author_rank: Super Contributor
timestamp: September 11, 2020, 10:16:15 pm
content: 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 filewhen 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...)

author: ricko_uk
author_rank: Super Contributor
timestamp: September 15, 2020, 12:15:17 am
content: Thank you all for all the infos, feedback and also the software, much appreciated!!!

author: SilverSolder
author_rank: Super Contributor
timestamp: September 15, 2020, 12:46:10 am
content: Quote from: hamster_nz on September 11, 2020, 10:51:25 amPNG supports it.

author: Fixpoint
author_rank: Regular Contributor
timestamp: September 15, 2020, 07:54:50 am
content: I think GIMP will do that when you save an image as raw data, so you don't need any code or special software.

author: SiliconWizard
author_rank: Super Contributor
timestamp: September 20, 2020, 06:38:53 pm
content: 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.

author: SilverSolder
author_rank: Super Contributor
timestamp: September 21, 2020, 01:30:55 pm
content: ImageMagick may be able to do some magick here too.