Posts: 5
Threads: 3
Joined: May 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()
Posts: 741
Threads: 122
Joined: Dec 2017
May-26-2020, 06:09 AM
(This post was last modified: May-26-2020, 06:11 AM by DPaul.)
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
Posts: 817
Threads: 1
Joined: Mar 2018
May-26-2020, 11:07 AM
(This post was last modified: May-26-2020, 11:08 AM by scidam.)
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
Posts: 5
Threads: 3
Joined: May 2020
May-26-2020, 10:07 PM
(This post was last modified: May-26-2020, 10:18 PM by johnEmScott.)
(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'
Posts: 817
Threads: 1
Joined: Mar 2018
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.
|