0
votes

I'm looking to do this in Bash, if possible. The bitmap image in question is 64x64, and only contains black and white pixels, nothing in between. I'm trying to write a script in bash that can somehow read the image, and return either a 1 for white, and a 0 for black, for each pixel in the image. So, the output would look something like this:

01001001010001010101010...

Can this be done in Bash? If so, can some example code be given?

2
That can easily be done through Python and Numpy. Will it help if I give you a python script for it? Sorry no experience with bash.Prakhar Verma
Yes, a python script would work great!AfterHours
Is the script ready yet?AfterHours
Please check my answer below.Prakhar Verma

2 Answers

2
votes

You can do it with ImageMagick + netpbm as follows:

convert my.png -monochrome pnm:-|pnmtoplainpnm|tail -n+4|tr -d ' \n'

If you do not have netpbm available on your platform for whatever reason:

convert my.png -monochrome -compress none pnm:-|sed '1,2d;s/255/1/g'|tr -d ' \n'

I use "png" as an input here, but ImageMagick will accept a wide range of input bitmap formats: https://www.imagemagick.org/script/formats.php

Test

my.png

enter image description here

%convert my.png -monochrome -compress none pnm:-|sed '1,2d;s/255/1/g'|tr -d ' \n'\
|fold -16

0000000000000000
0000000000000000
0000000000000000
0001111111111100
0000000010000000
0000000010000000
0000000010000000
0000000010000000
0000000010000000
0000000010000000
0000000010000000
0000000010000000
0000000010000000
0000000010000000
0000000000000000
0000000000000000
0
votes

Try the below script:

import cv2

img = cv2.imread(r'/home/Bitmapfont.png')
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

grey_img[grey_img !=255] = 0
grey_img[grey_img ==255] = 1

print grey_img

Let me know it any change is required :)