kadaj13
kadaj13

Reputation: 43

Patch Extracting In Python

I have an image and I would like to extract patches of it and then save each patch as an image in that folder. Here is my first attempt:

from sklearn.feature_extraction import image
from sklearn.feature_extraction.image import extract_patches_2d
import os, sys
from PIL import Image



imgFile = Image.open('D1.gif')
window_shape = (10, 10)
B = extract_patches_2d(imgFile, window_shape)

print imgFile

But I get the following error:

AttributeError: shape

I have searched through internet and I couldn't find anything. I would be very grateful if anyone can help me on this.

Thanks in advance

Upvotes: 0

Views: 2691

Answers (1)

nempat
nempat

Reputation: 456

As per documentation first parameter for extract_patches_2d is an array or a shape.

You should first create an array from your imgFile so you get the pixels, and then pass that array to the function.

import numpy
import PIL

# Convert Image to array
img = PIL.Image.open("foo.jpg").convert("L")
arr = numpy.array(img)

Upvotes: 1

Related Questions