<commit_before># Python OCR

import sys
import Image
import itertools
import numpy as np

if sys.version_info.major < 3:
    range = xrange 

# test data

testfile = "data/hk.png"

# code

im_raw = Image.open(testfile)

def image_to_array(im):
    """
    Converts a PIL Image object into a numpy array representing
    a greyscale image.
    """
    im_greyscale = im_raw.convert("L")
    width, height = im_greyscale.size
    im_array = np.ndarray(width * height)
    for i, p in enumerate(im_greyscale.getdata()):
        im_array[i] = p
    im_array.shape = (width,height)
    return im_array

im_array = image_to_array(im_raw)

# sliding windows

def windows(im_array, rectangles):
    """
    Parameters:
    im_array - a numpy array representing a greyscale image
    rectangles - a list of (width, height) pairs of rectangle sizes
    """
    width, height = im_array.shape
    for dx, dy in rectangles:
        for x0, y0 in itertools.product(range(width-dx),range(height-dy)): 
            yield im_array[x0:x0+dx, y0:y0+dy]<commit_msg>Changed the approach of the windows() function. It now takes an image object as an input and uses the transform() method to do the resizing.<commit_after># Python OCR

import sys
import Image
import itertools
import numpy as np

if sys.version_info.major < 3:
    range = xrange 

# test data

testfile = "data/hk.png"

# code

def image_to_array(im):
    """
    Converts a PIL Image object into a numpy array representing
    a greyscale image.
    """
    im_greyscale = im_raw.convert("L")
    width, height = im_greyscale.size
    im_array = np.ndarray(width * height)
    for i, p in enumerate(im_greyscale.getdata()):
        im_array[i] = p
    im_array.shape = (width,height)
    return im_array


# sliding windows

def windows(im, rectangles, output_size):
    """
    Parameters:
    im - a PIL image
    rectangles - a list of (width, height) pairs of rectangle sizes
    size - the desired output size in (width, height) format
    """
    im_width, im_height = im.size
    for dx, dy in rectangles:
        for x0, y0 in itertools.product(range(im_width-dx),range(im_height-dy)): 
            yield im.transform(output_size, Image.EXTENT, (x0, y0, x0+dx, y0+dy))


def array_windows(im, rectangles, output_size):
    """
    Parameters:
    im_array - a numpy array representing a greyscale image
    rectangles - a list of (width, height) pairs of rectangle sizes
    """
    return (image_to_array(w) for w in windows(im, rectangles, output_size))


# testing code

im_raw = Image.open(testfile)
im_array = image_to_array(im_raw)
{'fraction_non_alphanumeric': 0.09606481481481481, 'fraction_numerical': 0.005401234567901234, 'mean_word_length': 3.230016313213703, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '148288', 'n_tokens_mistral': 865, 'n_tokens_neox': 859, 'n_words': 268}