0

I'm doing some prototype code for a project. I'm using Pillow to open the image and other minor things, but I want to invert the image manually using its pixel values. I used two's complement in hopes of inverting it. However, when I display the final image, it's a solid grey square instead of inverted colors. I just used a picture of a possum, 275 pixels by 183 pixels. Any idea why it displays grey block and not an inverted image?

#importing Image module

from PIL import Image
import numpy
#sets numpy to print out full array
numpy.set_printoptions(threshold=numpy.inf)


def twos_comp(val, bits):
    """compute the 2's complement of int value val"""
    if (val & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255
    val = val - (1 << bits)        # compute negative value
return val


im = Image.open('possum.jpg')
#im.show()

data = numpy.asarray(im)
#print(data)
#print("FINISHED PRINTING")

#print('NOW PRINTING BINARY')
data_binary = numpy.unpackbits(data)
data_binary.ravel()

#print(data_binary)

#print('FINISHED PRINTING')

#getting string of binary array
binaryString = numpy.array2string(data_binary)
binaryString = ''.join(binaryString.split())
binaryString = binaryString[:-1]
binaryString = binaryString[1:]

#print("Binary String: " + binaryString)

out = twos_comp(int(binaryString,2), len(binaryString))

#print('Now printing twos:')
#print(out)


#formatting non-binary two's comp as binary
outBinary = "{0:b}".format(out)
#print('Now printing binary twos: ' + outBinary)


outBinary = outBinary.encode('utf-8')

a_pil_image = Image.frombytes('RGB', (275, 183), outBinary)

a_pil_image.show()

1 Answers1

0

You can invert quite simply with PIL/Pillow:

from PIL import Image, ImageChops

# Load image from disk and ensure RGB
im = Image.open('lena.png').convert('RGB')

# Invert image and save to disk
res = ImageChops.invert(im)
res.save('result.png')

Turns Lena into negated Lena:

enter image description here


Or, if you want to be more mathematical about it:

from PIL import Image                                                                       
import numpy as np                                                                          

im = Image.open('lena.png').convert('RGB')                                                    

# Make Numpy array
imnp = np.array(im)                                                                         
# Invert
imnp = 255 - imnp                                                                           
# Save
Image.fromarray(imnp).save('result.png') 

If you imagine a black image is represented by (0,0,0) and a white image by (255,255,255), it is hopefully not hard to see that inversion of colours is achieved by subtracting from 255 rather than using two's complement.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I can't do this. As I said in the post, I want to convert them manually using the image's pixels themselves and modifying them on my own, not use the library to do all the work. – Braydon Rekart Nov 21 '18 at 21:30
  • Sorry, I didn't see in your question where you said you couldn't use a library. Just use the second piece of code I supplied - that doesn't use a library and it mathematically subtracts each pixel itself from 255. – Mark Setchell Nov 21 '18 at 22:08
  • I could probably have been clearer in my question. Thank you. – Braydon Rekart Nov 22 '18 at 05:27