سؤال

I am trying to write a python function to mirror the right half of a picture to the left half. So far I have this code but it works in the opposite way (it mirrors from L to R) I know it must be a few simple changes, but I just seem to have a block now. Any help appreciated.

def mirrorVertical(source):
  mirrorPoint = getWidth(source) / 2
  width = getWidth(source)
  for y in range(0,getHeight(source)):
    for x in range(0,mirrorPoint):
      leftPixel = getPixel(source,x,y)
      rightPixel = getPixel(source,width - x - 1,y)
      color = getColor(leftPixel)
      setColor(rightPixel,color)
هل كانت مفيدة؟

المحلول

  color = getColor(rightPixel)
  setColor(leftPixel,color)

نصائح أخرى

It seems like you are iterating from the top left corner to the middle, instead of the right corner to the middle. Might want to try range(getWidth(), mirrorPoint) for x and leave y the same.

Before changing the color of the rightPixel, you should save this color to somewhere in order to set it on the leftPixel.

Something like

color_left = getColor(leftPixel)
color_right = getColor(rightPixel)
setColor(leftPixel, color_right)
setColor(rightPixel, color_left)

should do the trick.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top