I am trying to divide an image into patches and visualize it but matplotlib keep showing totally incorrect output.
from PIL import Image
import os
def imgcrop(input, xPieces, yPieces):
filename, file_extension = os.path.splitext(input)
im = Image.open(input)
imgwidth, imgheight = im.size
height = imgheight // yPieces
width = imgwidth // xPieces
for i in range(0, yPieces):
for j in range(0, xPieces):
box = (j * width, i * height, (j + 1) * width, (i + 1) * height)
a = im.crop(box)
np_img = np.asarray(a)
plt.imshow(np_img)
I used the method as follows:
imgcrop("cats.jpeg", 14, 14)
I got a 16 x 16
patches but in different colours entirely different from the image
code credit: #How to Split Image Into Multiple Pieces in Python
Input:
Output: