Reputation: 137
I am attempting to flip a image using python without importing any library. Using the following code I am able to mirror the right hand side of the image to the left but the right hand side of the image remains unchanged.
I am stuck on getting the right hand side of the image to update.
height = len(image)
width = len(image[0])
for y in range(height//2):
for x in range(width):
pixo = image[x][y]
pixn = image[x][-y]
pixo.red = pixn.red
pixo.green = pixn.green
pixo.blue = pixn.blue
Upvotes: 0
Views: 1135
Reputation: 1
Use this code instead:
height = len(image)
width = len(image[0])
for i in range(height):
for j in range(width//2):
temp = float(image[i][j])
image[i][j] = image[i][-j]
image[i][-j] = temp
I think the problem was that you used a shallow copy and both parameters changed simultaneously, instead of that using float()
to make a new object creates a deep copy.
Upvotes: 0
Reputation: 2533
Use swapping:
pixo = image[x][y]
pixn = image[x][-y]
pixo.red, pixn.red = pixn.red, pixo.red
pixo.green, pixn.green = pixn.green, pixo.green
pixo.blue pixn.blue = pixn.blue, pixo.blue
Even easier thanks to @Mercury:
image[x][y], image[x][-y] = image[x][-y], image[x][y]
Upvotes: 1