batch_crop package¶
Submodules¶
batch_crop.batch_crop module¶
Crop files in bulk, maintaining the crop region’s relative position
-
class
batch_crop.batch_crop.
BatchCropper
(window: tkinter.Tk)[source]¶ Bases:
tkinter.Frame
Tkinter GUI app for performing batch crops of images
-
scale_factor
¶ Factor by which original image dimensions are multiplied to yield the displayed image dimensions
- Type
int
-
image_tk
¶ Image to display to the user for crop region selection
- Type
ImageTk.PhotoImage
-
to_crop
¶ The paths of all images to crop
- Type
List[str]
-
start_x
¶ x-coordinate of one corner of the selected region
- Type
float
-
start_y
¶ y-coordinate of one corner of the selected region
- Type
float
-
end_x
¶ x-coordinate of the opposing corner of the selection
- Type
float
-
end_y
¶ y-coordinate of the opposing corner of the selection
- Type
float
-
rect
¶ Displayed rectangle that demarcates the selected region to crop
- Type
tk.Canvas
-
orig_size
¶ The original size of the loaded image, stored as
(width, height)
- Type
Tuple[float, float]
-
canvas
¶ Where the image is displayed to the user
- Type
tk.Canvas
- Type
tk.Button
- Type
tk.Button
- Type
tk.Button
- Type
tk.Button
- Type
tk.Button
- Type
tk.Button
- Type
tk.Button
-
label_instructions
¶ - Type
tk.Label
-
label_dir
¶ Displays the directory of images to crop
- Type
tk.Label
-
label_dir_label
¶ Displays the label for the directory
- Type
tk.Label
-
label_ext
¶ Displays the extension of images to crop
- Type
tk.Label
-
label_ext_label
¶ Displays the label for the extension
- Type
tk.Label
-
static
callback_about
() → None[source]¶ Display the project’s about text as stored in :file:about.txt
See
display_block()
for the details of how the text is displayed.- Returns
None
-
callback_crop
() → None[source]¶ Trigger the cropping of all images
Checks if a region is selected, then triggers
BatchCropper.crop_all_files()
.- Returns
None
-
static
callback_license
() → None[source]¶ Display the project’s license as stored in :file:LICENSE.txt
See
display_block()
for the details of how the text is displayed.- Returns
None
-
callback_load_coors
() → None[source]¶ Load coordinates for selected region from INI file
The user is shown a dialog to choose the file from which coordinates are loaded. The INI file should be created using the
BatchCropper.callback_save_coors()
method. The coordinates specified in the file are used to create a region that is stored and displayed as if the user had selected it.Error dialogs are displayed if no image is loaded or if the configuration file cannot be parsed.
The configuration file is read with
get_ratios_from_file()
, which yields abox_ratio
(see Description of Units) that is then loaded usingBatchCropper.set_coors_ratios()
.- Returns
None
-
callback_load_image
() → None[source]¶ Load an image of the user’s choice
Meant to be triggered by tkinter when user selects a button. The user is allowed to choose an image, which then is displayed. All images of the same extension and in the same directory, including the displayed image, have their paths stored in
to_crop
. The instruction text is updated to tell the user to select a region.- Returns
None
-
callback_mouse_down
(event) → None[source]¶ Start drawing out a rectangle
The rectangle is started using
BatchCropper.replace_rect()
.This callback is meant to be bound using Tkinter to the mouse move event. Tkinter will then pass the needed
event
parameter as it calls this method whenever the mouse moves.- Parameters
event – The event from Tkinter that has attributes
.x
and.y
that hold the coordinates of the cursor when mouse released- Returns
None
-
callback_mouse_move
(event) → None[source]¶ Expand the displayed selected region to follow the cursor
This allows the user to drag out the rectangle. The rectangle is only changed if the mouse button is down (checked by member variables
end_x
andend_y
beingNone
if button down).This callback is meant to be bound using Tkinter to the mouse move event. Tkinter will then pass the needed
event
parameter as it calls this method whenever the mouse moves.- Parameters
event – The event from Tkinter that has attributes
.x
and.y
that hold the coordinates of the cursor when mouse released- Returns
None
-
callback_mouse_up
(event) → None[source]¶ Save event coordinates as end coordinates and update instructions
Store the x and y coordinates of
event
to theend_x
andend_y
instance variables. This callback is meant to be bound using Tkinter to the mouse button up event. Tkinter will then pass the neededevent
parameter as it calls this method whenever the mouse button is released.- Parameters
event – The event from Tkinter that has attributes
.x
and.y
that hold the coordinates of the cursor when mouse released- Returns
None
-
callback_save_coors
() → None[source]¶ Save coordinates of the currently selected region to a file
The user is shown a dialog to select where to save the generated INI file. This coordinates can be later loaded using
BatchCropper.callback_load_coors()
.The coordinates are actually saved as a
box_ratio
, which is generated byBatchCropper.get_coors_ratios()
. The file is created and saved bysave_ratios_to_file()
.Error dialogs are displayed if no image is loaded or if no region is selected.
- Returns
None
-
crop_all_files
() → None[source]¶ Crop all files at the paths in
to_crop
Validates that the user has selected a region.
No validation is performed on
to_crop
. The user is asked to confirm, skip, or abort before any file is over-written.Each file is cropped using
crop_file()
- Returns
None
-
display_image
(image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/batch-crop/envs/latest/lib/python3.7/site-packages/PIL/Image.py'>) → PIL.ImageTk.PhotoImage[source]¶ Create and display an image for tkinter from a provided image
- Parameters
image – The image to process and display
- Returns
Image that was loaded onto the tkinter canvas
-
get_coors_ratios
() → Tuple[float, float, float, float][source]¶ Get ratios that represent the coordinates of the current region
For definitions of coordinates and ratios, see Description of Units
- Returns
A box_ratio that represents the current region
-
replace_rect
(x: float, y: float) → None[source]¶ Create a new rectangle
The created rectangle will have identical start and end coordinates.
- Parameters
x – x-coordinate for both start and end corners of rectangle
y – y-coordinate for both start and end corners of rectangle
- Returns
None
-
resize_rect
(x1: float, y1: float, x2: float, y2: float) → None[source]¶ Change coordinates of region selection rectangle
The region selection rectangle is the red rectangle that represents the region to be cropped
- Parameters
x1 – x-coordinate of first corner
y1 – y-coordinate of first corner
x2 – x-coordinate of opposite corner
y2 – y-coordinate of opposite corner
- Returns
None
-
set_coors_ratios
(box_ratio: Tuple[float, float, float, float]) → None[source]¶ Set the coordinates of the selected region from ratios
For definitions of coordinates and ratios, see Description of Units
This method accepts ratios and uses them to set the selection region to the proper displayed coordinates as if the user had selected the region.
- Parameters
box_ratio – The
box_ratio
to use- Returns
None
-
-
batch_crop.batch_crop.
coor_to_box
(coors: Tuple[float, float, float, float]) → Tuple[float, float, float, float][source]¶ Convert start and end coors into left, upper, right, and lower bounds
This is useful for converting between the Tkinter concept of start and end coordinates and the
Image.crop(...)
concept of bounds.>>> coors = (5, 3, 1, 2) >>> coor_to_box(coors) (1, 2, 5, 3)
- Parameters
coors – The
box_coor
to convert to abox
- Returns
A Tuple of bounds of the form
left_bound, upper_bound, right_bound, lower_bound
that represents the region to crop. The bounds represent values on the same coordinate system as normal.
-
batch_crop.batch_crop.
coors_to_ratios
(image_size: Tuple[float, float], coors: Tuple[float, float, float, float]) → Tuple[float, float, float, float][source]¶ Convert a
box_coor
to abox_ratio
>>> image_size = 10, 100 >>> coors = 1, 2, 5, 4 >>> coors_to_ratios(image_size, coors) (0.1, 0.02, 0.5, 0.04)
- Parameters
image_size – The size of the image that is the context for
coors
coors –
box_coor
to convert
- Returns
The
box_ratio
-
batch_crop.batch_crop.
crop_file
(box_ratio: Tuple[float, float, float, float], in_path: str, out_path: str) → None[source]¶ Save a copy of an image cropped to a specified region
Crops the image at
in_path
to the same relative region as the user selected on the template image. For example, if the selected region takes up the middle ninth (in a 3x3 grid of equivalent rectangles) of the image, the crop will be the middle ninth of the image atpath
, even if the two images have different dimensions.No validation is performed on
in_path
.The cropped image is formatted as a JPEG and saved to
out_path
. Any existing file atout_path
may be overwritten.The cropped image is created using
crop_image()
.- Parameters
box_ratio – A
box_ratio
(See Description of Units) that describes the region to cropin_path – The path of the image to crop
out_path – The path of the file to save the cropped image to
- Returns
True
if cropping should continue,False
otherwise.
-
batch_crop.batch_crop.
crop_image
(box_ratio: Tuple[float, float, float, float], image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/batch-crop/envs/latest/lib/python3.7/site-packages/PIL/Image.py'>)[source]¶ Generate a copy of an image cropped to a specified region
- Parameters
box_ratio – A
box_ratio
(See Description of Units) that defines the region to cropimage – The image to crop
- Returns
The cropped image
-
batch_crop.batch_crop.
display_block
(title: str, content: str) → None[source]¶ Display a block of text in a new window
The window is of fixed size height=30 and width=100 and has a scrollbar for the text.
- Parameters
title – Title of displayed window
content – Text to display in window
- Returns
None
-
batch_crop.batch_crop.
gen_ratios_config
(box_ratio: Tuple[float, float, float, float]) → configparser.ConfigParser[source]¶ Create the configuration that stores the provided box
The configuration is stored under the section
crop-coordinates
in the following format INI, givenbox_ratio = (x1, y1, x2, y2)
:[crop-coordinates] start_x = {x1} start_y = {y2} end_x = {x2} end_y = {y2}
substituting
{...}
for the value of the variable in braces.- Parameters
box_ratio – The box_ratio to generate a configuration for
- Returns
The configuration
-
batch_crop.batch_crop.
get_ratios_from_config
(config: configparser.ConfigParser) → Tuple[float, float, float, float][source]¶ Get a
box_ratio
from a configuration- Parameters
config – INI configuration describing the
box_ratio
to read- Returns
box_ratio
described by the configuration
-
batch_crop.batch_crop.
get_ratios_from_file
(path: str) → Tuple[float, float, float, float][source]¶ Get a
box_ratio
from a configuration fileThe configuration file should have been generated by
save_ratios_to_file()
. The configuration in the file is converted to abox_ratio
byget_ratios_from_config()
.- Parameters
path – Path to configuration INI file
- Returns
box_ratio
that was described by the file
-
batch_crop.batch_crop.
get_scale_factor
(max_dimen: float, image_raw: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/batch-crop/envs/latest/lib/python3.7/site-packages/PIL/Image.py'>) → float[source]¶ Get the factor by which to scale
image_raw
based onmax_dimen
- Parameters
max_dimen – What to scale the largest dimension of the image to
image_raw – The image to scale
- Returns
The scaled image
-
batch_crop.batch_crop.
open_image
(path: str) → <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/batch-crop/envs/latest/lib/python3.7/site-packages/PIL/Image.py'>[source]¶ Attempt to open an image, using a method appropriate for the format
Supported image types: RAW / ARW and those supported by Pillow. Errors are not handled. Format is determined by file extension.
- Parameters
path – Path to the image. Must correctly point to a supported image type.
- Returns
A Pillow Image object loaded from
path
-
batch_crop.batch_crop.
open_raw_image
(path: str) → <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/batch-crop/envs/latest/lib/python3.7/site-packages/PIL/Image.py'>[source]¶ Open RAW-formatted image using
rawpy
No format checking or error handling is performed.
- Parameters
path – Path to the image. Must be correct.
- Returns
A Pillow Image object representing the image at
path
-
batch_crop.batch_crop.
ratios_to_coors
(image_size: Tuple[float, float], ratios: Tuple[float, float, float, float]) → Tuple[float, float, float, float][source]¶ Convert a
box_ratio
to abox_coor
- Parameters
image_size – Size of image that is the context for
box_coor
ratios – The
box_ratio
to convert
- Returns
The
box_coor
-
batch_crop.batch_crop.
save_ratios_to_file
(box_ratio: Tuple[float, float, float, float], path: str) → None[source]¶ Save the configuration for the
box_ratio
to the specified INI fileThe configuration is generated by
gen_coors_config()
.- Parameters
box_ratio – The
box_ratio
to store in the filepath – The path to the INI file to store the configuration in. The file should be empty.
- Returns
None
-
batch_crop.batch_crop.
scale_image
(scale_factor: float, image_raw: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/batch-crop/envs/latest/lib/python3.7/site-packages/PIL/Image.py'>) → <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/batch-crop/envs/latest/lib/python3.7/site-packages/PIL/Image.py'>[source]¶ Scale the provided image to fit within a 500x500 box
The image’s shape is not changed, the largest dimension is just forced to be 500. The factor by which the image is scaled is stored as the
scale_factor
attribute.- Parameters
scale_factor – The factor by which the image’s width and height are re-sized
image_raw – The image to re-size
- Returns
The re-sized image