Skip to main content

A opinionated tool for coercing data into something displayable

Project description

img-show

img-show is a Python package that simplifies the process of displaying images using OpenCV. It allows you to load, coerce, and display images of various types, shapes, and data formats effortlessly, handling the complexities of image preprocessing and window management for you.

Features

  • Image Loading and Coercion:
    • Supports images in NumPy arrays.
    • Automatically handles PyTorch tensors.
    • Coerces images into valid shapes for display, even with extra dimensions.

Installation

You can install img-show via pip:

pip install img-show

Requirements

  • Python >= 3.8
  • NumPy
  • OpenCV
  • Tkinter (usually included with Python installations)

Usage

Displaying an Image

img-show provides a simple show_img function to display images.

from img_show import show_img

# Assuming 'img' is a NumPy array or a PyTorch tensor
show_img(img)

Handling Different Image Formats

img-show can handle images in various formats, including NumPy arrays and PyTorch tensors.

import numpy as np
import torch
from img_show import show_img

# NumPy array
img_numpy = np.random.rand(256, 256, 3)
show_img(img_numpy)

# PyTorch tensor
img_tensor = torch.randn(3, 256, 256)
show_img(img_tensor)

Customizing Display Options

You can customize the window name, wait delay, and whether to wait for a key press.

from img_show import show_img

show_img(img, window_name='My Image', wait_delay=5000, do_wait=True)

Resizing Images Automatically

If the image is larger than the screen, img-show automatically resizes the window to fit the screen while maintaining the aspect ratio.

from img_show import show_img

# Display a large image
large_img = np.random.rand(4000, 6000, 3)
show_img(large_img)

Handling Images with Extra Dimensions

img-show can coerce images with extra dimensions (e.g., singleton dimensions) into valid shapes for display.

import numpy as np
from img_show import show_img

# Image with an extra leading dimension
extra_dim_img = np.random.rand(1, 256, 256, 3)
show_img(extra_dim_img)

# Image with multiple singleton dimensions
multiple_extra_dims_img = np.random.rand(1, 1, 256, 256, 3)
show_img(multiple_extra_dims_img)

# Image with singleton channel dimension
singleton_channel_img = np.random.rand(256, 256, 1)
show_img(singleton_channel_img)

Handling Images in Different Channel Orders

img-show automatically handles different channel orders (e.g., channels-first vs. channels-last).

import numpy as np
from img_show import show_img

# Channels-first format (e.g., PyTorch)
channels_first_img = np.random.rand(3, 256, 256)
show_img(channels_first_img)

channels_first_img = np.random.rand(1, 1, 3, 256, 256, 1)
show_img(channels_first_img)

# Channels-last format (e.g., Numpy)
channels_last_img = np.random.rand(256, 256, 3)
show_img(channels_last_img)

channels_last_img = np.random.rand(1, 1, 256, 256, 3, 1)
show_img(channels_last_img)

API Reference

show_img Function

Displays an image using OpenCV's imshow function, automatically handling image coercion and window sizing.

show_img(img: Any, window_name: str = ' ', wait_delay: int = 0, do_wait: bool = True)

Parameters

  • img: The image to display. Can be a NumPy array or a PyTorch tensor.
  • window_name: The name of the display window (default is a blank space).
  • wait_delay: The delay in milliseconds for cv2.waitKey() (default is 0, which waits indefinitely).
  • do_wait: Whether to wait for a key press after displaying the image (default is True).

coerce_img

Converts the image to a NumPy array with a valid shape and data type for display.

coerce_img(img: Any) -> np.ndarray

License

img-show is licensed under the MIT License.

Author

Ben Elfner

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

img_show-0.1.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

img_show-0.1.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file img_show-0.1.1.tar.gz.

File metadata

  • Download URL: img_show-0.1.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.20

File hashes

Hashes for img_show-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f60f64c54f7a8eaf0bdb27e3aef0f55871b38b10d9730039984e317abc0f9a5a
MD5 7243400c1fb6cc709a224e45aa35060d
BLAKE2b-256 ef8e4669ee7c6e6357453c376379c22b63aae4f5f74f393bb2a4ed9de25aaea6

See more details on using hashes here.

File details

Details for the file img_show-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: img_show-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.20

File hashes

Hashes for img_show-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 afaf84387167b70f69c1c0622e2dfb38effdc1b52e86d2f48b7512fb27a4d2f9
MD5 c471a7f8d5f19be697489a63a0e6cfd7
BLAKE2b-256 07a2b84955ba644e99283b54ac4db22a9807fa08f0128ae05fba0cfac3b39371

See more details on using hashes here.

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