3

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

https://i.stack.imgur.com/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?

Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85
John Calzone
  • 41
  • 2
  • 5
  • Whatr module are you using for Picture manipulation? (What provides the `getPixel`, `setColor` andother methods? ) – jsbueno Oct 23 '12 at 02:07
  • Almost certainly [*JES*](http://code.google.com/p/mediacomp-jes/). – nneonneo Oct 23 '12 at 02:15
  • The logic is not quite right, but there's one thing that looks wrong from the start: you're modifying a pixel and then modifying its counterpart, which is really itself by the time Python gets there. Suppose you have two pixels, `top` and `bottom`. In one iteration, you'll modify `top`, assigning it `bottom`. Then, when you get to `bottom`, you try to set it to `top`'s color, only now `top` *is* `bottom`. So half your picture will be unmodified. An easy way to avoid this is to create a different image with the same dimensions, and map things from your original to that. – NullUserException Oct 23 '12 at 02:20

2 Answers2

1

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.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • I think this suffers from the same problem I pointed out in the comments. – NullUserException Oct 23 '12 at 02:22
  • 1
    I'm operating under the assumption that the picture needs to be modified in-place (which requires swapping). If OP is free to make new images, then creating a new image and filling it in will be easier. – nneonneo Oct 23 '12 at 02:23
  • I think this would work but i need the function to take only one input, that input being a picture. Oh and I'm not really familiar with swapping pictures. I'm just trying to edit this one to what the output needs to be. – John Calzone Oct 23 '12 at 02:24
  • `mirrorHorizontal` takes only one input. `mirrorRowsHorizontal` is a helper function. – nneonneo Oct 23 '12 at 02:24
  • Never mind, I still can't figure it out. Is there another hint you could provide me with, @nneonneo? I feel dumb that I'm not figuring this out. – John Calzone Oct 23 '12 at 03:19
0

One year later, I think we can give the answer :

def mirrorRowsHorizontal(picture, y_start, y_end):
    width = getWidth(picture)

    for y in range(y_start/2, y_end/2):
        for x in range(0, width):
            sourcePixel = getPixel(picture, x, y_start/2 + y)
            targetPixel = getPixel(picture, x, y_start/2 + y_end - y - 1)
            color = getColor(sourcePixel)
            setColor(sourcePixel, getColor(targetPixel))
            setColor(targetPixel, color)

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

Taken from vertical flip here.

Example with 3 stripes :

mirrorRowsHorizontal(picture, 0, h/3)
mirrorRowsHorizontal(picture, h/3, 2*h/3)
mirrorRowsHorizontal(picture, 2*h/3, h)

Before :

enter image description here

After :

enter image description here

Community
  • 1
  • 1
Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85