This Python script consumes a list of input directories of PNG files and crops them.
The user must specify a pivot for each group of directories. This pivot location is guaranteed to be
Preserved during the crop. For example, (0.5, 0.5)
ensures that the pivot is centered on the image.
Use config.py to edit the input config.
Email: joshikatsu@gmail.com
Install the package using pip install pivotcrop
.
Example usage:
from pivotcrop import PivotCropper
PivotCropper(
output_dir="output",
root_dir="testdata",
pivot_groups={
# All directories with images using pivot (0.5, 1) should be configured here.
(0.5, 1): [
# Two directories that should share the same bounding box.
BBoxGroup("Images/A", "Images/B"),
# Another directory using same pivot, but a different bounding box.
BBoxGroup("Images/C")
],
# Entry with a different pivot
(0.5, 0.5): [
BBoxGroup("Images/D"),
# A directory where each image gets its own bounding box.
IndependentDir("Images/E")
],
}
).crop()
The config entries are meant to be relative to the current working directory. The directory structure is preserved in the output.
Pivot x y is [0, 1], with [0, 0] at the top left.
We simply loop over all directories and keep track of minimum and maximum points for the bounding box. The main complexity of this script comes from ensuring that the cropped image still has the same relative pivot as the original.
Consider an animation where all images can be encompassed by a bounding box
One way to describe this constraint is to say that on each axis, the lower bound must change proportionally to the upper bound. The ratio of this proprotion is determined by the corresponding component of the pivot.
If we define the change to our upper and lower bounds on the
To retain the same pivot point when cropping, we must make sure that the following ratio holds:
For simplicity, let
If we need to reduce the minimum to maintain the ratio (
If we need to increase the maximum to maintain the ratio
Same deal for the