简体   繁体   中英

Python - NumPy Array Logical XOR operation byte wise

I am reading an image via Pillow and converting it to a numpy array.

        A = numpy.asarray(Image.open(
            ImageNameA).convert("L"))
        B = numpy.asarray(Image.open(
            ImageNameB).convert("L"))

print A
[[255 255 255 ..., 255 255 255]
 [255 255 255 ..., 255 255 255]
 [255 255 255 ..., 255 255 255]
 ..., 
 [255 255 255 ..., 255 255 255]
 [255 255 255 ..., 255 255 255]
 [255 255 255 ..., 255 255 255]]

Now when I do any logical operation on these 2 numpy arrays, I get it in form of 'True' and 'False'

Answer = numpy.logical_xor(A,B)

print  numpy.logical_xor(A,C)
[[False False False ..., False False False]
 [False False False ..., False False False]
 [False False False ..., False False False]
 ..., 
 [False False False ..., False False False]
 [False False False ..., False False False]
 [False False False ..., False False False]]

My image processing functions cant work with True, False ... How can i get an image in form of 0 , 255 (in bytes)

From the question title, I suppose the function you meant to use is actualy numpy.bitwise_xor it will output arrays in the 0-255 range as you expect.

logical_xor treats all number above 1 as True and 0 as False and always outputs a boolean array (only 0s and 1s).

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM