Patch Extracting In Python
<p>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:</p>

<pre><code>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
</code></pre>

<p>But I get the following error:</p>

<p>AttributeError: shape</p>

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

<p>Thanks in advance</p>

<p>As per <a href="http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.image.extract_patches_2d.html" rel="nofollow">documentation</a> first parameter for extract_patches_2d is an array or a shape. </p>

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

<pre><code>import numpy
import PIL

# Convert Image to array
img = PIL.Image.open("foo.jpg").convert("L")
arr = numpy.array(img)
</code></pre>

python image patch