在Jython中通过对角线镜像图像 [英] Mirroring an Image by its Diagonal in Jython

查看:189
本文介绍了在Jython中通过对角线镜像图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要镜像一个图像。应将图像的右上侧翻转到左下方。我创建了一个函数,将图像的左上角翻转到右下角,但我似乎无法弄清楚如何以另一种方式进行操作。这是代码:

So I need to mirror an image. The top right side of the image should be flipped over to the bottom left side. I created a function that flips the top left side of an image to the bottom right, but I just can't seem to figure out how to do it the other way. Here's the code:

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

btw,我正在使用名为JES的IDE。

btw, i'm using an IDE called "JES".

推荐答案

如果通过镜像,你的意思是对角翻转,这应该有效:

If by "mirroring", you meant "flip diagonally", this should work :

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

给予:

.................. .............................. .................

.................................................................

关于对角镜像的相关答案这里

Related answer about mirroring diagonally here.

这篇关于在Jython中通过对角线镜像图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆