'For python 2.x.
# Returns
The next batch.'

def next(self):

    with self.lock:
        (index_array, current_index, current_batch_size) = next(self.index_generator)
    batch_x = np.zeros(((current_batch_size,) + self.image_shape), dtype=K.floatx())
    grayscale = (self.color_mode == 'grayscale')
    for (i, j) in enumerate(index_array):
        fname = self.filenames[j]
        img = load_img(os.path.join(self.directory, fname), grayscale=grayscale, target_size=self.target_size)
        x = img_to_array(img, data_format=self.data_format)
        x = self.image_data_generator.random_transform(x)
        x = self.image_data_generator.standardize(x)
        batch_x[i] = x
    if self.save_to_dir:
        for i in range(current_batch_size):
            img = array_to_img(batch_x[i], self.data_format, scale=True)
            fname = '{prefix}_{index}_{hash}.{format}'.format(prefix=self.save_prefix, index=(current_index + i), hash=np.random.randint(10000.0), format=self.save_format)
            img.save(os.path.join(self.save_to_dir, fname))
    if (self.class_mode == 'input'):
        batch_y = batch_x.copy()
    elif (self.class_mode == 'sparse'):
        batch_y = self.classes[index_array]
    elif (self.class_mode == 'binary'):
        batch_y = self.classes[index_array].astype(K.floatx())
    elif (self.class_mode == 'categorical'):
        batch_y = np.zeros((len(batch_x), self.num_class), dtype=K.floatx())
        for (i, label) in enumerate(self.classes[index_array]):
            batch_y[(i, label)] = 1.0
    else:
        return batch_x
    return (batch_x, batch_y)
