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()
2
  • 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? Commented Mar 29, 2017 at 17:23
  • Alteast try to print the Dimensions of Image and see its Documentation Commented Mar 29, 2017 at 17:29

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

your elements in taille[0] mathces with the elements in taille[1] but as soon as i + 1 >= len(j) the code stops working.
Also when you print taille you should see it
I already printed taille you can see the reesult just next to the print line
Yes that is the problem, 4961,3501, they are not of equal size
if I change the dimensions of the picture, I'm risking losing a big part of it?
|
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).

8 Comments

thank you for your answer, I'm sorry for not pasting the code instead of taking a screenshot,
I tried your solution, but I still got problems (( np.savetxt("matrix.txt",im[:, :, 0]) IndexError: too many indices for array))
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.
I edited the post, I posted the png file that I'm running my test on !
what is your end goal? If I save all axis, its saving but with a file size of 450Mb , which is huge.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.