<jupyter_start><jupyter_text>Resizer in SoftwareThis reference design illustrates how to run a resize operation in **software** using Jupyter Notebooks and Python.In this example, we will use the **Bilinear** interpolation.> The resize IP transforms the source image to the size of the destination image. Different types of interpolation techniques can be used in resize function,namely: Nearest-neighbor, Bilinear, and Area interpolation. The type of interpolation can be passed as a parameter to the Python API.    > Reference: https://www.xilinx.com/support/documentation/sw_manuals/xilinx2017_1/ug1233-xilinx-opencv-user-guide.pdf Contents     * [Image resize using PIL library](Image-resize-using-PIL-library)   * [Import libraries](Import-libraries)* [Create an Image object](Create-an-Image-object)* [Display the image to be resized](Display-the-image-to-be-resized)* [Resizing](Resizing)* [Display resized image](Display-resized-image)* [References](References) Image resize using PIL libraryPIL is the **Python Imaging Library**. This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities.The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.Reference: https://pillow.readthedocs.io/en/5.2.x/reference/Image.html?highlight=Image.resize Import libraries1. **PIL** library to load and resize the image   2. **numpy** to store the pixel array of the image3. **matplotlib** to show the image in the notebook<jupyter_code>from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Create an Image objectWe will load image from the SD card and create an Image object. Then we create a numpy array of the pixels.Image By Aboubakrhadnine - Own work, CC BY-SA 4.0, Link.<jupyter_code>image_path = "images/sahara.jpg"
original_image = Image.open(image_path)<jupyter_output><empty_output><jupyter_text>Display the image to be resizedLet's also show the original image size. It may take a while to render a large picture. For better visual effect, we double the size of the canvas.The following code only changes the display size, not the picture itself:```pythoncanvas = plt.gcf()size = canvas.get_size_inches()canvas.set_size_inches(size*2)```<jupyter_code>canvas = plt.gcf()
size = canvas.get_size_inches()
canvas.set_size_inches(size*2)

old_width, old_height = original_image.size
print("Image size: {}x{} pixels.".format(old_width, old_height))
_ = plt.imshow(original_image)<jupyter_output>Image size: 3840x2160 pixels.<jupyter_text>ResizingWe will set image resize dimensions.<jupyter_code>resize_factor = 2
new_width = int(old_width/resize_factor)
new_height = int(old_height/resize_factor)<jupyter_output><empty_output><jupyter_text>We will use `resize()` method from the PIL library.We map multiple input pixels to a single output pixels to downscale the image   The Python Imaging Library provides different resampling filters. We will just choose the bilinear filter in our example.Pick one nearest pixel from the input image. Ignore all other input pixels.<jupyter_code>resized_image = original_image.resize((new_width, new_height), Image.BILINEAR)<jupyter_output><empty_output><jupyter_text>Display resized image<jupyter_code>print("Image size: {}x{} pixels.".format(new_width, new_height))
_ = plt.imshow(resized_image)<jupyter_output>Image size: 1920x1080 pixels.<jupyter_text>We can time the resize in software operation.<jupyter_code>%%timeit
resized_image = original_image.resize((new_width, new_height), Image.BILINEAR)<jupyter_output>1 loop, best of 3: 1.02 s per loop
{'fraction_non_alphanumeric': 0.08497246262785209, 'fraction_numerical': 0.01049042748492001, 'mean_word_length': 6.306513409961686, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 28, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '622162', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1097, 'n_words': 392, 'path': 'pynq_helloworld/notebooks/edge/resizer_ps.ipynb', 'repo_name': 'mahinlma/PYNQ-experiment', 'size': '3813'}