6, అక్టోబర్ 2025, సోమవారం

Reading images in Python............python program 2ndB.Tech...47/50

 import cv2

 

# Define the path to your image file

image_path = "path/to/your/image.jpg"  # Replace with the actual path to your image

 

# Read the image

# cv2.imread() returns a NumPy array representing the image data

# The second argument (flags) is optional:

# cv2.IMREAD_COLOR (default): Loads a color image (BGR format)

# cv2.IMREAD_GRAYSCALE: Loads a grayscale image

# cv2.IMREAD_UNCHANGED: Loads image as is, including alpha channel if present

img = cv2.imread(image_path, cv2.IMREAD_COLOR)

 

# Check if the image was loaded successfully

if img is None:

    print(f"Error: Could not load image from {image_path}")

else:

    print(f"Image loaded successfully. Shape: {img.shape}")

    # You can now process or display the image (e.g., using cv2.imshow)

    cv2.imshow("Loaded Image", img)

    cv2.waitKey(0)  # Wait indefinitely until a key is pressed

    cv2.destroyAllWindows() # Close all OpenCV windows




  • hon provides simple tools to work with images. Whether you're looking to open, view or save images there are many libraries to help. Let's see how to process the images using different libraries.

1. Using ImageIO

ImageIO is used for reading and writing images in various formats like PNG, JPEG, GIF, TIFF and more. It's particularly useful for scientific and multi-dimensional image data likie medical images or animated GIFs. It’s simple and works across different platforms. You can download the image from here.

Output:

download
ImageIO

2. Using OpenCV

OpenCV (Open Source Computer Vision Library) is one of the most popular tools for real-time computer vision. It supports image processing, face detection, video analysis, object detection and much more. It can Can apply filters, transformations and can bes used for face/object recognition. This library is cross-platform that is it is available on multiple programming languages such as Python, C++, etc. 

Output:

g4g
OpenCV

3. Using Matplotlib

Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002. Matplotlib comes with a wide variety of plots. Plots helps to understand trends, patterns and to make correlations. They’re typically instruments for reasoning about quantitative information. 

import matplotlib.image as mpimg
import matplotlib.pyplot as plt

img = mpimg.imread('g4g.png')

plt.imshow(img)

Output:

download
Matplotlib

4. Using PIL

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. Pillow is user friendly and easy to use library developed by Alex Clark and other contributors. 

from PIL import Image

img = Image.open('g4g.png')

img.show()

print(img.mode)

Output:

out1-1
PIL

These Python libraries make image reading and processing easy and efficient for all kinds of tasks. Choose the one that best fits your needs and start exploring image data with just a few lines of code.

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి