Reputation: 17
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?
Upvotes: 0
Views: 759
Reputation: 9365
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
%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
Upvotes: 2
Reputation: 467
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 :)
Upvotes: 0