Skip to main content

Extracts a part of the image specified by width and height from the given image file path using OpenCV.

Project description

Extracts a part of the image specified by width and height from the given image file path using OpenCV.

pip install getpartofimg

Tested against Windows 10 / Python 3.10 / Anaconda

Parameters:

image : Any The file path of the image from which a part will be extracted.

width : int The desired width of the extracted part.

height : int The desired height of the extracted part.

allow_resize : bool, optional (default=True) If True, allows resizing of the input image when the specified width or height is greater than the original image's dimensions.

Returns:

numpy.ndarray The extracted part of the image as a NumPy array.

Raises:

ValueError If the 'allow_resize' parameter is set to False and the specified width or height is greater than the original image's dimensions, a ValueError is raised with a message indicating that the image is too small.

Notes:

The function uses the 'cv2.imread_plus' function ( https://github.com/hansalemaos/a_cv_imwrite_imread_plus ) to read the image from the given file path. It then calculates the possible ranges for extracting the desired part based on the image's dimensions and the specified width and height. If the 'allow_resize' parameter is True, the function resizes the image using 'cv2.easy_resize_image' ( https://github.com/hansalemaos/a_cv2_easy_resize ) with the 'cv2.INTER_AREA' interpolation method to fit the desired part. Otherwise, it raises a ValueError.

Random starting coordinates within the allowable range are generated to extract the part of the image specified by the given width and height.

# Example usage:
import cv2
from getpartofimg import get_part_of_image
im = get_part_of_image(image=r"https://raw.githubusercontent.com/hansalemaos/screenshots/main/pic5.png", width=600, height=500, allow_resize=True)
cv2.imwrite('c:\\testimage.png', im)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

getpartofimg-0.10.tar.gz (49.7 kB view hashes)

Uploaded Source

Built Distribution

getpartofimg-0.10-py3-none-any.whl (50.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page