Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jython/Python - Flipping a picture horizontally

I'm trying to "cut" a picture in half and flip both sides horizontally. See link below.

https://i.sstatic.net/mg9Qg.jpg

Original picture:

enter image description here

What the output needs to be:

enter image description here

What I'm getting

enter image description here

This is what I have, but all it does is flip the picture horizontally

def mirrorHorizontal(picture):
  mirrorPoint = getHeight(picture)/2
  height = getHeight(picture)
  for x in range(0, getWidth(picture)):
    for y in range(0, mirrorPoint):
      topPixel = getPixel(picture, x, y)
      bottomPixel = getPixel(picture, x, height - y - 1)
      color = getColor(topPixel)
      setColor(bottomPixel, color)

So how do I flip each side horizontally so that so that it comes out looking like the second picture?

like image 365
John Calzone Avatar asked Jul 02 '26 14:07

John Calzone


1 Answers

One approach would be to define a function for flipping part of an image horizontally:

def mirrorRowsHorizontal(picture, y_start, y_end):
    ''' Flip the rows from y_start to y_end in place. '''
    # WRITE ME!

def mirrorHorizontal(picture):
    h = getHeight(picture)
    mirrorRowsHorizontal(picture, 0, h/2)
    mirrorRowsHorizontal(picture, h/2, h)

Hopefully, that gives you a start.

Hint: You may need to swap two pixels; to do this, you'll want to use a temporary variable.

like image 54
nneonneo Avatar answered Jul 04 '26 03:07

nneonneo