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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file getpartofimg-0.10.tar.gz.

File metadata

  • Download URL: getpartofimg-0.10.tar.gz
  • Upload date:
  • Size: 49.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for getpartofimg-0.10.tar.gz
Algorithm Hash digest
SHA256 fdf6e590b0bd67994b478d3c58e843c485da3674eb32e1759eda847ab83113b5
MD5 2c319165afa569294c41b670d61b206a
BLAKE2b-256 30dac0bb7334099a407c85b0befd644496a045418ec7d41114bfc43cfecf212b

See more details on using hashes here.

File details

Details for the file getpartofimg-0.10-py3-none-any.whl.

File metadata

  • Download URL: getpartofimg-0.10-py3-none-any.whl
  • Upload date:
  • Size: 50.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for getpartofimg-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 4d5c32f6d1c5b78274cd4a3efa8a9daf63d1bd40a9ed2692d37b17e56f8e21e3
MD5 81dc90db669bc5f57697da70d794868d
BLAKE2b-256 9df814536520c673d6cbddd81badd3af3670d5573b5cd00e68cbb5e7cefe04c2

See more details on using hashes here.

Supported by

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