0

In order to manipulate images later by swapping pixels ("photobooth transformation"), I started by writing a script to create an array of dimension n x n , from a "photo. png" of n x n pixels. But when I want to check if the table is really the same as the picture, it doesn't work Here is the script:

from PIL import Image 
import numpy as np
#the image is 50x50 pixels in gray mode
im =Image.open(r "D:\0-Python0-Image pixelsphoto.png") 
print("size=",im.size," ",'mode=', im.mode," ", "format=",im.format) 
im.show()
# Image → numpy array
T= np.array(im) 
# array formatting ..there are 50 rows like this one
for i in range(0,50):
    print('{:>3d}'.format(T[u][0]),'{:>3d}'.format(T[u][1]),......'{:>3d}'.format(T[u][48]),'{:>3d}'.format(T[u][49]),end=""),print()

Then I copy the table in a text file and save it in the format "photo.pgm" : P2 49 49 255

18 17 16 12 11 10 11 13 ........ . 19 20 17 14 13 13 13 12 .......... etc

The script works well, but when I open the file "photo.pgm", the pixels are mixed up and I can't find the initial photo thank you for your help

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • dont know anything about .pgm but luckily How to write PIL image filter for plain pgm format? https://stackoverflow.com/questions/4270700/how-to-write-pil-image-filter-for-plain-pgm-format. , Image conversion in PIL, pgm file error https://stackoverflow.com/questions/12759013/image-conversion-in-pil-pgm-file-error let us know if still working – pippo1980 Jun 15 '21 at 17:22

1 Answers1

0

thank you pipo1980 for your help but in the meantime I found an explanation to my problem In fact in the 50x50 array the indexes are counted between 0 and 49 while in the photo application (GIMP or Photoshop) pixels are counted between 1 and 50 I made the mistake to define the pgm file with the specifications P2 49 49 255 instead of P2 50 50 256 So the problem is solved