简体   繁体   English

如何在 Python 中不使用 JES 功能水平翻转图像

[英]How to flip an image horizontally without JES functions in Python

I am trying to flip an image horizontally that is passed as a parameter to my function.我正在尝试水平翻转图像,该图像作为参数传递给我的 function。 I cannot use JES functions.我不能使用 JES 功能。 I have the below code.我有下面的代码。 What am I doing wrong?我究竟做错了什么?

height = len(image)
width  = len(image[0])

for row in range(height):
    for col in range(width//2):
        srcPixel = image[row][col]
        tgtPixel = image[width - col - 1][row]
        tmpPixel = srcPixel
        srcPixel = tgtPixel
        tgtPixel = tmpPixel
return True
height = len(image)
width  = len(image[0])

for row in range(height):
    for col in range(width//2):
        tmpPixel = image[row][col]
        image[row][col] = image[row][width - col - 1]
        image[row][width - col - 1] = tmpPixel
return True

tmpPixel do not keep the address of the data unlike maybe C. tmpPixel 不像 C 那样不保留数据的地址。

You seem to have a multi-dimensional list so, simply reverse every row.您似乎有一个多维列表,因此只需反转每一行。

for row in image:
    row.reverse()

If you want to flip it vertically just reverse the entire image.如果你想垂直翻转它,只需反转整个图像。

image.reverse()

test script测试脚本

image = [
    [0,  1,  2,  3],
    [4,  5,  6,  7],
    [8,  9,  10, 11],
    [12, 13, 14, 15],
]

#flip vertical
image.reverse()

print(image)
#[[12, 13, 14, 15], 
# [8,  9,  10, 11], 
# [4,  5,  6,  7], 
# [0,  1,  2,  3]]

#flip horizontal from a state of already being flipped vertically
for row in image:
    row.reverse()
    
print(image)
#[[15, 14, 13, 12], 
# [11, 10, 9,  8],
# [7,  6,  5,  4],
# [3,  2,  1,  0]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM