Easy Images
py_simple.easy_images
easy_images is meant to simplify common image processing tasks (resizing, converting, rotating, and inspecting images) using Pillow.
ImageProcessingError
Bases: Exception
Raised when a py_simple image function fails to complete.
This covers a missing input file, an unsupported/corrupt image format, or any other error that prevents a function from returning a real result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Description of what went wrong. |
required |
adjust_brightness(input_path, output_path, value)
Adjusts the brightness of an image by darkening or brightening it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path of the image to adjust. |
required |
output_path
|
str
|
Path to save the adjusted image to. |
required |
value
|
float
|
A value of 0 gives a completely black image, 1 leaves it untouched, and anything above 1 brightens it. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
from py_simple import adjust_brightness
adjust_brightness("photo.jpg", "photo_bright.jpg", 1.5)
from PIL import Image, ImageEnhance
with Image.open("photo.jpg") as img:
ImageEnhance.Brightness(img).enhance(1.5).save("photo_bright.jpg")
convert_image(input_path, output_path)
Convert an image to a different format based on the output file's extension (e.g. PNG to JPG).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path of the image to convert. |
required |
output_path
|
str
|
Path to save the converted image to. The new format is inferred from this path's extension. |
required |
Example
from py_simple import convert_image
convert_image("photo.png", "photo.jpg")
from PIL import Image
with Image.open("photo.png") as img:
img.convert("RGB").save("photo.jpg")
convert_to_greyscale(input_path, output_path)
Convert an image to grayscale and save it.
Each pixel becomes a brightness value from 0 (black) to 255 (white),
giving 256 possible shades of gray. Pillow calls this 8-bit
grayscale. Mode L stores the gray value; mode LA also stores
opacity. Outputs ending in .jpg, .jpeg or .bmp use L because
Pillow's JPEG/BMP encoders cannot save LA and would raise
OSError. Other extensions use LA.
The original dimensions are kept.
PNG, TIFF and WebP output retain transparency. JPEG, BMP and GIF output discard it without blending a background. Only the first frame of animated or multi-frame images is converted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str | PathLike[str]
|
Path of the source image. |
required |
output_path
|
str | PathLike[str]
|
Path to save the grayscale image to. The file extension determines the output format. |
required |
Raises:
| Type | Description |
|---|---|
ImageProcessingError
|
If the input file is missing or its image format is not recognized. |
OSError
|
If the image cannot be read or written. |
ValueError
|
If the output file extension is not recognized. |
Example
from py_simple import convert_to_greyscale
convert_to_greyscale("photo.jpg", "photo_gray.png")
from PIL import Image
with Image.open("photo.jpg") as img:
img.convert("LA").save("photo_gray.png")
create_thumbnail(input_path, output_path, max_width, max_height)
Create a thumbnail that fits within the given dimensions.
The image keeps its original aspect ratio and is never enlarged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path of the source image. |
required |
output_path
|
str
|
Path to save the thumbnail to. |
required |
max_width
|
int
|
Maximum thumbnail width in pixels. |
required |
max_height
|
int
|
Maximum thumbnail height in pixels. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If either maximum dimension is not a positive integer. |
Example
from py_simple import create_thumbnail
create_thumbnail("photo.jpg", "thumbnail.jpg", 320, 320)
from PIL import Image
with Image.open("photo.jpg") as img:
img.thumbnail((320, 320))
img.save("thumbnail.jpg")
crop_image(input_path, output_path, left, top, right, bottom)
Crop an image to a bounding box and save the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path of the image to crop. |
required |
output_path
|
str
|
Path to save the cropped image to. |
required |
left
|
int
|
Left pixel coordinate. |
required |
top
|
int
|
Top pixel coordinate. |
required |
right
|
int
|
Right pixel coordinate. |
required |
bottom
|
int
|
Bottom pixel coordinate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If coordinates are not non-negative integers, if
|
ImageProcessingError
|
If the input file is missing or not a supported image. |
Example
from py_simple import crop_image
crop_image("photo.jpg", "cropped.jpg", 10, 10, 100, 100)
from PIL import Image
with Image.open("photo.jpg") as img:
img.crop((10, 10, 100, 100)).save("cropped.jpg")
flip_image(input_path, output_path, direction='horizontal')
Flip an image horizontally or vertically and save the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path of the image to flip. |
required |
output_path
|
str
|
Path to save the flipped image to. |
required |
direction
|
str
|
Either "horizontal" (left-right) or "vertical" (top-bottom). Defaults to "horizontal". |
'horizontal'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ImageProcessingError
|
If the input file is missing or not a supported image. |
Example
from py_simple import flip_image
flip_image("photo.jpg", "photo_flipped.jpg", "horizontal")
from PIL import Image
with Image.open("photo.jpg") as img:
img.transpose(Image.Transpose.FLIP_LEFT_RIGHT).save(
"photo_flipped.jpg"
)
get_image_info(input_path)
Get basic information about an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path of the image to inspect. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
With keys "width", "height", "format", and "mode". |
Example
from py_simple import get_image_info
info = get_image_info("photo.jpg")
# {"width": 1920, "height": 1080, "format": "JPEG", "mode": "RGB"}
from PIL import Image
with Image.open("photo.jpg") as img:
info = {
"width": img.width,
"height": img.height,
"format": img.format,
"mode": img.mode,
}
resize_image(input_path, output_path, width, height)
Resize an image to the given dimensions and save it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path of the image to resize. |
required |
output_path
|
str
|
Path to save the resized image to. |
required |
width
|
int
|
Target width in pixels. |
required |
height
|
int
|
Target height in pixels. |
required |
Example
from py_simple import resize_image
resize_image("photo.jpg", "photo_small.jpg", 320, 240)
from PIL import Image
with Image.open("photo.jpg") as img:
img.resize((320, 240)).save("photo_small.jpg")
rotate_image(input_path, output_path, angle)
Rotate an image by the given angle (counter-clockwise) and save it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path of the image to rotate. |
required |
output_path
|
str
|
Path to save the rotated image to. |
required |
angle
|
float
|
Degrees to rotate counter-clockwise, e.g. 90. |
required |
Example
from py_simple import rotate_image
rotate_image("photo.jpg", "photo_rotated.jpg", 90)
from PIL import Image
with Image.open("photo.jpg") as img:
img.rotate(90, expand=True).save("photo_rotated.jpg")