0

I was curious about image processing with python, so I found this great library imageio, I tried to manipulate the pixels of a picture and save them in a new file, but i had some problems with the loops this is what the code looks like enter image description here and this the error that i Got ! IndexError: index 3507 is out of bounds for axis 0 with size 3507 the code :

 # -*- coding: iso-8859-1 -*-
    import imageio
    import numpy as np
    im = imageio.imread("JAFFRE009a.png")
    taille=im.shape  #taille is a tuple (Width,Height)
    print taille  # (4961,3507)
    matrice_pixels=open("matrice.txt",'w')
    for i in range(taille[1]):
        line=""
        for j in range(taille[0]):
            line+=repr(im[i][j])
        matrice_pixels.write(line+'\n')
    matrice_pixels.close()
  • That is a pretty common error or that people ask about all the time. Can you let us know which stackoverflow questions you read about it and explain why those answers are not helping you solve your problem? – takendarkk Mar 29 '17 at 17:23
  • Alteast try to print the Dimensions of Image and see its Documentation – ATul Singh Mar 29 '17 at 17:29

2 Answers2

1

Because your image doesn't have squarred shape, reshape it before you go through your loop

edyvedy13
  • 2,156
  • 4
  • 17
  • 39
0

EDIT

We can iterate through each row/column position and save to a file as below.It will take very long time depending upon file size.

Instead of writing your own function, you may want to take advantage of inbuilt binary save (which is more efficient) as

np.save('matrix.py', np_array)

You can load this file as np array and manipulate

Or as a text file using np.save [ will take longer ]

np.save('matrix.txt', np_array)

Working Code:

import imageio
import numpy as np
im = imageio.imread("9v9zU.png")
matrice_pixels=open("matric.txt","wb")
nx,ny = im.shape
for i in range(nx):
    line=""
    for j in range(ny):
        line+=repr(im[i][j])
    matrice_pixels.write(line+'\n')

matrice_pixels.close()

#Save as Binary data
np.save('matrix1.npy', im)

#Save as Human readable data
np.savetxt('matrix1.txt', im)

Alternately, you may want to look into off the shelf libraries that will do what you are intending to do. For e.g. This SO link discusses how to remove section of the picture based upon its color using PIL library.

Also , in future, please DO NOT post a picture of your code. Copy/pase to SO window so that we can copy and modify. In this case I had write everything down line by line to test(thankfully code was not that long).

Community
  • 1
  • 1
Anil_M
  • 10,893
  • 6
  • 47
  • 74
  • thank you for your answer, I'm sorry for not pasting the code instead of taking a screenshot, – Driss Ousti Mar 30 '17 at 13:29
  • I tried your solution, but I still got problems (( np.savetxt("matrix.txt",im[:, :, 0]) IndexError: too many indices for array)) – Driss Ousti Mar 30 '17 at 13:29
  • Can you post your `png` file to the link where you have code screenshot so that I can take a look. With my test `png` there are no errors. – Anil_M Mar 30 '17 at 13:37
  • I edited the post, I posted the png file that I'm running my test on ! – Driss Ousti Mar 30 '17 at 13:42
  • what is your end goal? If I save all axis, its saving but with a file size of 450Mb , which is huge. – Anil_M Mar 30 '17 at 13:54
  • Oh that's huge, my end goal is to get rid of the black bands that appears when you scan a document, so i was trying the pixels matrix into a txt file just to get an idea where the text starts and where it ends – Driss Ousti Mar 30 '17 at 14:14
  • ok- fixed your original code. It takes a long time though. You need to assign x and y axis and have large value for outer loop and smaller for inner. np.savetxt will essentially do the same job. – Anil_M Mar 30 '17 at 14:19
  • Also look at other libraries that will allow you to manipulate colors easily. for e.g. `PIL` – Anil_M Mar 30 '17 at 14:25