<jupyter_start><jupyter_text>Data Setup<jupyter_code>image = Image.open('bird.jpg')
# summarize some details about the image
print(image.format)
print(image.mode)
print(image.size)

pixels = np.asarray(image)
# convert from integers to floats
pixels = pixels.astype('float32')

# show the image
imshow(np.asarray(image))
def grid_display(list_of_images, list_of_titles=[], no_of_columns=6, figsize=(10,10)):

    fig = plt.figure(figsize=figsize)
    column = 0
    for i in range(len(list_of_images)):
        column += 1
        #  check for end of column and create a new figure
        if column == no_of_columns+1:
            fig = plt.figure(figsize=figsize)
            column = 1
        fig.add_subplot(1, no_of_columns, column)
        plt.imshow(list_of_images[i])
        plt.axis('off')
        if len(list_of_titles) >= len(list_of_images):
            plt.title(list_of_titles[i])<jupyter_output><empty_output><jupyter_text>Image NormalizationA pixel value is normalised by subtracting the min value and dividing by the max minus the min.<jupyter_code># Pre normalization -  pixel range is 0-255
print('Data Type: %s' % pixels.dtype)
print("Before normalization")
print('Min: %.3f, Max: %.3f' % (pixels.min(), pixels.max()))
# normalize to the range 0-1
pixels /= 255.0

# Post normalization
print("\nAfter normalization")
print('Min: %.3f, Max: %.3f' % (pixels.min(), pixels.max()))<jupyter_output>Data Type: float32
Before normalization
Min: 0.000, Max: 255.000

After normalization
Min: 0.000, Max: 1.000<jupyter_text>StandardizationSubstract by the mean and divide by the standard deviation<jupyter_code>pixels = np.asarray(image)
# convert from integers to floats
pixels = pixels.astype('float32')
# calculate global mean and standard deviation
mean, std = pixels.mean(), pixels.std()
print('Mean: %.3f, Standard Deviation: %.3f' % (mean, std))
# global standardization of pixels
pixels = (pixels - mean) / std
#Mean and standard deviation post standardization
mean, std = pixels.mean(), pixels.std()
print('Mean: %.3f, Standard Deviation: %.3f' % (mean, std))<jupyter_output>Mean: 0.000, Standard Deviation: 1.000<jupyter_text>Smart Image cropping library  KatnaKatna is a open source library that handles the task(s) for smart cropping.The Smart crop feature tries to automatically identify important image areas where the user will focus more and tries to retain it while cropping. For a given input cropping dimension/final output image size, following selection and filtering criteria are used.<jupyter_code>#!pip install katna
from Katna.image import Image
img_module = Image()
image_file_path = ('./bird.jpg')
# folder to save extracted images. Make sure the output folder is created beforehand
output_folder_cropped_image = "selectedcrops"
crop_list = img_module.crop_image(
   file_path=image_file_path,
   crop_width=600,
   crop_height=600,
   num_of_crops=5
)

 if len(crop_list) > 0:
        
        img = cv2.imread(image_file_path)
        
        for counter, crop in enumerate(crop_list):
            img_module.save_crop_to_disk(
                crop,
                img,
                file_path=output_folder_cropped_image,
                file_name="cropped_image_" + str(counter),
                file_ext=".jpeg",
            )
output_images=os.path.join(output_folder_cropped_image, "*.jpeg") 
print(output_images)
images = [ PIL.Image.open(f) for f in glob(output_images) ]
grid_display(images)<jupyter_output>selectedcrops/*.jpeg
{'fraction_non_alphanumeric': 0.10718390804597701, 'fraction_numerical': 0.019827586206896553, 'mean_word_length': 4.059593023255814, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, '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}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '410488', 'license': 'MIT', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1104, 'n_words': 309, 'path': 'Chapter 03/Image Preprocessing.ipynb', 'repo_name': 'bpbpublications/Practical-Full-stack-Machine-Learning', 'size': '3480'}