Отразить по горизонтали изображение в Python (JES)
Мне нужно сделать функцию, которая будет копировать изображение, но зеркально. Я создал код для зеркального отображения изображения, но он не работает, и я не знаю почему, потому что я проследил код, и он должен отражать изображение. Вот код:
def invert(picture):
width = getWidth(picture)
height = getHeight(picture)
for y in range(0, height):
for x in range(0, width):
sourcePixel = getPixel(picture, x, y)
targetPixel = getPixel(picture, width - x - 1, height - y - 1)
color = getColor(sourcePixel)
setColor(sourcePixel, getColor(targetPixel))
setColor(targetPixel, color)
show(picture)
return picture
def main():
file = pickAFile()
picture = makePicture(file)
newPicture = invert(picture)
show(newPicture)
Может кто-нибудь объяснить мне, что не так? Спасибо.
2 ответа
Решение
Попробуй это:
def flip_vert(picture):
width = getWidth(picture)
height = getHeight(picture)
for y in range(0, height/2):
for x in range(0, width):
sourcePixel = getPixel(picture, x, y)
targetPixel = getPixel(picture, x, height - y - 1)
color = getColor(sourcePixel)
setColor(sourcePixel, getColor(targetPixel))
setColor(targetPixel, color)
return picture
def flip_horiz(picture):
width = getWidth(picture)
height = getHeight(picture)
for y in range(0, height):
for x in range(0, width/2):
sourcePixel = getPixel(picture, x, y)
targetPixel = getPixel(picture, width - x - 1, y)
color = getColor(sourcePixel)
setColor(sourcePixel, getColor(targetPixel))
setColor(targetPixel, color)
return picture
Проблема в том, что вы зацикливаетесь на всем изображении, а не только на половине ширины. Вы дважды отражаете свое изображение и получаете то же самое изображение, что и на выходе.
если вы зеркально отражаете оси Y, код должен быть
for y in range(0, height):
for x in range(0, int(width / 2)):