Reputation: 208
I've been looking through some possible solution for this question, and i have found out that you can use the PIL library, but i wanted to ask if there is a possible solution by using a for loop.
Here's what I've tried:
! git clone https://github.com/carldjapardi/Trial-
import numpy as np
from matplotlib.pyplot import imshow
from PIL import Image
path = "/content/Trial-/chaplin.jpg"
image = Image.open(path).convert('L')
mat_image = np.array(image) #turn to np array
m, n = mat_image.shape #img shape, m is the height, n is the width
mat_image_reversed = mat_image
for i in range(n):
reversed_column = []
for j in range(m-1, -1, -1):
reversed_column.append(mat_image_reversed[i][j])
mat_image_reversed[i] = reversed_column
imshow(mat_image_reversed, cmap = 'gray')
The problem is i keep getting a:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-33-84670325c66b> in <module>()
----> 7 mat_image_reversed[i] = reversed_column
ValueError: cannot copy sequence with size 394 to array axis with dimension 700
i tried replacing it withmat_image_reversed[j] = reversed_column
, but it gave the same error
Can anyone tell me what is wrong?
Upvotes: 0
Views: 408
Reputation: 56
…
for i in range(m):
reversed_column = []
for j in range(n-1, -1, -1):
…
But I guess what you need is:
mat_image_reversed = np.flip(mat_image,0)
or
mat_image_reversed = np.flip(mat_image,1)
accordingly.
Upvotes: 1