Отражение изображения по диагонали в Jython

Поэтому мне нужно отразить изображение. Верхняя правая сторона изображения должна быть перевернута на нижнюю левую сторону. Я создал функцию, которая переворачивает верхнюю левую часть изображения в нижнюю правую, но я просто не могу понять, как это сделать по-другому. Вот код:

def mirrorPicture(picture):
 height = getHeight(canvas)
 width = height 

 # to make mirroring easier, let us make it a square with odd number 
 # of rows and columns
 if (height % 2 == 0):
    height =  width = height -1  # let us make the height and width odd


 maxHeight = height - 1
 maxWidth  = width - 1

 for y in range(0, maxWidth):
      for x in range(0, maxHeight - y):     
      sourcePixel = getPixel(canvas, x, y)
      targetPixel = getPixel(canvas, maxWidth - y, maxWidth - x)
      color = getColor(sourcePixel)
      setColor(targetPixel, color)

 return canvas

кстати, я использую IDE под названием "JES".


person user2387191    schedule 14.06.2013    source источник
comment
Вы также не выполняете правильную перестановку в опубликованном коде: вам нужно обменять цвета sourcePixel и targetPixel. Что касается вашего вопроса, подумайте об этом так: как указано, ваша программа копирует (x, y) в (max-y, max-x). Нарисуйте схему, если нужно: какие пиксели нужно скопировать, куда перевернуть по другой диагонали?   -  person Armin Rigo    schedule 14.06.2013


Ответы (1)


Если под «зеркальным отображением» вы имели в виду «перевернуть по диагонали», это должно работать:

def mirrorPicture(picture):
    height = getHeight(picture)
    width = getWidth(picture)

    newPicture = makeEmptyPicture(height, width)

    for x in range(0, width):   
        for y in range(0, height):
            sourcePixel = getPixel(picture, x, y)

            targetPixel = getPixel(newPicture, y, x)
            #                                  ^^^^  (simply invert x and y)
            color = getColor(sourcePixel)
            setColor(targetPixel, color)

    return newPicture

Предоставление:


..................enter image description here..............................enter image description here.................


Related answer about mirroring diagonally here.

person Gauthier Boaglio    schedule 16.06.2013