scikit-image Python (How It Works: A Guide for Developers)
Scikit-image is a collection of algorithms designed for image processing in Python. It’s freely available and unrestricted, boasting high-quality, peer-reviewed code from an active community of volunteers. The Scikit-image project started in Google in 2009 as part of the Google Summer Code program under the mentorship of Stefan van der Walt and other Scikit-image contributors. It aimed to create a Python library for image processing that would be easy to use, efficient, and extendable for academic and industrial applications. In this article, we will learn about the Scikit-image Python imaging library and a PDF generation library from IronSoftware called IronPDF.
Getting Started
To learn about the Scikit-image, check out the official website. Additionally, Data Carpentry offers a great lesson on image processing in Python using Scikit.
Installation via pip
- Ensure you have Python installed (at least version 3.10).
Open your terminal or command prompt.
- Update pip:
python -m pip install -U pip
python -m pip install -U pip
SHELL- Install scikit-image via pip:
python -m pip install -U scikit-image
python -m pip install -U scikit-image
SHELL- To access demo datasets, use:
python -m pip install -U scikit-image[data]
python -m pip install -U scikit-image[data]
SHELL- For additional scientific packages, including parallel processing capabilities:
python -m pip install -U scikit-image[optional]
python -m pip install -U scikit-image[optional]
SHELL
Basic Example
import skimage.io
import matplotlib.pyplot as plt
# Load an image from file
image = skimage.io.imread(fname='land.jpg')
# Display the image
plt.imshow(image)
plt.show()
import skimage.io
import matplotlib.pyplot as plt
# Load an image from file
image = skimage.io.imread(fname='land.jpg')
# Display the image
plt.imshow(image)
plt.show()
Filters
import skimage as ski
# Load a sample image from the scikit-image default collection
image = ski.data.coins()
# Apply a Sobel filter to detect edges
edges = ski.filters.sobel(image)
# Display the edges
ski.io.imshow(edges)
ski.io.show()
import skimage as ski
# Load a sample image from the scikit-image default collection
image = ski.data.coins()
# Apply a Sobel filter to detect edges
edges = ski.filters.sobel(image)
# Display the edges
ski.io.imshow(edges)
ski.io.show()
Scikit-image, often abbreviated as skimage, is a powerful Python library for image-processing tasks. It is built on top of NumPy arrays, SciPy, and matplotlib, and provides various functions and algorithms to manipulate and analyze images. The skimage.data.coins()
is used to get access to sample images from the library. skimage.filters
provides access to inbuilt filters and utility functions.
Key Features of Scikit-image
1. Image Filtering and Edge Detection
from skimage import io, filters
# Load an image
image = io.imread('image.jpg')
# Apply Gaussian blur
blurred_image = filters.gaussian(image, sigma=1.0)
# Apply Sobel edge detection
edges = filters.sobel(image)
# Display the original image, blurred image, and edges
io.imshow_collection([image, blurred_image, edges])
io.show()
from skimage import io, filters
# Load an image
image = io.imread('image.jpg')
# Apply Gaussian blur
blurred_image = filters.gaussian(image, sigma=1.0)
# Apply Sobel edge detection
edges = filters.sobel(image)
# Display the original image, blurred image, and edges
io.imshow_collection([image, blurred_image, edges])
io.show()
Output
2. Feature Extraction with HOG (Histogram of Oriented Gradients)
from skimage import io, color, feature
# Load an example image and convert to grayscale
image = io.imread('image.jpg')
gray_image = color.rgb2gray(image)
# Compute HOG features and visualize them
hog_features, hog_image = feature.hog(gray_image, visualize=True)
# Display the original image and the HOG image
io.imshow_collection([image, gray_image, hog_image])
io.show()
from skimage import io, color, feature
# Load an example image and convert to grayscale
image = io.imread('image.jpg')
gray_image = color.rgb2gray(image)
# Compute HOG features and visualize them
hog_features, hog_image = feature.hog(gray_image, visualize=True)
# Display the original image and the HOG image
io.imshow_collection([image, gray_image, hog_image])
io.show()
Output
3. Geometric Transformation - Resizing and Rotation
from skimage import io, transform
# Load an image
image = io.imread('image.jpg')
# Resize image by dividing its dimensions by 2
resized_image = transform.resize(image, (image.shape[0] // 2, image.shape[1] // 2))
# Rotate image by 45 degrees
rotated_image = transform.rotate(image, angle=45)
# Display the original image, resized image, and rotated image
io.imshow_collection([image, resized_image, rotated_image])
io.show()
from skimage import io, transform
# Load an image
image = io.imread('image.jpg')
# Resize image by dividing its dimensions by 2
resized_image = transform.resize(image, (image.shape[0] // 2, image.shape[1] // 2))
# Rotate image by 45 degrees
rotated_image = transform.rotate(image, angle=45)
# Display the original image, resized image, and rotated image
io.imshow_collection([image, resized_image, rotated_image])
io.show()
Output
4. Image Denoising with Total Variation Filter
from skimage import io, restoration
# Load a noisy image
image = io.imread('image.jpg')
# Apply total variation denoising
denoised_image = restoration.denoise_tv_chambolle(image, weight=0.1)
# Display the noisy image and the denoised image
io.imshow_collection([image, denoised_image])
io.show()
from skimage import io, restoration
# Load a noisy image
image = io.imread('image.jpg')
# Apply total variation denoising
denoised_image = restoration.denoise_tv_chambolle(image, weight=0.1)
# Display the noisy image and the denoised image
io.imshow_collection([image, denoised_image])
io.show()
Output
You can find more about image processing and NumPy array on the official page.
Introducing IronPDF
IronPDF is a robust Python library designed to handle the creation, editing, and signing of PDF documents using HTML, CSS, images, and JavaScript. It prioritizes performance efficiency and operates with minimal memory usage. Key features include:
HTML to PDF Conversion: Convert HTML files, HTML strings, and URLs into PDF documents, leveraging capabilities like rendering webpages using the Chrome PDF renderer.
Cross-Platform Support: Compatible with Python 3+ across Windows, Mac, Linux, and various Cloud Platforms. IronPDF is also accessible for .NET, Java, Python, and Node.js environments.
Editing and Signing: Customize PDF properties, enforce security measures such as passwords and permissions, and apply digital signatures seamlessly.
Page Templates and Settings: Create PDF layouts with features like headers, footers, page numbers, adjustable margins, custom paper sizes, and responsive designs.
- Standards Compliance: Adheres strictly to PDF standards such as PDF/A and PDF/UA, ensures UTF-8 character encoding compatibility, and adeptly manages assets such as images, CSS stylesheets, and fonts.
Installation
pip install ironpdf
pip install scikit-image
pip install ironpdf
pip install scikit-image
Generate PDF Documents using IronPDF and Scikit Image
Prerequisites
- Make sure Visual Studio Code is installed as a code editor
- Python version 3 is installed
To start with, let us create a Python file to add our scripts.
Open Visual Studio Code and create a file, scikitDemo.py
.
Install necessary libraries:
pip install scikit-image
pip install ironpdf
pip install scikit-image
pip install ironpdf
Then add the below Python code to demonstrate the usage of IronPDF and scikit-image Python packages.
from skimage import io, filters
from ironpdf import *
# Apply your license key
License.LicenseKey = "YOUR_LICENSE_KEY"
# Load an image
image = io.imread('image.jpg')
# Apply Gaussian blur
blurred_image = filters.gaussian(image, sigma=1.0)
# Apply Sobel edge detection
edges = filters.sobel(image)
# Save the results to a file
io.imshow_collection([image, blurred_image, edges]).savefig('ironPdf-skimage.png')
# Convert the saved image to a PDF document
ImageToPdfConverter.ImageToPdf("ironPdf-skimage.png").SaveAs("ironPdf-skimage.pdf")
# Display the images
io.show()
from skimage import io, filters
from ironpdf import *
# Apply your license key
License.LicenseKey = "YOUR_LICENSE_KEY"
# Load an image
image = io.imread('image.jpg')
# Apply Gaussian blur
blurred_image = filters.gaussian(image, sigma=1.0)
# Apply Sobel edge detection
edges = filters.sobel(image)
# Save the results to a file
io.imshow_collection([image, blurred_image, edges]).savefig('ironPdf-skimage.png')
# Convert the saved image to a PDF document
ImageToPdfConverter.ImageToPdf("ironPdf-skimage.png").SaveAs("ironPdf-skimage.pdf")
# Display the images
io.show()
Code Explanation
This code snippet demonstrates how to use scikit-image (skimage
) and IronPDF together to process an image and convert the results into a PDF document. Here’s an explanation of each part:
Import Statements: Imports necessary functions from scikit-image for image loading and image filtering, and imports IronPDF functionality.
Applying License Key: Sets the license key for IronPDF. This step is required to use IronPDF functionalities.
Loading and Processing an Image: Loads an image named
'image.jpg'
using scikit-image'sio.imread
function. Then applies Gaussian blur to the loaded image usingfilters.gaussian
with a sigma value of 1.0, and applies Sobel edge detection to the loaded image usingfilters.sobel
.Displaying and Saving Results:
io.imshow_collection([image, blurred_image, edges]).savefig('ironPdf-skimage.png')
: Saves a collection of images (original, blurred, and edges) as'ironPdf-skimage.png'
.Converting Image to PDF:
ImageToPdfConverter.ImageToPdf("ironPdf-skimage.png").SaveAs("ironPdf-skimage.pdf")
: Converts the saved PNG image to a PDF document using IronPDF's functionality.- Displaying the Images:
io.show()
: Displays the images in a graphical window.
This code snippet combines the capabilities of scikit-image for image processing and IronPDF for converting processed images into PDF documents. It demonstrates loading an image, applying Gaussian blur and Sobel edge detection, saving them as a PNG file, converting the PNG to PDF using IronPDF, and displaying the processed images. This integration is useful for tasks where images need to be processed, analyzed, and documented in a PDF format, such as in scientific research, image analysis reports, or automated document generation workflows.
Output
IronPDF License
IronPDF runs on a license key for Python. IronPDF for Python offers a free-trial license to allow users to check out its extensive features before purchase.
Place the License Key at the start of the script before using the IronPDF package:
from ironpdf import *
# Apply your license key
License.LicenseKey = "YOUR_LICENSE_KEY"
from ironpdf import *
# Apply your license key
License.LicenseKey = "YOUR_LICENSE_KEY"
Conclusion
scikit-image empowers Python developers to tackle image-related tasks efficiently. Whether you’re working on computer vision, medical imaging, or artistic projects, this package has you covered. scikit-image is a versatile and powerful library for image processing in Python, offering a wide range of functions and algorithms for tasks such as filtering, segmentation, feature extraction, and geometric transformations. Its seamless integration with other scientific libraries makes it a preferred choice for researchers, developers, and engineers working with image analysis and computer vision applications.
IronPDF is a Python library that facilitates the creation, editing, and manipulation of PDF documents within Python applications. It offers features such as generating PDF files from various sources like HTML, images, or existing PDFs. Additionally, IronPDF supports tasks like merging or splitting PDF documents, adding annotations, watermarks, or digital signatures, extracting text or images from PDFs, and managing document properties such as metadata and security settings. This library provides an efficient way to handle PDF-related tasks programmatically, making it suitable for applications requiring document generation, report creation, or document management functionalities.
Together with both libraries, users can work with images, process them efficiently, and store the results in PDF docs for archiving purposes.