Python Forum
pillow reversing the order of pixels after every row - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: pillow reversing the order of pixels after every row (/thread-27100.html)



pillow reversing the order of pixels after every row - johnEmScott - May-26-2020

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()



RE: pillow reversing the order of pixels after every row - DPaul - May-26-2020

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


RE: pillow reversing the order of pixels after every row - scidam - May-26-2020

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



RE: pillow reversing the order of pixels after every row - johnEmScott - May-26-2020

(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'



RE: pillow reversing the order of pixels after every row - scidam - May-27-2020

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.