.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_rotate/04_01_rotate_image.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_04_rotate_04_01_rotate_image.py: Rotate Image ============ We use the rotate module to rotate a 2D image. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 15-25 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from matplotlib import animation from splineops.rotate.rotate import rotate from splineops.resize.resize import resize import requests from io import BytesIO from PIL import Image .. GENERATED FROM PYTHON SOURCE LINES 26-32 Load and Preprocess the Image ----------------------------- Load a Kodak image, convert it to grayscale, normalize it, and then resize it by a factor of (0.5, 0.5). After that, scale its intensity back to [0, 255] before rotation. .. GENERATED FROM PYTHON SOURCE LINES 32-102 .. code-block:: Python # Load the 'kodim17.png' image url = 'https://r0k.us/graphics/kodak/kodak/kodim22.png' response = requests.get(url) img = Image.open(BytesIO(response.content)) data = np.array(img, dtype=np.float64) # Convert to grayscale using a standard formula data_gray = ( data[:, :, 0] * 0.2989 + data[:, :, 1] * 0.5870 + data[:, :, 2] * 0.1140 ) # Normalize the grayscale image to [0,1] data_normalized = data_gray / 255.0 # Define zoom factors for resizing zoom_factors = (0.3, 0.3) interp_method = "cubic" # Resize the image using spline interpolation (this returns image in [0,1]) image_resized = resize( data_normalized, zoom_factors=zoom_factors, method=interp_method ) # Bring the resized image back to [0,255] image_resized = (image_resized * 255.0).astype(np.float32) # Define rotation angle rotation_angle = 45 # Use the center of the resized image as the custom center of rotation custom_center = (image_resized.shape[0] // 2, image_resized.shape[1] // 2) # Rotate the image (now in [0,255]) rotated_image = rotate( image_resized, angle=rotation_angle, center=custom_center ) # Create a circular mask radius = min(image_resized.shape) // 2 rows, cols = rotated_image.shape rr, cc = np.ogrid[:rows, :cols] # Display the original and rotated images fig, ax = plt.subplots(1, 2, figsize=(10, 5)) # Display the original image ax[0].imshow(image_resized, cmap="gray", vmin=0, vmax=255) ax[0].set_title("Original Resized Image") ax[0].axis("off") # Display the rotated image ax[1].imshow(rotated_image, cmap="gray", vmin=0, vmax=255) ax[1].scatter( custom_center[1], custom_center[0], color="red", label="Center of Rotation" ) ax[1].set_title(f"Rotated Image ({rotation_angle}°") ax[1].axis("off") ax[1].legend() plt.tight_layout() plt.show() .. image-sg:: /auto_examples/04_rotate/images/sphx_glr_04_01_rotate_image_001.png :alt: Original Resized Image, Rotated Image (45° :srcset: /auto_examples/04_rotate/images/sphx_glr_04_01_rotate_image_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.675 seconds) .. _sphx_glr_download_auto_examples_04_rotate_04_01_rotate_image.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/splineops/splineops.github.io/main?urlpath=lab/tree/notebooks_binder/auto_examples/04_rotate/04_01_rotate_image.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/04_rotate/04_01_rotate_image.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 04_01_rotate_image.ipynb <04_01_rotate_image.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 04_01_rotate_image.py <04_01_rotate_image.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 04_01_rotate_image.zip <04_01_rotate_image.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_