Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的对角镜像_Python_Image_Jython_Mirror_Jes - Fatal编程技术网

Python中的对角镜像

Python中的对角镜像,python,image,jython,mirror,jes,Python,Image,Jython,Mirror,Jes,我正在学习python编程课程,我们正在通过定义镜像点,然后使用嵌套for循环将像素从一侧复制到另一侧来镜像图像。例如,垂直镜像图像将使用以下代码: def mirrorVertical(source): mirrorPoint = getWidth(source) / 2 width = getWidth(source) for y in range(0,getHeight(source)): for x in range(0,mirrorPoint): leftPixe

我正在学习python编程课程,我们正在通过定义镜像点,然后使用嵌套for循环将像素从一侧复制到另一侧来镜像图像。例如,垂直镜像图像将使用以下代码:

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)
我目前正在做一个作业问题,要求我们在对角线方向镜像一个图像,这样左上方的图像就会反射到右下方。到目前为止,我发现的每个示例和答案都只适用于方形图像,我需要能够将其应用于任何图像,最好是通过定义对角镜像点。我一直在尝试使用y=mx+b样式的公式定义镜像点,但我不知道如何告诉Python将其作为一条线。任何不特定于方形图像的帮助都将不胜感激


注意:因为我是新来的,所以我还不能发布图片,但是对角线镜像点从左下角到右上角。左上角三角形中的图像将反射到右下角。

我假设您希望根据45度线而不是矩形对角线进行镜像

您必须创建一个新图像,它的宽度是原始图像的高度,它的高度是原始图像的宽度


如果坐标系的原点位于左下角,请将原始坐标系中的点(x,y)复制到新图像中的(y,x)位置。如果是另一个角落,你必须多想一想;)

可以将非方形数组的左上角与右下角互换,如下所示:

height = getHeight(source)
width = getWidth(source)
for i in range(height - 1):
    for j in range(int(width * float(height - i) / height)):
        # Swap pixel i,j with j,i
这适用于沿对角线进行镜像。您似乎暗示您可能希望沿某个任意位置镜像。在这种情况下,您需要决定如何填充镜像线另一侧没有匹配像素的像素

您提到您正在处理分配,因此可能需要显式执行循环,但请注意,如果您将数据放入numpy数组中,您可以通过numpy函数组合
fliplr
flipud
转置
轻松(更高效)实现所需


还要注意,在您的代码示例(左/右镜像)中,您正在将左像素复制到右侧,但反之亦然,因此您实际上没有镜像图像。

仅共享一种对角镜像方式,从左下到右上

如果学生需要从“右上到左下”镜像或使用相反的对角线,他负责适应

注意:这表现为我们沿着通过
mirrorPt
点的垂直轴,为每行像素独立操作一个垂直镜像


输出(安东尼·塔皮斯的绘画作品)







以下是实验性的,只是为了好玩

#绘制点,检查点是否在图像区域中
def提取点(图、柱、x、y):
如果(x>=0)和(x=0)和(y-dy):
err=err-dy
x0=x0+sx
如果(x0==x1)和(y0==y1):
支点(pic、col、x0、y0)
打破
如果(e2x1
#*要使顶部反射到底部,请使用x0=w)或\
(startPt[1]<0)或(startPt[1]>=h)或\
(endPt[0]<0)或(endPt[0]>=w)或\
(endPt[1]<0)或(endPt[1]>=h):
printNow(“错误:输入点必须在图像范围内!”)
一无所获
新的w=abs(起始点[0]-结束点[0])
new_h=abs(startPt[1]-endPt[1])
如果(新的w!=新的h):
printNow(“错误:输入点不是正方形!”)
一无所获
printNow(“给定:(“+str(startPt[0])+”、“+str(endPt[0])+”)和(”\
+str(startPt[1])+“,“+str(endPt[1])+”)
newPicture=makeEmptyPicture(新建,新建)
如果(startPt[0]# !! Works only with squared pictures... !!
def diagMirrorPicture(picture):
    height = getHeight(picture)
    width = getWidth(picture)

    if (height != width):
        printNow("Error: The input image is not squared. Found [" + \
                                     str(width) + " x " + str(height) + "] !")
        return None

    newPicture = makeEmptyPicture(width, height)

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

            # Copy bottom-left as is
            targetPixel = getPixel(newPicture, x, y)
            setColor(targetPixel, color)

            # Mirror bottom-left to top right
            targetPixel = getPixel(newPicture, y, x)
            #                                  ^^^^  (simply invert x and y)
            setColor(targetPixel, color)

        # Here we shift the mirror point
        mirrorPt += 1

    return newPicture


file = pickAFile()
picture = makePicture(file)

picture = diagMirrorPicture(picture)

if (picture):
    writePictureTo(picture, "/home/mirror-diag2.png")
    show(picture)
# Draw point, with check if the point is in the image area
def drawPoint(pic, col, x, y):
   if (x >= 0) and (x < getWidth(pic)) and (y >= 0) and (y < getHeight(pic)):
     px = getPixel(pic, x, y)
     setColor(px, col)

# Draw line segment given two points
# From Bresenham's line algorithm :
# http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
def drawLine(pic, col, x0, y0, x1, y1):

   dx = abs(x1-x0)
   dy = abs(y1-y0) 
   sx = sy = 0

   #sx = 1 if x0 < x1 else -1
   #sy = 1 if y0 < y1 else -1

   if (x0 < x1): 
     sx = 1 
   else: 
     sx = -1
   if (y0 < y1):
     sy = 1 
   else: 
     sy = -1

   err = dx - dy

   while (True):

     drawPoint(pic, col, x0, y0)

     if (x0 == x1) and (y0 == y1): 
       break

     e2 = 2 * err
     if (e2 > -dy):
       err = err - dy
       x0 = x0 + sx

     if (x0 == x1) and (y0 == y1):
       drawPoint(pic, col, x0, y0)
       break

     if (e2 <  dx):
       err = err + dx
       y0 = y0 + sy 


# Works only with squared cropped areas :
# i.e. in [(x0, y0), (x1, y1)], abs(x1-x0) must be equal to abs(y1-y0)
#
# USAGE :
#    * To get bottom reflected to top use x0 > x1 
#    * To get top reflected to bottom use x0 < x1 

def diagCropAndMirrorPicture(pic, startPt, endPt):
    w = getWidth(pic)
    h = getHeight(pic)

    if (startPt[0] < 0) or (startPt[0] >= w) or \
       (startPt[1] < 0) or (startPt[1] >= h) or \
       (endPt[0] < 0) or (endPt[0] >= w) or \
       (endPt[1] < 0) or (endPt[1] >= h):
          printNow("Error: The input points must be in the image range !")
          return None

    new_w = abs(startPt[0] - endPt[0])
    new_h = abs(startPt[1] - endPt[1])

    if (new_w != new_h):
          printNow("Error: The input points do not form a square !")
          return None

    printNow("Given: (" + str(startPt[0]) + ", " + str(endPt[0]) + ") and (" \
                        + str(startPt[1]) + ", " + str(endPt[1]) + ")")

    newPicture = makeEmptyPicture(new_w, new_h)

    if (startPt[0] < endPt[0]):
        offsetX = startPt[0]
        switchX = False
        switchTB = True
    else:
        offsetX = endPt[0]
        switchX = True
        switchTB = False

    if (startPt[1] < endPt[1]):
        offsetY = startPt[1]
        switchY = False
    else:
        offsetY = endPt[1]
        switchY = True

    # (switchX  XOR  switchY)
    changeDiag = (switchX != switchY)

    mirror_pt = 0
    for x in range(0, new_w, 1):

        for y in range(mirror_pt, new_h, 1):
        #for y in range(0, new_h, 1):

            oldX = x
            oldY = y


            if (switchTB):
                sourcePixel = getPixel(picture, offsetX+new_w-1- oldX, offsetY+new_h-1- oldY)
            else:
                sourcePixel = getPixel(picture, offsetX+oldX, offsetY+oldY)
            color = getColor(sourcePixel)

            if (changeDiag):
                newX = new_w-1 - x
                newY = new_h-1 - y
                #printNow("Change Diag !")
            else:
                newX = x
                newY = y

            # Copied half
            if (switchTB):
                targetPixel = getPixel(newPicture, new_w-1- x, new_h-1- y)
            else:
                targetPixel = getPixel(newPicture, x, y)
            setColor(targetPixel, color)

            # Mirror half (simply invert x and y)
            if (switchTB):
                targetPixel = getPixel(newPicture, new_h-1- newY, new_w-1- newX)
            else:
                targetPixel = getPixel(newPicture, newY, newX)
            setColor(targetPixel, color)


        # Here we shift the mirror point
        if (not changeDiag):
            mirror_pt += 1


    return newPicture


file = pickAFile()
pic = makePicture(file)
picture = makePicture(file)

# Draw working area
drawLine(pic, white, 30, 60, 150, 180)
drawLine(pic, white, 30, 180, 150, 60)
drawLine(pic, black, 30, 60, 30, 180)
drawLine(pic, black, 30, 60, 150, 60)
drawLine(pic, black, 150, 60, 150, 180)
drawLine(pic, black, 30, 180, 150, 180)
show(pic)
writePictureTo(pic, "D:\\pic.png")

# Build cropped and mirrored areas
pic1 = diagCropAndMirrorPicture(picture, (150, 60), (30, 180))
pic2 = diagCropAndMirrorPicture(picture, (30, 180), (150, 60))
pic3 = diagCropAndMirrorPicture(picture, (150, 180), (30, 60))
pic4 = diagCropAndMirrorPicture(picture, (30, 60), (150, 180))

# Show cropped and mirrored areas
if (pic1):
    writePictureTo(pic1, "D:\\pic1.png")
    show(pic1)

if (pic2):
    writePictureTo(pic2, "D:\\pic2.png")
    show(pic2)

if (pic3):
    writePictureTo(pic3, "D:\\pic3.png")
    show(pic3)

if (pic4):
    writePictureTo(pic4, "D:\\pic4.png")
    show(pic4)
## WORKS WITH RECTANGLES! 
def mirrorDiagonal(sourceImage):
    copyImage = sourceImage
    width = copyImage.width
    height = copyImage.height
    for y in range(0,height):
        ##The for loop for X is bounded by a slope of width to height.
        mirrorPoint = int((width/height)*y)
        for x in range(0, mirrorPoint):
            leftPixel = copyImage.getpixel((x,y))
            percentageX = float(x / width)
            percentageY = float(y / height)
            rightPixel = sourceImage.getpixel((width*percentageY, height*percentageX))
            copyImage.putpixel((x,y),rightPixel)
    return copyImage