[英]How to flip an image horizontally without JES functions in Python
我正在嘗試水平翻轉圖像,該圖像作為參數傳遞給我的 function。 我不能使用 JES 功能。 我有下面的代碼。 我究竟做錯了什么?
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 不像 C 那樣不保留數據的地址。
您似乎有一個多維列表,因此只需反轉每一行。
for row in image:
row.reverse()
如果你想垂直翻轉它,只需反轉整個圖像。
image.reverse()
測試腳本
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.