1

I need to transpose an image in python without using any other import statements or function libraries. The image is being passed as a parameter to my function. The image is a 2-D table of RGB objects. I have the following code but it gives me an index error - list index out of range. What am I doing wrong?

#Find the size of the non-ragged table
numrows = len(image) 
numcols = len(image[0]) # All rows have same no. cols

# Build the table
result = [] # Result accumulator
for m in range(numcols):
    
    # Make a single row
    row = [] # Single row accumulator
    for n in range(numrows):
        row.append(image[n][m])
    
    # Add the result to the table
    result.append(row)
    image.clear()
    image.append(row)
return True
Kevin Omar
  • 127
  • 9
JMushiana
  • 55
  • 7
  • 1
    There's a [trick](https://stackoverflow.com/a/6473724/4799172) which wouldn't require any imports btw – roganjosh Jul 17 '20 at 09:42
  • Can you show an example starting value for `image` that causes the problem? Actually - what exactly do you think that `image.clear()` line is doing? Does it make sense to have that happen in the place where it is happening? Why? – Karl Knechtel Jul 17 '20 at 09:50
  • My approach seems similar to what some have suggested in that post. Why is my method throwing an error – JMushiana Jul 17 '20 at 09:51
  • Trying to make a transposed copy with the pixels from the original image. Then remove all the rows in the image and replace it with the rows from the transposed copy. – JMushiana Jul 17 '20 at 09:53

1 Answers1

0

Each time through the outer loop, you attempt to completely erase image and then append the most recently computed row to it. But this is your source data; the next time through the outer loop, you attempt to build the next row with row.append(image[n][m]) as if nothing had happened.

You are already using result to accumulate the result; don't modify image during the process. Not only does it cause the problem you are seeing, but it is bad interface design: the person using your code (which will probably be you, months from now when you have forgotten what you were thinking) will very definitely not expect the value of image to change at all when computing the transposed version and giving it a separate name.

You have a list named result, and you are apparently doing this inside a function; the natural thing to do would be to return result, and not modify any of the input data. If for some reason you had to modify the input list, then the way to do that is to assign the contents of result back into the image list, a single time, after all the loops: something like image[:] = result. But in this case, there is no good reason to return anything; return True doesn't mean anything useful (if something went wrong, you should not return False or any other value; you should instead raise an Exception - that's what they're for). Since in this case the calling code gets all the information it needs by just using the modified version of image after calling the function, the return value is useless. The standard library also adopts this convention.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • The ask from my function is that it does not return anything. It needs to modify the image. Are you suggesting, I take it out of the outer loop? – JMushiana Jul 17 '20 at 09:57
  • Thank You Thank You Thank You @Karl Knechtel ! That solved the problem! Its working now. I am new to python and returning to coding after 16 years. Thanks once again! Much appreciated. – JMushiana Jul 17 '20 at 10:05