Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Invert Pillow image colours
#1
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 = Image.open('your_image.png')

inverted_image = PIL.ImageOps.invert(image)

inverted_image.save('new_name.png')
And that didn't work.

This is my code:

from PIL import Image  
import numpy as np

im = Image.open('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)
new_image.show()
I would be grateful if anyone could help
Reply
#2
This appears to work
#! /usr/bin/env python3

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

image = Image.open('/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))

    final_transparent_image.save('/home/johnny/Desktop/new.png')
else:
    inverted_image = ImageOps.invert(image)
    inverted_image.save('/home/johnny/Desktop/new.png')
Before and After
       
Reply
#3
Quote:
from PIL import Image
import PIL.ImageOps    

image = Image.open('your_image.png')

inverted_image = PIL.ImageOps.invert(image)

inverted_image.save('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 = Image.open('start.jpg')
inverted_image = PIL.ImageOps.invert(image)
inverted_image.save('start_fin.png')
Reply
#4
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?
Reply
#5
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

       
Reply
#6
ok.......
Reply


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

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