1

I am trying to flip an image horizontally that is passed as a parameter to my function. I cannot use JES functions. I have the below code. What am I doing wrong?

height = len(image)
width  = len(image[0])

for row in range(height):
    for col in range(width//2):
        srcPixel = image[row][col]
        tgtPixel = image[width - col - 1][row]
        tmpPixel = srcPixel
        srcPixel = tgtPixel
        tgtPixel = tmpPixel
return True
JMushiana
  • 55
  • 7
  • 1
    Does this answer your question? [Flip horizontally an image in Python (JES)](https://stackoverflow.com/questions/17129189/flip-horizontally-an-image-in-python-jes) – Bando Jul 15 '20 at 20:15
  • There are several issues that I find suspicious with this code. First of all, you never write to `image`. Second, you use `row` one time as the first index and another time as the second index, one of them seems to be wrong. Third, the last three lines in the loop don’t have any effect. – mkrieger1 Jul 15 '20 at 20:17

2 Answers2

0
height = len(image)
width  = len(image[0])

for row in range(height):
    for col in range(width//2):
        tmpPixel = image[row][col]
        image[row][col] = image[row][width - col - 1]
        image[row][width - col - 1] = tmpPixel
return True

tmpPixel do not keep the address of the data unlike maybe C.

Tarik
  • 10,810
  • 2
  • 26
  • 40
0

You seem to have a multi-dimensional list so, simply reverse every row.

for row in image:
    row.reverse()

If you want to flip it vertically just reverse the entire image.

image.reverse()

test script

image = [
    [0,  1,  2,  3],
    [4,  5,  6,  7],
    [8,  9,  10, 11],
    [12, 13, 14, 15],
]

#flip vertical
image.reverse()

print(image)
#[[12, 13, 14, 15], 
# [8,  9,  10, 11], 
# [4,  5,  6,  7], 
# [0,  1,  2,  3]]

#flip horizontal from a state of already being flipped vertically
for row in image:
    row.reverse()
    
print(image)
#[[15, 14, 13, 12], 
# [11, 10, 9,  8],
# [7,  6,  5,  4],
# [3,  2,  1,  0]]
OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26
  • I need to flip it horizontally. And I don't believe a 2d Table has a reverse() method or does it? I am not sure. – JMushiana Jul 15 '20 at 21:38
  • @JMushiana ~ both of my examples work. For you to tell me that "you don't think" and "you aren't sure" means you aren't even testing the answers that are given to you. I'm positive it works and is the cleanest answer, unless you are using something that isn't a regular python list. If that's the case you need to fix your question and be more clear. – OneMadGypsy Jul 15 '20 at 21:52
  • I did not mean that. I meant I was not sure if a 2 D table has a reverse() method. Anyway, I tried your answer, and it doesn't work in my case. The solution I write is checked by an auto grader which says it is 66% incorrect if I use your method. I appreciate your answers though Michael, don't get me wrong. I am new at learning python so bear with me. – JMushiana Jul 15 '20 at 21:57
  • @JMushiana ~ my script is 100% perfect IF you are using a regular python list. I have posted a test script to prove it. Also, as a `list` there is no such thing as a `2D` type. It's just `list` inside of `list` and anything you can do to `list` can be done to either. – OneMadGypsy Jul 15 '20 at 22:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217924/discussion-between-michael-guidry-and-jmushiana). – OneMadGypsy Jul 15 '20 at 22:27