In the pre-processing step of an image processing pipeline, it is often common to crop, resize, flip images. Here is how to do it in Python...
In this series of post, we'll use the package scikit-image.
1. Crop
To crop an image, we use the slicing and indexing syntax available for numpy arrays.
++++ Python Script ++++++++ End of Python Script ++++
The first lines of the script contain the imports:
1.1. Loading the image coins
-
import numpy as np: Imports NumPy, the foundation for handling numerical data and arrays in Python. The aliasnpis standard practice. -
import matplotlib.pyplot as plt: Imports the plotting module from Matplotlib, used to render charts, plots, and images. The aliaspltis also standard. -
import skimage as ski: Imports scikit-image, a dedicated image processing library built on top of NumPy.
Then, we load the coins image in skimage with img = ski.data.coins() as a numpy array.
Fig. 1: Image coins from skimage |
1.2. Cropping the Image
- For cropping the image (line #7), we define a bounding box from its top left corner whose XY-coordinates (x0,y0) are (180,100) and the bottom right corner (x1,y1) are equal to (230,150).
- However, an image is defined by a 2D numpy array where the first axis corresponds to the Y and the second axis to the X. Thus, to define the bounding box with array indexing, we are first defining the Y range and then the X range. So, we need to write the box coordinates like this....
img[y0:y1, x0:x1].
1.3. Exercises
- Exercise: Try to crop the same coin giving the coin center
(205,125)and the diameter25. - Exercise: Try to crop the coin giving the top left corner
(180,100)and the box size(50,50)
2. Flip
For flipping, we use the step in the indexing syntax (as a reminder, for indexing, the syntax is arr[start:stop:step]. By assigning -1 to the step, we reverse/flip the numpy array. For example..
a = np.array([0,1,2,3]) b = a[::-1] # [3,2,1,0]
Note: This syntax also works with List
++++ Python Script ++++++++ End of Python Script ++++
... and the corresponding montage.
![]() |
| Fig. 3: A) Original, B) Flip vertically, C) Flip horizontally, D) Flip along both axes |
3. Padding
For padding, we use a dedicated function of numpy 'pad(..)'
++++ Python Script ++++++++ End of Python Script ++++
Various modes are available for the padding:
- ‘constant’ (default): Pads with a constant value.
- ‘edge’: Pads with the edge values of array.
- ‘linear_ramp’: Pads with the linear ramp between end_value and the array edge value.
- ‘maximum’: Pads with the maximum value of all or part of the vector along each axis.
- ‘mean’: Pads with the mean value of all or part of the vector along each axis.
- ‘median’: Pads with the median value of all or part of the vector along each axis.
- ‘minimum’: Pads with the minimum value of all or part of the vector along each axis.
- ‘reflect’: Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.
- ‘symmetric’: Pads with the reflection of the vector mirrored along the edge of the array.
- ‘wrap’: Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.
- ‘empty’: Pads with undefined values.
- <function> : Function for setting the extra pixels.
The padding is interesting when you extract objects of interest by crop and want to put all the cropped images in a box of identical size like in the following montage...
4. Translation and Rotation
4.1. The Script
++++ Python Script ++++TODO++++ End of Python Script ++++
4.2. Description
5. Downsampling (binning)
TODO
6. Resizing
TODO
Thank you for reading.




No comments:
Post a Comment