简体   繁体   中英

Python applying logical_xor function on two gif images returns the error ValueError: image has wrong mode

I have two gif images and I need to do logical_xor on them from PIL library This is my code:

from PIL import Image

image = Image.open("image.gif") key = Image.open("key.gif")

test = image.mode == key.mode

print(test)

def logical_xor(image1, image2): """Logical XOR between two images. .. code-block:: python out = ((bool(image1) != bool(image2)) % MAX) :rtype: :py:class:~PIL.Image.Image """

   image1.load()
   image2.load()
   return image1._new(image1.im.chop_xor(image2.im))

secret = logical_xor(image, key)

I am getting this error:

True Traceback (most recent call last): File "C:/Users/negut_000/OneDrive/Scoala/Crypto/Image Encrypt Decrypt OTP/Encrypt.py", line 24, in <module> secret = logical_xor(image, key) File "C:/Users/negut_000/OneDrive/Scoala/Crypto/Image Encrypt Decrypt OTP/Encrypt.py", line 21, in logical_xor return image1._new(image1.im.chop_xor(image2.im)) ValueError: image has wrong mode Process finished with exit code 1

It seems that the images have the same mode so I don't understand the problem. Please help!

Using instead of

   image = Image.open("image.gif")
   key = Image.open("key.gif")

This code

image = Image.open("image.gif", mode='r').convert("1")
key = Image.open("key.gif", mode='r').convert("1")

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