OPENCV – PYTHON | COMPUTER VISION Applications | Libraries for software development | Must Know !
Python is a versatile language known for its rich ecosystem of libraries. Among these, Django, Flask, Pillow, Scikit-Image, and NumPy stand out as powerful tools that can help you build web applications, work with images, and perform advanced image processing and manipulation. In this blog, we’ll explore these libraries and provide practical examples of how to use them in various applications.
Django
Django is a high-level Python web framework that simplifies web development by providing reusable components and promoting the “Don’t Repeat Yourself” (DRY) principle. Let’s create a simple web app that displays a list of books using Django:
- First, install Django:
pip install django
- Create a Django project and a new app within it:
django-admin startproject booklist
cd booklist
python manage.py startapp books
- Define a model for books in the
models.py
file of thebooks
app:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=50)
- Create a view to display the list of books in the
views.py
file:
from django.shortcuts import render
from .models import Book
def book_list(request):
books = Book.objects.all()
return render(request, ‘books/book_list.html’, {‘books’: books})
- Create a template to render the list in the
templates
folder:
<!– books/book_list.html –>
<h1>Book List</h1>
<ul>
{% for book in books %}
<li>{{ book.title }} by {{ book.author }}</li>
{% endfor %}
</ul>
Add the
books
app to theINSTALLED_APPS
list in the project’ssettings.py
.Create and apply the database migration.
python manage.py makemigrations
python manage.py migrate
Finally, create a URL pattern in the
urls.py
file:
from django.urls import path
from . import views
urlpatterns = [
path(‘books/’, views.book_list, name=’book_list’),
]
Run the server with python manage.py runserver
and navigate to http://localhost:8000/books/
to see your book list.
Django is a versatile framework that can be used for creating a wide range of web applications, from simple blogs to complex e-commerce platforms.
Flask
Flask is a lightweight micro web framework for Python, known for its simplicity and ease of use. Here’s a minimal example of a Flask app that greets the user:
Install Flask:
pip install Flask
- Create a Python script for your Flask app:
from flask import Flask
app = Flask(__name__)
@app.route(‘/’)
def hello_world():
return ‘Hello, Flask!’
if __name__ == ‘__main__’:
app.run()
Run the script, and your Flask app will be accessible at http://localhost:5000
.
Flask is excellent for creating lightweight web applications or RESTful APIs.
Pillow
Pillow is a Python Imaging Library (PIL) fork, designed for working with images. Let’s use Pillow to apply a simple image filter:
- Install Pillow
pip install Pillow
- Create a Python script for image filtering:
from PIL import Image, ImageFilter
# Open an image file
image = Image.open(‘input.jpg’)
# Apply a filter (e.g., Gaussian blur)
blurred_image = image.filter(ImageFilter.GaussianBlur(5))
# Save the filtered image
blurred_image.save(‘output.jpg’)
blurred_image.show()
Pillow allows you to perform various image operations, such as resizing, cropping, and more.
Scikit-Image
Scikit-Image (skimage) is an image processing library built on top of SciPy. Let’s use Scikit-Image to perform edge detection on an image:
- Install scikit-image:
pip install scikit-image
- Create a Python script for edge detection:
from skimage import io, color, feature
import matplotlib.pyplot as plt
# Load an image
image = io.imread(‘input.jpg’)
# Convert it to grayscale
gray_image = color.rgb2gray(image)
# Apply Canny edge detection
edges = feature.canny(gray_image)
# Display the original and edge-detected images
plt.subplot(121)
plt.imshow(image, cmap=’gray’)
plt.title(‘Original Image’)
plt.axis(‘off’)
plt.subplot(122)
plt.imshow(edges, cmap=’gray’)
plt.title(‘Edge Detection’)
plt.axis(‘off’)
plt.show()
Scikit-Image is a powerful tool for various image processing tasks, including segmentation, feature extraction, and more.
NumPy
NumPy is a fundamental package for scientific computing in Python, providing support for arrays and matrices. Let’s use NumPy to perform a matrix operation:
- Install NumPy:
pip install numpy
- Create a Python script for matrix multiplication
import numpy as np
# Define two matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Perform matrix multiplication
result = np.dot(A, B)
print(result)
NumPy is essential for data manipulation, numerical computing, and scientific applications in Python.
These five libraries—Django, Flask, Pillow, Scikit-Image, and NumPy—offer a wide range of functionalities that cater to diverse applications. Whether you’re building a web application, processing images, or performing scientific computations, Python’s rich library ecosystem has you covered. So, dive in and start exploring the possibilities these libraries offer!