Im using scipy to read a image and extract the RGB array like this
img_b = misc.imread('google.png')
img_b_blue = img_b[:, :, 0]
img_b_blue = img_b_blue.ravel()
Now I need to convert the img_b_blue array to binary and get the most significant bit.
I can convert using map:
img_b_blue_bin = map(bin, img_b_blue)
But it comes as string in the format '0b11110001'. Theres a way to using map and convert to binary without the 'b'? And how can I get the most significant bit?
You can get the most significant bit by right-shifting 7 bits. The result is an integer array.
img_b_blue_bin = img_b_blue >> 7
Alternatively, probably clearer in your use case, is compare with 128. Higher = white, lower = black. The result is a boolean array.
img_b_blue_bin = img_b_blue >= 128
Similarly, the (n+1)-th least significant bit can be found using (x >> n) & 1
, eg the 2nd most significant bit:
img_b_blue_2nd_msb = (img_b_blue >> 6) & 1
the least significant bit:
img_b_blue_lsb = img_b_blue & 1
See How am I getting a single bit from an int? for how this works for a single integers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.