Skip to content

Commit f4510f6

Browse files
authored
Crop function now subtracts pixels instead of requiring specific window measurements
- Revamped crop functionality - Merge pull request #43 from Daethyra/3.0.0
2 parents 3cff0ec + d67517c commit f4510f6

4 files changed

Lines changed: 23 additions & 5 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Create a GIF from images or video:
6464
|------------------|--------|---------|--------------------------------------------------------------|
6565
| `--fps` | float | 24 | Frames per second |
6666
| `--size` | int int| - | Max width and height (aspect ratio preserved) |
67-
| `--crop` | int int int int | - | Crop box: left top right bottom |
67+
| `--crop` | int int int int | - | Pixels to cut from each edge: left top right bottom. e.g. 0 20 0 20 trims 20px from top & bottom |
6868
| `--loop` | int | 0 | Loop count (0 = infinite) |
6969
| `--start-time` | float | - | Start time in seconds (video only) |
7070
| `--end-time` | float | - | End time in seconds (video only) |
@@ -109,6 +109,10 @@ Reduce palette to 64 colors and turn off dithering for a crisp, retro look
109109

110110
poetry run python -m transimage gif ./frames -o movie.gif --fps 12 --colors 64 --no-dither
111111

112+
Remove 20px from top and bottom, resize to fit 640x480
113+
114+
poetry run python -m transimage gif ./frames -o movie.gif --fps 12 --size 640 480 --crop 0 20 0 20
115+
112116
## Programmatic Usage
113117

114118
Inside a script or interactive session, you can use the package directly after installing it in your environment.
@@ -184,6 +188,9 @@ Please submit issues regarding any oversight you see. Pull requests for improvem
184188

185189
## Changelog
186190

191+
### 3.0.0 (25-06-2026)
192+
- Revamped `--crop` to be more intuitive. Crop now takes the number of pixels to cut off, instead of requiring the user to provide the exact measurements of
193+
187194
### 2.1.0 (24-06-2026)
188195
- Added `--colors` and `--no-dither` arguments to give the user more control over a GIF's output file size.
189196

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "transimage"
3-
version = "2.2.0"
3+
version = "3.0.0"
44
description = "CLI tool for converting images between different formats and creating GIFs from image sequences, directories, or video files. Supports JPG, PNG, BMP, and WebP."
55
authors = ["Daethyra <109057945+Daethyra@users.noreply.github.com>"]
66
license = "MIT"

src/transimage/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def main() -> None:
7474
)
7575
gif_parser.add_argument(
7676
"--crop", nargs=4, type=int, metavar=("L", "T", "R", "B"),
77-
help="Crop box: left top right bottom"
77+
help="Pixels to cut from each side: left top right bottom (e.g. --crop 0 20 0 20 removes 20px from top and bottom)"
7878
)
7979
gif_parser.add_argument("--loop", type=int, default=0, help="Loop count (0 = infinite)")
8080
gif_parser.add_argument("--start-time", type=float, default=None, help="Start time in seconds (video only)")

src/transimage/gif_creator.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,20 @@ def _process_frames(self, images: List[Image.Image]) -> List[Image.Image]:
9393
if self.size:
9494
img = img.copy()
9595
img.thumbnail(self.size, self.resize_method)
96-
# Crop if a box is given
96+
# crop (cut amounts from each edge)
9797
if self.crop:
98-
img = img.crop(self.crop)
98+
left_cut, top_cut, right_cut, bottom_cut = self.crop
99+
w, h = img.size
100+
left = left_cut
101+
top = top_cut
102+
right = w - right_cut
103+
bottom = h - bottom_cut
104+
# Clamp to valid range (prevent zero/negative dimensions)
105+
left = max(0, min(left, w))
106+
top = max(0, min(top, h))
107+
right = max(left, min(right, w))
108+
bottom = max(top, min(bottom, h))
109+
img = img.crop((left, top, right, bottom))
99110
# Ensure image is in RGB mode (GIF doesn't store alpha like PNG)
100111
if img.mode in ("RGBA", "LA", "P"):
101112
background = Image.new("RGB", img.size, (255, 255, 255))

0 commit comments

Comments
 (0)