Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Invert Pillow image colours
Hi guys!

I've been trying to invert the pixel colours of an image of Harry Potter for about 2 days now. I have a list of RGB tuples that make up each pixel of the image. I want to convert each of those tuples to a list so I can edit them. I've tried list comprehension and it didn't work. I've tried mapping and it didn't work either. I also tried this:

from PIL import Image
import PIL.ImageOps    

image ='your_image.png')

inverted_image = PIL.ImageOps.invert(image)'new_name.png')
And that didn't work.

This is my code:

from PIL import Image  
import numpy as np

im ='harrypotter.png')

pixels = list(im.getdata())
width, height = im.size
pixels = [pixels[i * width:(i + 1) * width] for i in range(height)]

pixels_list = list(pixels)

for i in range(0, len(pixels_list)):
    for k in range(0, len(pixels_list[i][i])):
        pixels_list[i][i][k] = pixels_list[i][i][k] - (pixels_list[i][i][k] * 2)

array = np.array(pixels_list, dtype = np.uint8)

new_image = Image.fromarray(array)
I would be grateful if anyone could help
This appears to work
#! /usr/bin/env python3

# Do the imports
from PIL import Image
from PIL import ImageOps

image ='/home/johnny/Desktop/start.jpg')

if image.mode == 'RGBA':
    r,g,b,a = image.split()
    rgb_image = Image.merge('RGB', (r,g,b))

    inverted_image = ImageOps.invert(rgb_image)

    r2, g2, b2 = inverted_image.split()

    final_transparent_image = Image.merge('RGBA', (r2, g2, b2, a))'/home/johnny/Desktop/new.png')
    inverted_image = ImageOps.invert(image)'/home/johnny/Desktop/new.png')
Before and After
from PIL import Image
import PIL.ImageOps    

image ='your_image.png')

inverted_image = PIL.ImageOps.invert(image)'new_name.png')
And that didn't work.
What did not work?
If i use image from @menator01 the result will be just the same as he has posted.
from PIL import Image
import PIL.ImageOps

image ='start.jpg')
inverted_image = PIL.ImageOps.invert(image)'start_fin.png')
It didn't work for me. It said it doesn't work for this type of image. And I want it to work with all of them. PNG, JPG, you name it. So, I want to do it my way with the pixel tuples. How do I convert ta list of tuples to a list of lists without list comprehension or mapping?
I had a typo in the code I posted but corrected it. I know you said you want to use numpy but, it will convert both png and jpg


Possibly Related Threads…
Thread Author Replies Views Last Post
  5 variants to invert dictionaries with non-unique values Drakax1 2 363 Aug-31-2020, 11:40 AM
Last Post: snippsat
  How to invert pixel numbers of MNIST data set squillam 1 925 Oct-16-2019, 11:18 AM
Last Post: scidam
  How to create shadow between two colours and control a line after we trace it? CrazyPythonNerd 0 567 Mar-06-2019, 01:31 PM
Last Post: CrazyPythonNerd
  Saving python cmd output into a text file with colours kapilan15 2 1,824 Jan-25-2019, 06:25 PM
Last Post: metulburr
  Trying to invert DICOM image with Python 3.6 newmanf 1 3,358 Feb-28-2018, 08:49 PM
Last Post: Gribouillis
  Closing an image opened by Pillow in Window Photo Viewer bigmit37 16 14,147 Aug-11-2017, 03:54 AM
Last Post: grahamnt

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020