Python Forum
pillow reversing the order of pixels after every row
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pillow reversing the order of pixels after every row
#1
I need help reversing the order of pixels in an image after every row. If anyone has a solution it would be appreciated.
def main():
    from PIL import Image

    x = 32
    y = 12
    
    im = Image.open('animation1/masktest1.bmp')
    rgb_im = im.convert('RGB')
    im2 = Image.new( 'RGB', (32,12), "black")
    pixels = im2.load()

    for j in range(y):
        for i in range(x):
            if(j % 2 == 0):
                r, g, b = rgb_im.getpixel((i, j))
                pixels[i,j] = (r, g, b)
            else:
                for o in range(0, x, -1):
                    r, g, b = rgb_im.getpixel((i, j))
                    pixels[o, j] = (r, g, b)

    im2.save("animation1/masktest2.bmp")

main()
Reply
#2
Hi, You have a very, very small image (or part of an image).
You would like to reverse the order of the odd numbered rows?
Then
for o in range(0, x, -1):

( x = 32) will not do anything. Try (x,0,-1)

Paul
Reply
#3
The pixels variable has shape 32x32x3, it is a numpy array. You can easily manipulate with numpy arrays without pure-Python loops; If you want to invert all rows of the image, just do:
inverted = pixels[:, ::-1, :]
If you want to invert all odd rows, do
inverted = pixels.copy()
inverted[::2,...] = inverted[::2, ::-1, :]
# now inverted var. has desired form
Reply
#4
(May-26-2020, 06:09 AM)DPaul Wrote: Hi, You have a very, very small image (or part of an image).
You would like to reverse the order of the odd numbered rows?
Then
for o in range(0, x, -1):

( x = 32) will not do anything. Try (x,0,-1)

Paul

Um thanks for the suggestion but when I try this I get the following error:

Error:
Traceback (most recent call last): File "/home/john/Pictures/test.py", line 24, in <module> main() File "/home/john/Pictures/test.py", line 20, in main pixels[o, j] = (r, g, b) IndexError: image index out of range

(May-26-2020, 11:07 AM)scidam Wrote: The pixels variable has shape 32x32x3, it is a numpy array. You can easily manipulate with numpy arrays without pure-Python loops; If you want to invert all rows of the image, just do:
inverted = pixels[:, ::-1, :]
If you want to invert all odd rows, do
inverted = pixels.copy()
inverted[::2,...] = inverted[::2, ::-1, :]
# now inverted var. has desired form

Thanks for the suggestion but I get an error with this code. It seems there is a conflict between numpy and pillow. Numpy has a .copy() but Pillow doesn't

def main():
    from PIL import Image

    x = 32
    y = 12

    im = Image.open('animation1/masktest1.bmp')
    rgb_im = im.convert('RGB')
    im2 = Image.new( 'RGB', (x,y), "black")
    pixels = im2.load()

    for j in range(y):
        for i in range(x):
            r, g, b = rgb_im.getpixel((i, j))
            inverted = pixels.copy()
            inverted[::2,...] = inverted[::2, ::-1, :]
            # now inverted var. has desired form
    im2.save("animation1/masktest2.bmp")    

main()
Error:
Traceback (most recent call last): File "/home/john/Pictures/test2.py", line 19, in <module> main() File "/home/john/Pictures/test2.py", line 13, in main inverted = pixels.copy() AttributeError: 'PixelAccess' object has no attribute 'copy'
Reply
#5
I expected that pixels variable is a numpy array, but, in fact, it is not. Find a way to extract/convert an Image object to numpy array; try np.asarray(pixels); To convert numpy array back to Image object, use Image.fromarray method.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  identify not white pixels in bmp flash77 17 2,456 Nov-10-2023, 09:21 PM
Last Post: flash77
  Convert 400 grayscale pixels into RGB python420 1 2,455 Jan-02-2020, 04:19 PM
Last Post: Clunk_Head
  How to extract temperature value of pixels of a thermal image recorded by IR camera hamacvan 1 14,448 Jan-13-2019, 06:06 PM
Last Post: Larz60+
  Reversing word in strings yields unexpected result Dec 4 3,629 May-17-2017, 05:32 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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