imgbytesizer 0.2.3__tar.gz → 0.2.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {imgbytesizer-0.2.3 → imgbytesizer-0.2.4}/PKG-INFO +25 -12
- {imgbytesizer-0.2.3 → imgbytesizer-0.2.4}/README.md +9 -4
- imgbytesizer-0.2.4/pyproject.toml +40 -0
- {imgbytesizer-0.2.3 → imgbytesizer-0.2.4/src}/imgbytesizer/__init__.py +1 -1
- {imgbytesizer-0.2.3 → imgbytesizer-0.2.4/src}/imgbytesizer/logger.py +1 -1
- {imgbytesizer-0.2.3 → imgbytesizer-0.2.4/src}/imgbytesizer/main.py +2 -2
- imgbytesizer-0.2.4/src/imgbytesizer/py.typed +1 -0
- {imgbytesizer-0.2.3 → imgbytesizer-0.2.4/src}/imgbytesizer/resizer.py +4 -4
- imgbytesizer-0.2.3/imgbytesizer.egg-info/PKG-INFO +0 -191
- imgbytesizer-0.2.3/imgbytesizer.egg-info/SOURCES.txt +0 -20
- imgbytesizer-0.2.3/imgbytesizer.egg-info/dependency_links.txt +0 -1
- imgbytesizer-0.2.3/imgbytesizer.egg-info/entry_points.txt +0 -2
- imgbytesizer-0.2.3/imgbytesizer.egg-info/requires.txt +0 -2
- imgbytesizer-0.2.3/imgbytesizer.egg-info/top_level.txt +0 -1
- imgbytesizer-0.2.3/pyproject.toml +0 -16
- imgbytesizer-0.2.3/setup.cfg +0 -4
- imgbytesizer-0.2.3/tests/test_formatter.py +0 -12
- imgbytesizer-0.2.3/tests/test_logger.py +0 -48
- imgbytesizer-0.2.3/tests/test_main.py +0 -128
- imgbytesizer-0.2.3/tests/test_resizer.py +0 -291
- imgbytesizer-0.2.3/tests/test_utils.py +0 -99
- {imgbytesizer-0.2.3 → imgbytesizer-0.2.4}/LICENSE +0 -0
- {imgbytesizer-0.2.3 → imgbytesizer-0.2.4/src}/imgbytesizer/formatter.py +0 -0
- {imgbytesizer-0.2.3 → imgbytesizer-0.2.4/src}/imgbytesizer/utils.py +0 -0
|
@@ -1,16 +1,23 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
2
|
Name: imgbytesizer
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.4
|
|
4
4
|
Summary: A CLI tool to resize images to a target file size.
|
|
5
|
-
|
|
6
|
-
License: MIT License
|
|
5
|
+
License: MIT
|
|
7
6
|
Keywords: image,resize,compression,cli,optimize
|
|
7
|
+
Author: pyyupsk
|
|
8
|
+
Author-email: contact@fasu.dev
|
|
8
9
|
Requires-Python: >=3.9
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Requires-Dist: pillow (>=11.2.1,<12.0.0)
|
|
18
|
+
Requires-Dist: pyfiglet (>=1.0.2,<2.0.0)
|
|
19
|
+
Requires-Dist: tabulate (>=0.9.0,<0.10.0)
|
|
9
20
|
Description-Content-Type: text/markdown
|
|
10
|
-
License-File: LICENSE
|
|
11
|
-
Requires-Dist: Pillow>=11.0.0
|
|
12
|
-
Requires-Dist: tabulate>=0.9.0
|
|
13
|
-
Dynamic: license-file
|
|
14
21
|
|
|
15
22
|
# ImgByteSizer
|
|
16
23
|
|
|
@@ -177,10 +184,15 @@ imgbytesizer profile_pic.jpg 400KB --min-dimension 400
|
|
|
177
184
|
|
|
178
185
|
## Requirements
|
|
179
186
|
|
|
180
|
-
|
|
181
|
-
-
|
|
182
|
-
|
|
183
|
-
|
|
187
|
+
```toml
|
|
188
|
+
requires-python = ">=3.9"
|
|
189
|
+
|
|
190
|
+
dependencies = [
|
|
191
|
+
"pillow (>=11.2.1,<12.0.0)",
|
|
192
|
+
"pyfiglet (>=1.0.2,<2.0.0)",
|
|
193
|
+
"tabulate (>=0.9.0,<0.10.0)",
|
|
194
|
+
]
|
|
195
|
+
```
|
|
184
196
|
|
|
185
197
|
## Contributing
|
|
186
198
|
|
|
@@ -189,3 +201,4 @@ Contributions are welcome! Feel free to submit issues or pull requests.
|
|
|
189
201
|
## License
|
|
190
202
|
|
|
191
203
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
204
|
+
|
|
@@ -163,10 +163,15 @@ imgbytesizer profile_pic.jpg 400KB --min-dimension 400
|
|
|
163
163
|
|
|
164
164
|
## Requirements
|
|
165
165
|
|
|
166
|
-
|
|
167
|
-
-
|
|
168
|
-
|
|
169
|
-
|
|
166
|
+
```toml
|
|
167
|
+
requires-python = ">=3.9"
|
|
168
|
+
|
|
169
|
+
dependencies = [
|
|
170
|
+
"pillow (>=11.2.1,<12.0.0)",
|
|
171
|
+
"pyfiglet (>=1.0.2,<2.0.0)",
|
|
172
|
+
"tabulate (>=0.9.0,<0.10.0)",
|
|
173
|
+
]
|
|
174
|
+
```
|
|
170
175
|
|
|
171
176
|
## Contributing
|
|
172
177
|
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "imgbytesizer"
|
|
3
|
+
version = "0.2.4"
|
|
4
|
+
description = "A CLI tool to resize images to a target file size."
|
|
5
|
+
keywords = ["image", "resize", "compression", "cli", "optimize"]
|
|
6
|
+
|
|
7
|
+
authors = [{ name = "pyyupsk", email = "contact@fasu.dev" }]
|
|
8
|
+
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT License" }
|
|
11
|
+
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
|
|
14
|
+
dependencies = [
|
|
15
|
+
"pillow (>=11.2.1,<12.0.0)",
|
|
16
|
+
"pyfiglet (>=1.0.2,<2.0.0)",
|
|
17
|
+
"tabulate (>=0.9.0,<0.10.0)",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.scripts]
|
|
21
|
+
imgbytesizer = "imgbytesizer.main:main"
|
|
22
|
+
|
|
23
|
+
[tool.poetry]
|
|
24
|
+
packages = [{ include = "imgbytesizer", from = "src" }]
|
|
25
|
+
|
|
26
|
+
[tool.poetry.group.dev.dependencies]
|
|
27
|
+
flake8 = "^7.2.0"
|
|
28
|
+
pytest = "^8.3.5"
|
|
29
|
+
pytest-cov = "^6.1.1"
|
|
30
|
+
requests = "^2.32.3"
|
|
31
|
+
types-requests = "^2.32.0.20250515"
|
|
32
|
+
tomli = "^2.2.1"
|
|
33
|
+
types-tabulate = "^0.9.0.20241207"
|
|
34
|
+
vulture = "^2.14"
|
|
35
|
+
yapf = "^0.43.0"
|
|
36
|
+
tox = "^4.26.0"
|
|
37
|
+
|
|
38
|
+
[build-system]
|
|
39
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
40
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -13,7 +13,7 @@ from .logger import setup_logger
|
|
|
13
13
|
from .resizer import resize_to_target_filesize
|
|
14
14
|
from .utils import IMG_FORMATS, parse_filesize
|
|
15
15
|
|
|
16
|
-
f: Figlet = Figlet(font=
|
|
16
|
+
f: Figlet = Figlet(font="slant")
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
def main() -> int:
|
|
@@ -21,7 +21,7 @@ def main() -> int:
|
|
|
21
21
|
# Setup logger
|
|
22
22
|
logger: logging.Logger = setup_logger()
|
|
23
23
|
|
|
24
|
-
print(f.renderText(
|
|
24
|
+
print(f.renderText("ImgByteSizer"))
|
|
25
25
|
|
|
26
26
|
parser: argparse.ArgumentParser = argparse.ArgumentParser(
|
|
27
27
|
description="Resize an image to match a target file size",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -256,8 +256,8 @@ def _try_combined_approach(
|
|
|
256
256
|
# Apply minimum dimension constraint if specified
|
|
257
257
|
if min_dimension is not None:
|
|
258
258
|
if new_width < min_dimension or new_height < min_dimension:
|
|
259
|
-
scale_w: float = min_dimension / new_width if new_width < min_dimension else 1
|
|
260
|
-
scale_h: float = min_dimension / new_height if new_height < min_dimension else 1
|
|
259
|
+
scale_w: float = (min_dimension / new_width if new_width < min_dimension else 1)
|
|
260
|
+
scale_h: float = (min_dimension / new_height if new_height < min_dimension else 1)
|
|
261
261
|
scale = max(scale_w, scale_h)
|
|
262
262
|
new_width = int(orig_width * scale)
|
|
263
263
|
new_height = int(orig_height * scale)
|
|
@@ -371,8 +371,8 @@ def _try_resizing(
|
|
|
371
371
|
# Apply minimum dimension constraint if specified
|
|
372
372
|
if min_dimension is not None:
|
|
373
373
|
if new_width < min_dimension or new_height < min_dimension:
|
|
374
|
-
scale_w: float = min_dimension / new_width if new_width < min_dimension else 1
|
|
375
|
-
scale_h: float = min_dimension / new_height if new_height < min_dimension else 1
|
|
374
|
+
scale_w: float = (min_dimension / new_width if new_width < min_dimension else 1)
|
|
375
|
+
scale_h: float = (min_dimension / new_height if new_height < min_dimension else 1)
|
|
376
376
|
scale = max(scale_w, scale_h)
|
|
377
377
|
new_width = max(min_dimension, int(new_width * scale))
|
|
378
378
|
new_height = max(min_dimension, int(new_height * scale))
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: imgbytesizer
|
|
3
|
-
Version: 0.2.3
|
|
4
|
-
Summary: A CLI tool to resize images to a target file size.
|
|
5
|
-
Author-email: pyyupsk <contact@fasu.dev>
|
|
6
|
-
License: MIT License
|
|
7
|
-
Keywords: image,resize,compression,cli,optimize
|
|
8
|
-
Requires-Python: >=3.9
|
|
9
|
-
Description-Content-Type: text/markdown
|
|
10
|
-
License-File: LICENSE
|
|
11
|
-
Requires-Dist: Pillow>=11.0.0
|
|
12
|
-
Requires-Dist: tabulate>=0.9.0
|
|
13
|
-
Dynamic: license-file
|
|
14
|
-
|
|
15
|
-
# ImgByteSizer
|
|
16
|
-
|
|
17
|
-
[](https://pypi.org/project/imgbytesizer/)
|
|
18
|
-
[](https://pepy.tech/projects/imgbytesizer)
|
|
19
|
-
[](https://codecov.io/gh/pyyupsk/imgbytesizer)
|
|
20
|
-
|
|
21
|
-
ImgByteSizer is a command-line tool that precisely resizes and optimizes images to match a specific file size while maintaining the best possible quality.
|
|
22
|
-
|
|
23
|
-
## Features
|
|
24
|
-
|
|
25
|
-
- 🎯 Resize images to an exact target file size
|
|
26
|
-
- 🖼️ Maintains aspect ratio during resizing
|
|
27
|
-
- 🔍 Intelligent quality optimization with binary search
|
|
28
|
-
- 🔄 Format conversion (JPEG, PNG, WebP)
|
|
29
|
-
- 📏 Minimum dimension constraints
|
|
30
|
-
- 🎨 Beautiful terminal output with progress indicators
|
|
31
|
-
- 📊 Detailed comparison of original vs. processed images
|
|
32
|
-
|
|
33
|
-
## Installation
|
|
34
|
-
|
|
35
|
-
```bash
|
|
36
|
-
pip install imgbytesizer
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
## Usage
|
|
40
|
-
|
|
41
|
-
```bash
|
|
42
|
-
imgbytesizer image.jpg 500KB
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
### Basic Examples
|
|
46
|
-
|
|
47
|
-
```bash
|
|
48
|
-
# Resize an image to 500KB
|
|
49
|
-
imgbytesizer large_photo.jpg 500KB
|
|
50
|
-
|
|
51
|
-
# Resize and convert to WebP format
|
|
52
|
-
imgbytesizer image.png 1MB -f webp
|
|
53
|
-
|
|
54
|
-
# Specify output file path
|
|
55
|
-
imgbytesizer photo.jpg 1MB -o compressed_photo.jpg
|
|
56
|
-
|
|
57
|
-
# Ensure minimum dimension is at least 400px
|
|
58
|
-
imgbytesizer large_image.jpg 300KB --min-dimension 400
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
### Command-Line Options
|
|
62
|
-
|
|
63
|
-
```bash
|
|
64
|
-
usage: imgbytesizer [-h] [-o OUTPUT] [-f {jpg,jpeg,png,webp}] [--min-dimension MIN_DIMENSION] [--no-exact] [--debug] [-q] [-v] [image_path] [target_size]
|
|
65
|
-
|
|
66
|
-
Resize an image to match a target file size
|
|
67
|
-
|
|
68
|
-
positional arguments:
|
|
69
|
-
image_path Path to the input image
|
|
70
|
-
target_size Target file size (e.g., "1MB", "500KB")
|
|
71
|
-
|
|
72
|
-
options:
|
|
73
|
-
-h, --help show this help message and exit
|
|
74
|
-
-v, --version Show version information
|
|
75
|
-
|
|
76
|
-
primary options:
|
|
77
|
-
-o, --output OUTPUT Output path (default: input_resized.ext)
|
|
78
|
-
-f, --format {jpg,jpeg,png,webp}
|
|
79
|
-
Output format
|
|
80
|
-
|
|
81
|
-
advanced options:
|
|
82
|
-
--min-dimension MIN_DIMENSION
|
|
83
|
-
Minimum width/height in pixels
|
|
84
|
-
--no-exact Do not pad file to get exact target size
|
|
85
|
-
|
|
86
|
-
utility:
|
|
87
|
-
--debug Enable debug logging
|
|
88
|
-
-q, --quiet Minimal output
|
|
89
|
-
|
|
90
|
-
examples:
|
|
91
|
-
imgbytesizer image.jpg 500KB # Resize to 500 KB
|
|
92
|
-
imgbytesizer photo.png 2MB -o small_photo.png # Resize to 2 MB with custom output
|
|
93
|
-
imgbytesizer image.jpg 100KB -f webp # Resize and convert to WebP
|
|
94
|
-
imgbytesizer large.jpg 50KB --min-dimension 200 # Ensure min dimension is 200px
|
|
95
|
-
imgbytesizer -v # Show version information
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
## How It Works
|
|
99
|
-
|
|
100
|
-
ImgByteSizer uses binary search algorithms to efficiently find the optimal combination of image dimensions and compression quality to match your target file size:
|
|
101
|
-
|
|
102
|
-
1. First attempts to adjust quality without resizing (for formats that support quality settings)
|
|
103
|
-
2. If quality adjustment alone isn't sufficient, performs binary search for optimal dimensions
|
|
104
|
-
3. Fine-tunes with an additional quality optimization pass
|
|
105
|
-
4. Adds minimal padding if necessary to hit the exact target size
|
|
106
|
-
|
|
107
|
-
The tool prioritizes maintaining the highest possible quality while meeting the target size constraint.
|
|
108
|
-
|
|
109
|
-
## Example Output
|
|
110
|
-
|
|
111
|
-
```
|
|
112
|
-
❯ imgbytesizer image.png 1MB -f webp
|
|
113
|
-
|
|
114
|
-
◐ Opening image.png
|
|
115
|
-
File: image.png
|
|
116
|
-
Format: WEBP
|
|
117
|
-
Dimensions: 1024 × 1024 pixels
|
|
118
|
-
Size: 1.95 MB
|
|
119
|
-
Target size: 1.00 MB
|
|
120
|
-
Trying quality adjustment without resizing...
|
|
121
|
-
Testing quality 100 |█████████████████░░░░░░░░░░░░░| 58% Size: 476.46 KB
|
|
122
|
-
✓ Found optimal quality: 100 (size: 476.46 KB)
|
|
123
|
-
Trying combined scaling and quality approach...
|
|
124
|
-
Trying scale factor 1.00 (1024×1024)...
|
|
125
|
-
Quality 100 |██████████████████████████████| 100% Size: 476.46 KB
|
|
126
|
-
Trying scale factor 1.25 (1280×1280)...
|
|
127
|
-
Quality 100 |██████████████████████████████| 100% Size: 680.51 KB
|
|
128
|
-
Trying scale factor 1.50 (1536×1536)...
|
|
129
|
-
Quality 100 |██████████████████████████████| 100% Size: 903.38 KB
|
|
130
|
-
Trying scale factor 2.00 (2048×2048)...
|
|
131
|
-
Quality 96 |██████████████████████████████| 100% Size: 1011.68 KB
|
|
132
|
-
|
|
133
|
-
✓ Combined approach success: scale=2.00, size=1011.68 KB
|
|
134
|
-
Adjusting to exact target size...
|
|
135
|
-
✓ Adjusted to exact size: 1.00 MB
|
|
136
|
-
|
|
137
|
-
╭─────────────┬────────────┬───────────────╮
|
|
138
|
-
│ Metric │ Original │ Processed │
|
|
139
|
-
├─────────────┼────────────┼───────────────┤
|
|
140
|
-
│ Dimensions │ 1024×1024 │ 2048×2048 │
|
|
141
|
-
├─────────────┼────────────┼───────────────┤
|
|
142
|
-
│ Size │ 1.95 MB │ 1.00 MB │
|
|
143
|
-
├─────────────┼────────────┼───────────────┤
|
|
144
|
-
│ Target Size │ │ 1.00 MB │
|
|
145
|
-
├─────────────┼────────────┼───────────────┤
|
|
146
|
-
│ Difference │ │ 0 B (0.0%) │
|
|
147
|
-
├─────────────┼────────────┼───────────────┤
|
|
148
|
-
│ Reduction │ │ 48.8% smaller │
|
|
149
|
-
╰─────────────┴────────────┴───────────────╯
|
|
150
|
-
Time taken: 28.12 seconds
|
|
151
|
-
Output file: image_resized.webp
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
## Real Use Cases
|
|
155
|
-
|
|
156
|
-
### Web Development
|
|
157
|
-
|
|
158
|
-
```bash
|
|
159
|
-
# Create images that won't exceed page weight budget
|
|
160
|
-
imgbytesizer hero.jpg 200KB --min-dimension 1200
|
|
161
|
-
imgbytesizer background.png 100KB -f webp
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### Email Attachments
|
|
165
|
-
|
|
166
|
-
```bash
|
|
167
|
-
# Shrink images to fit email attachment limits
|
|
168
|
-
imgbytesizer family_photo.jpg 5MB
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
### Social Media Uploads
|
|
172
|
-
|
|
173
|
-
```bash
|
|
174
|
-
# Optimize images for social media platforms with size limits
|
|
175
|
-
imgbytesizer profile_pic.jpg 400KB --min-dimension 400
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
## Requirements
|
|
179
|
-
|
|
180
|
-
- Python 3.9+
|
|
181
|
-
- Dependencies:
|
|
182
|
-
- Pillow >= 11.0.0
|
|
183
|
-
- tabulate >= 0.9.0
|
|
184
|
-
|
|
185
|
-
## Contributing
|
|
186
|
-
|
|
187
|
-
Contributions are welcome! Feel free to submit issues or pull requests.
|
|
188
|
-
|
|
189
|
-
## License
|
|
190
|
-
|
|
191
|
-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
LICENSE
|
|
2
|
-
README.md
|
|
3
|
-
pyproject.toml
|
|
4
|
-
imgbytesizer/__init__.py
|
|
5
|
-
imgbytesizer/formatter.py
|
|
6
|
-
imgbytesizer/logger.py
|
|
7
|
-
imgbytesizer/main.py
|
|
8
|
-
imgbytesizer/resizer.py
|
|
9
|
-
imgbytesizer/utils.py
|
|
10
|
-
imgbytesizer.egg-info/PKG-INFO
|
|
11
|
-
imgbytesizer.egg-info/SOURCES.txt
|
|
12
|
-
imgbytesizer.egg-info/dependency_links.txt
|
|
13
|
-
imgbytesizer.egg-info/entry_points.txt
|
|
14
|
-
imgbytesizer.egg-info/requires.txt
|
|
15
|
-
imgbytesizer.egg-info/top_level.txt
|
|
16
|
-
tests/test_formatter.py
|
|
17
|
-
tests/test_logger.py
|
|
18
|
-
tests/test_main.py
|
|
19
|
-
tests/test_resizer.py
|
|
20
|
-
tests/test_utils.py
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
imgbytesizer
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
[project]
|
|
2
|
-
authors = [{ name = "pyyupsk", email = "contact@fasu.dev" }]
|
|
3
|
-
description = "A CLI tool to resize images to a target file size."
|
|
4
|
-
dependencies = ["Pillow>=11.0.0", "tabulate>=0.9.0"]
|
|
5
|
-
keywords = ["image", "resize", "compression", "cli", "optimize"]
|
|
6
|
-
license = { text = "MIT License" }
|
|
7
|
-
name = "imgbytesizer"
|
|
8
|
-
readme = "README.md"
|
|
9
|
-
requires-python = ">=3.9"
|
|
10
|
-
version = "0.2.3"
|
|
11
|
-
|
|
12
|
-
[project.scripts]
|
|
13
|
-
imgbytesizer = "imgbytesizer.main:main"
|
|
14
|
-
|
|
15
|
-
[tool.setuptools]
|
|
16
|
-
packages = ["imgbytesizer"]
|
imgbytesizer-0.2.3/setup.cfg
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
from imgbytesizer.formatter import format_filesize
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
def test_format_filesize():
|
|
5
|
-
# Test various file sizes
|
|
6
|
-
assert format_filesize(500) == "500.0B"
|
|
7
|
-
assert format_filesize(1024) == "1.0KB"
|
|
8
|
-
assert format_filesize(1024 * 1024) == "1.0MB"
|
|
9
|
-
assert format_filesize(1024 * 1024 * 1.5) == "1.5MB"
|
|
10
|
-
|
|
11
|
-
# Test with None
|
|
12
|
-
assert format_filesize(None) == "N/A"
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import sys
|
|
2
|
-
from io import StringIO
|
|
3
|
-
|
|
4
|
-
from imgbytesizer.logger import print_result
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def test_print_result():
|
|
8
|
-
# Capture stdout
|
|
9
|
-
captured_output = StringIO()
|
|
10
|
-
sys.stdout = captured_output
|
|
11
|
-
|
|
12
|
-
# Test basic result
|
|
13
|
-
print_result("Test", "Value")
|
|
14
|
-
output = captured_output.getvalue()
|
|
15
|
-
|
|
16
|
-
# Reset stdout
|
|
17
|
-
sys.stdout = sys.__stdout__
|
|
18
|
-
|
|
19
|
-
# Check output
|
|
20
|
-
assert output.startswith("Test")
|
|
21
|
-
assert "Value" in output
|
|
22
|
-
|
|
23
|
-
# Test with status
|
|
24
|
-
captured_output = StringIO()
|
|
25
|
-
sys.stdout = captured_output
|
|
26
|
-
print_result("Test", "Value", "good")
|
|
27
|
-
output = captured_output.getvalue()
|
|
28
|
-
sys.stdout = sys.__stdout__
|
|
29
|
-
assert output.startswith("Test")
|
|
30
|
-
assert "Value" in output
|
|
31
|
-
|
|
32
|
-
# Test with 'warning' status
|
|
33
|
-
captured_output = StringIO()
|
|
34
|
-
sys.stdout = captured_output
|
|
35
|
-
print_result("Test", "Value", "warning")
|
|
36
|
-
output = captured_output.getvalue()
|
|
37
|
-
sys.stdout = sys.__stdout__
|
|
38
|
-
assert output.startswith("Test")
|
|
39
|
-
assert "Value" in output
|
|
40
|
-
|
|
41
|
-
# Test with 'bad' status
|
|
42
|
-
captured_output = StringIO()
|
|
43
|
-
sys.stdout = captured_output
|
|
44
|
-
print_result("Test", "Value", "bad")
|
|
45
|
-
output = captured_output.getvalue()
|
|
46
|
-
sys.stdout = sys.__stdout__
|
|
47
|
-
assert output.startswith("Test")
|
|
48
|
-
assert "Value" in output
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import subprocess
|
|
2
|
-
import sys
|
|
3
|
-
from pathlib import Path
|
|
4
|
-
from unittest.mock import patch
|
|
5
|
-
|
|
6
|
-
import pytest
|
|
7
|
-
|
|
8
|
-
from imgbytesizer.main import main
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
@pytest.fixture
|
|
12
|
-
def mock_logger():
|
|
13
|
-
with patch("imgbytesizer.main.setup_logger") as mock:
|
|
14
|
-
yield mock.return_value
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
@pytest.fixture
|
|
18
|
-
def mock_resize():
|
|
19
|
-
with patch("imgbytesizer.main.resize_to_target_filesize") as mock:
|
|
20
|
-
yield mock
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
@pytest.fixture
|
|
24
|
-
def test_image(tmp_path):
|
|
25
|
-
"""Create a temporary test image."""
|
|
26
|
-
image = tmp_path / "test.jpg"
|
|
27
|
-
image.touch()
|
|
28
|
-
return image
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
def test_main_entrypoint(tmp_path):
|
|
32
|
-
script_path = Path(__file__).parent.parent / "imgbytesizer" / "main.py"
|
|
33
|
-
test_image = tmp_path / "test.jpg"
|
|
34
|
-
test_image.touch()
|
|
35
|
-
|
|
36
|
-
result = subprocess.run(
|
|
37
|
-
[sys.executable, str(script_path),
|
|
38
|
-
str(test_image), "1KB", "-q"],
|
|
39
|
-
capture_output=True,
|
|
40
|
-
text=True,
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
assert result.returncode in (0, 1)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def test_version_display(capsys):
|
|
47
|
-
"""Test version display."""
|
|
48
|
-
with patch.object(sys, "argv", ["imgbytesizer", "-v"]):
|
|
49
|
-
assert main() == 0
|
|
50
|
-
captured = capsys.readouterr()
|
|
51
|
-
assert "imgbytesizer v" in captured.out
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
def test_help_display(capsys):
|
|
55
|
-
"""Test help display."""
|
|
56
|
-
with patch.object(sys, "argv", ["imgbytesizer"]):
|
|
57
|
-
assert main() == 0
|
|
58
|
-
captured = capsys.readouterr()
|
|
59
|
-
assert "Resize an image to match a target file size" in captured.out
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
def test_missing_arguments(capsys):
|
|
63
|
-
"""Test missing required arguments."""
|
|
64
|
-
with patch.object(sys, "argv", ["imgbytesizer", "image.jpg"]):
|
|
65
|
-
assert main() == 0
|
|
66
|
-
captured = capsys.readouterr()
|
|
67
|
-
assert "Resize an image to match a target file size" in captured.out
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
def test_nonexistent_file(mock_logger):
|
|
71
|
-
"""Test handling of nonexistent input file."""
|
|
72
|
-
with patch.object(sys, "argv", ["imgbytesizer", "nonexistent.jpg", "1MB"]):
|
|
73
|
-
assert main() == 1
|
|
74
|
-
mock_logger.error.assert_called_once()
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
def test_invalid_filesize(mock_logger, test_image):
|
|
78
|
-
"""Test handling of invalid filesize."""
|
|
79
|
-
with patch.object(sys, "argv", ["imgbytesizer", str(test_image), "invalid"]):
|
|
80
|
-
assert main() == 1
|
|
81
|
-
mock_logger.error.assert_called_once()
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
def test_successful_resize(mock_logger, mock_resize, test_image):
|
|
85
|
-
"""Test successful image resize."""
|
|
86
|
-
mock_resize.return_value = Path("output.jpg")
|
|
87
|
-
|
|
88
|
-
with patch.object(sys, "argv", ["imgbytesizer", str(test_image), "1MB"]):
|
|
89
|
-
assert main() == 0
|
|
90
|
-
mock_resize.assert_called_once()
|
|
91
|
-
mock_logger.debug.assert_called_once()
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
def test_keyboard_interrupt(mock_logger, mock_resize, test_image):
|
|
95
|
-
"""Test handling of keyboard interrupt."""
|
|
96
|
-
mock_resize.side_effect = KeyboardInterrupt()
|
|
97
|
-
|
|
98
|
-
with patch.object(sys, "argv", ["imgbytesizer", str(test_image), "1MB"]):
|
|
99
|
-
assert main() == 130
|
|
100
|
-
mock_logger.warning.assert_called_once_with("Process interrupted by user.")
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
def test_general_exception(mock_logger, mock_resize, test_image):
|
|
104
|
-
"""Test handling of general exceptions."""
|
|
105
|
-
mock_resize.side_effect = Exception("Test error")
|
|
106
|
-
|
|
107
|
-
with patch.object(sys, "argv", ["imgbytesizer", str(test_image), "1MB"]):
|
|
108
|
-
assert main() == 1
|
|
109
|
-
mock_logger.error.assert_called_once_with("Error: Test error")
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
def test_debug_mode(mock_logger, mock_resize, test_image):
|
|
113
|
-
"""Test debug mode logging."""
|
|
114
|
-
mock_resize.side_effect = Exception("Test error")
|
|
115
|
-
|
|
116
|
-
with patch.object(sys, "argv", ["imgbytesizer", str(test_image), "1MB", "--debug"]):
|
|
117
|
-
assert main() == 1
|
|
118
|
-
mock_logger.setLevel.assert_called_once()
|
|
119
|
-
mock_logger.exception.assert_called_once_with("Detailed error information:")
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
def test_quiet_mode(mock_logger, mock_resize, test_image):
|
|
123
|
-
"""Test quiet mode."""
|
|
124
|
-
mock_resize.return_value = Path("output.jpg")
|
|
125
|
-
|
|
126
|
-
with patch.object(sys, "argv", ["imgbytesizer", str(test_image), "1MB", "-q"]):
|
|
127
|
-
assert main() == 0
|
|
128
|
-
mock_logger.setLevel.assert_called_once()
|
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
|
|
3
|
-
import pytest
|
|
4
|
-
from PIL import Image
|
|
5
|
-
|
|
6
|
-
from imgbytesizer.resizer import (
|
|
7
|
-
_adjust_to_exact_size, _final_quality_adjustment, _find_best_quality, _try_combined_approach,
|
|
8
|
-
_try_quality_adjustment, _try_resizing, resize_to_target_filesize
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
@pytest.fixture
|
|
13
|
-
def sample_image():
|
|
14
|
-
# Create a test image
|
|
15
|
-
img = Image.new("RGB", (800, 600), color="red")
|
|
16
|
-
img_path = "test_image.jpg"
|
|
17
|
-
img.save(img_path, "JPEG", quality=95)
|
|
18
|
-
yield img_path
|
|
19
|
-
# Cleanup
|
|
20
|
-
if os.path.exists(img_path):
|
|
21
|
-
os.remove(img_path)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
@pytest.fixture
|
|
25
|
-
def sample_png():
|
|
26
|
-
# Create a test PNG image
|
|
27
|
-
img = Image.new("RGBA", (800, 600), color=(255, 0, 0, 128))
|
|
28
|
-
img_path = "test_image.png"
|
|
29
|
-
img.save(img_path, "PNG")
|
|
30
|
-
yield img_path
|
|
31
|
-
# Cleanup
|
|
32
|
-
if os.path.exists(img_path):
|
|
33
|
-
os.remove(img_path)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
def test_resize_to_target_filesize_basic(sample_image):
|
|
37
|
-
# Test basic resizing functionality
|
|
38
|
-
target_size = 50 * 1024 # 50KB
|
|
39
|
-
output_path = resize_to_target_filesize(
|
|
40
|
-
sample_image,
|
|
41
|
-
target_size,
|
|
42
|
-
output_path="test_output.jpg",
|
|
43
|
-
format_name="JPEG",
|
|
44
|
-
quiet=True,
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
assert os.path.exists(output_path)
|
|
48
|
-
assert os.path.getsize(output_path) <= target_size
|
|
49
|
-
os.remove(output_path)
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
def test_resize_to_target_filesize_format_conversion(sample_image):
|
|
53
|
-
# Test format conversion
|
|
54
|
-
target_size = 50 * 1024 # 50KB
|
|
55
|
-
output_path = resize_to_target_filesize(
|
|
56
|
-
sample_image,
|
|
57
|
-
target_size,
|
|
58
|
-
output_path="test_output.webp",
|
|
59
|
-
format_name="WEBP",
|
|
60
|
-
quiet=True,
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
assert os.path.exists(output_path)
|
|
64
|
-
assert output_path.endswith(".webp")
|
|
65
|
-
os.remove(output_path)
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
def test_resize_to_target_filesize_min_dimension(sample_image):
|
|
69
|
-
# Test minimum dimension constraint
|
|
70
|
-
target_size = 50 * 1024 # 50KB
|
|
71
|
-
min_dim = 400
|
|
72
|
-
output_path = resize_to_target_filesize(
|
|
73
|
-
sample_image,
|
|
74
|
-
target_size,
|
|
75
|
-
output_path="test_output.jpg",
|
|
76
|
-
format_name="JPEG",
|
|
77
|
-
min_dimension=min_dim,
|
|
78
|
-
quiet=True,
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
img = Image.open(output_path)
|
|
82
|
-
width, height = img.size
|
|
83
|
-
assert min(width, height) >= min_dim
|
|
84
|
-
os.remove(output_path)
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
def test_resize_to_target_filesize_no_exact(sample_image):
|
|
88
|
-
# Test without exact size matching
|
|
89
|
-
target_size = 50 * 1024 # 50KB
|
|
90
|
-
output_path = resize_to_target_filesize(
|
|
91
|
-
sample_image,
|
|
92
|
-
target_size,
|
|
93
|
-
output_path="test_output.jpg",
|
|
94
|
-
format_name="JPEG",
|
|
95
|
-
exact_size=False,
|
|
96
|
-
quiet=True,
|
|
97
|
-
)
|
|
98
|
-
|
|
99
|
-
assert os.path.exists(output_path)
|
|
100
|
-
assert os.path.getsize(output_path) <= target_size
|
|
101
|
-
os.remove(output_path)
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
def test_try_quality_adjustment(sample_image):
|
|
105
|
-
# Test quality adjustment function
|
|
106
|
-
img = Image.open(sample_image)
|
|
107
|
-
target_size = 50 * 1024 # 50KB
|
|
108
|
-
result = _try_quality_adjustment(img, "JPEG", target_size, "test_output.jpg", quiet=True)
|
|
109
|
-
|
|
110
|
-
assert result is not None
|
|
111
|
-
assert os.path.exists(result)
|
|
112
|
-
assert os.path.getsize(result) <= target_size
|
|
113
|
-
os.remove(result)
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
def test_try_resizing(sample_image):
|
|
117
|
-
# Test resizing function
|
|
118
|
-
img = Image.open(sample_image)
|
|
119
|
-
target_size = 50 * 1024 # 50KB
|
|
120
|
-
result = _try_resizing(
|
|
121
|
-
img,
|
|
122
|
-
"JPEG",
|
|
123
|
-
target_size,
|
|
124
|
-
"test_output.jpg",
|
|
125
|
-
800, # original width
|
|
126
|
-
600, # original height
|
|
127
|
-
None, # no min dimension
|
|
128
|
-
quiet=True,
|
|
129
|
-
)
|
|
130
|
-
|
|
131
|
-
assert result is not None
|
|
132
|
-
assert os.path.exists(result)
|
|
133
|
-
assert os.path.getsize(result) <= target_size
|
|
134
|
-
os.remove(result)
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
def test_find_best_quality(sample_image):
|
|
138
|
-
# Test quality finding function
|
|
139
|
-
img = Image.open(sample_image)
|
|
140
|
-
target_size = 50 * 1024 # 50KB
|
|
141
|
-
result = _find_best_quality(img, "JPEG", target_size, quiet=True)
|
|
142
|
-
|
|
143
|
-
assert result is not None
|
|
144
|
-
size, buffer = result
|
|
145
|
-
assert size <= target_size
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
def test_adjust_to_exact_size(sample_image):
|
|
149
|
-
# Test exact size adjustment
|
|
150
|
-
img = Image.open(sample_image)
|
|
151
|
-
target_size = 50 * 1024 # 50KB
|
|
152
|
-
output_path = "test_output.jpg"
|
|
153
|
-
img.save(output_path, "JPEG", quality=95)
|
|
154
|
-
|
|
155
|
-
_adjust_to_exact_size(output_path, target_size, quiet=True)
|
|
156
|
-
|
|
157
|
-
assert os.path.exists(output_path)
|
|
158
|
-
assert abs(os.path.getsize(output_path) - target_size) <= 100
|
|
159
|
-
os.remove(output_path)
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
def test_png_format(sample_png):
|
|
163
|
-
# Test PNG format handling
|
|
164
|
-
target_size = 50 * 1024 # 50KB
|
|
165
|
-
output_path = resize_to_target_filesize(
|
|
166
|
-
sample_png,
|
|
167
|
-
target_size,
|
|
168
|
-
output_path="test_output.png",
|
|
169
|
-
format_name="PNG",
|
|
170
|
-
quiet=True,
|
|
171
|
-
)
|
|
172
|
-
|
|
173
|
-
assert os.path.exists(output_path)
|
|
174
|
-
assert output_path.endswith(".png")
|
|
175
|
-
os.remove(output_path)
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
def test_invalid_input():
|
|
179
|
-
# Test invalid input handling
|
|
180
|
-
with pytest.raises(Exception):
|
|
181
|
-
resize_to_target_filesize("nonexistent.jpg", 50 * 1024, quiet=True)
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
def test_small_target_size(sample_image):
|
|
185
|
-
# Test with very small target size
|
|
186
|
-
target_size = 1 * 1024 # 1KB
|
|
187
|
-
output_path = resize_to_target_filesize(
|
|
188
|
-
sample_image,
|
|
189
|
-
target_size,
|
|
190
|
-
output_path="test_output.jpg",
|
|
191
|
-
format_name="JPEG",
|
|
192
|
-
quiet=True,
|
|
193
|
-
)
|
|
194
|
-
|
|
195
|
-
assert os.path.exists(output_path)
|
|
196
|
-
assert os.path.getsize(output_path) <= target_size
|
|
197
|
-
os.remove(output_path)
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
def test_large_target_size(sample_image):
|
|
201
|
-
# Test with target size larger than original
|
|
202
|
-
original_size = os.path.getsize(sample_image)
|
|
203
|
-
target_size = original_size * 2 # Double the original size
|
|
204
|
-
output_path = resize_to_target_filesize(
|
|
205
|
-
sample_image,
|
|
206
|
-
target_size,
|
|
207
|
-
output_path="test_output.jpg",
|
|
208
|
-
format_name="JPEG",
|
|
209
|
-
quiet=True,
|
|
210
|
-
)
|
|
211
|
-
|
|
212
|
-
assert os.path.exists(output_path)
|
|
213
|
-
assert os.path.getsize(output_path) == original_size # Should not increase size
|
|
214
|
-
os.remove(output_path)
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
def test_try_combined_approach(sample_image):
|
|
218
|
-
# Test combined scaling and quality approach
|
|
219
|
-
img = Image.open(sample_image)
|
|
220
|
-
target_size = 100 * 1024 # 100KB
|
|
221
|
-
result = _try_combined_approach(
|
|
222
|
-
img,
|
|
223
|
-
"JPEG",
|
|
224
|
-
target_size,
|
|
225
|
-
"test_output.jpg",
|
|
226
|
-
800, # original width
|
|
227
|
-
600, # original height
|
|
228
|
-
None, # no min dimension
|
|
229
|
-
quiet=True,
|
|
230
|
-
)
|
|
231
|
-
|
|
232
|
-
assert result is not None
|
|
233
|
-
assert os.path.exists(result)
|
|
234
|
-
assert os.path.getsize(result) <= target_size
|
|
235
|
-
os.remove(result)
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
def test_final_quality_adjustment(sample_image):
|
|
239
|
-
# Test final quality adjustment when overshooting
|
|
240
|
-
img = Image.open(sample_image)
|
|
241
|
-
target_size = 50 * 1024 # 50KB
|
|
242
|
-
output_path = "test_output.jpg"
|
|
243
|
-
img.save(output_path, "JPEG", quality=95) # Save with high quality to ensure overshooting
|
|
244
|
-
|
|
245
|
-
_final_quality_adjustment(output_path, "JPEG", target_size, quiet=True)
|
|
246
|
-
|
|
247
|
-
assert os.path.exists(output_path)
|
|
248
|
-
assert os.path.getsize(output_path) <= target_size
|
|
249
|
-
os.remove(output_path)
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
def test_resize_to_target_filesize_error_handling():
|
|
253
|
-
# Test various error conditions
|
|
254
|
-
with pytest.raises(Exception):
|
|
255
|
-
# Test with invalid image path
|
|
256
|
-
resize_to_target_filesize("nonexistent.jpg", 50 * 1024, quiet=True)
|
|
257
|
-
|
|
258
|
-
with pytest.raises(Exception):
|
|
259
|
-
# Test with invalid format
|
|
260
|
-
resize_to_target_filesize("test.jpg", 50 * 1024, format_name="INVALID", quiet=True)
|
|
261
|
-
|
|
262
|
-
with pytest.raises(Exception):
|
|
263
|
-
# Test with negative target size
|
|
264
|
-
resize_to_target_filesize("test.jpg", -1, quiet=True)
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
def test_format_conversion_edge_cases(sample_image):
|
|
268
|
-
# Test format conversion edge cases
|
|
269
|
-
target_size = 50 * 1024 # 50KB
|
|
270
|
-
|
|
271
|
-
# Test JPEG to PNG conversion
|
|
272
|
-
output_path = resize_to_target_filesize(
|
|
273
|
-
sample_image,
|
|
274
|
-
target_size,
|
|
275
|
-
output_path="test_output.png",
|
|
276
|
-
format_name="PNG",
|
|
277
|
-
quiet=True,
|
|
278
|
-
)
|
|
279
|
-
assert output_path.endswith(".png")
|
|
280
|
-
os.remove(output_path)
|
|
281
|
-
|
|
282
|
-
# Test JPEG to WEBP conversion
|
|
283
|
-
output_path = resize_to_target_filesize(
|
|
284
|
-
sample_image,
|
|
285
|
-
target_size,
|
|
286
|
-
output_path="test_output.webp",
|
|
287
|
-
format_name="WEBP",
|
|
288
|
-
quiet=True,
|
|
289
|
-
)
|
|
290
|
-
assert output_path.endswith(".webp")
|
|
291
|
-
os.remove(output_path)
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import io
|
|
2
|
-
|
|
3
|
-
import pytest
|
|
4
|
-
from PIL import Image
|
|
5
|
-
|
|
6
|
-
from imgbytesizer.utils import (
|
|
7
|
-
IMG_FORMATS, get_file_size_bytes, get_output_format, get_output_path, parse_filesize
|
|
8
|
-
)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def test_parse_filesize():
|
|
12
|
-
# Test various file size formats
|
|
13
|
-
assert parse_filesize("1KB") == 1024
|
|
14
|
-
assert parse_filesize("1MB") == 1024 * 1024
|
|
15
|
-
assert parse_filesize("1GB") == 1024 * 1024 * 1024
|
|
16
|
-
assert parse_filesize("500B") == 500
|
|
17
|
-
assert parse_filesize("1.5KB") == int(1.5 * 1024)
|
|
18
|
-
assert parse_filesize("2.5MB") == int(2.5 * 1024 * 1024)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def test_parse_filesize_invalid():
|
|
22
|
-
# Test invalid file size formats
|
|
23
|
-
with pytest.raises(ValueError):
|
|
24
|
-
parse_filesize("")
|
|
25
|
-
with pytest.raises(ValueError):
|
|
26
|
-
parse_filesize("invalid")
|
|
27
|
-
with pytest.raises(ValueError):
|
|
28
|
-
parse_filesize("1TB") # Unsupported unit
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
def test_get_output_format():
|
|
32
|
-
# Test format determination
|
|
33
|
-
assert get_output_format("JPEG") == "JPEG"
|
|
34
|
-
assert get_output_format("JPG") == "JPEG"
|
|
35
|
-
assert get_output_format("PNG") == "PNG"
|
|
36
|
-
assert get_output_format("WEBP") == "WEBP"
|
|
37
|
-
|
|
38
|
-
# Test with requested format
|
|
39
|
-
assert get_output_format("JPEG", "PNG") == "PNG"
|
|
40
|
-
assert get_output_format("PNG", "WEBP") == "WEBP"
|
|
41
|
-
assert get_output_format("JPG", "JPEG") == "JPEG"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
def test_get_output_path():
|
|
45
|
-
# Test output path generation
|
|
46
|
-
assert get_output_path("image.jpg") == "image_resized.jpg"
|
|
47
|
-
assert get_output_path("image.png") == "image_resized.png"
|
|
48
|
-
assert get_output_path("image.jpg", "custom.jpg") == "custom.jpg"
|
|
49
|
-
assert get_output_path("image.jpg", format_name="PNG") == "image_resized.png"
|
|
50
|
-
assert get_output_path("image.jpg", format_name="WEBP") == "image_resized.webp"
|
|
51
|
-
assert get_output_path("image.jpg", "custom.webp", "WEBP") == "custom.webp"
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
def test_get_file_size_bytes():
|
|
55
|
-
# Create test image
|
|
56
|
-
img = Image.new("RGB", (100, 100), color="red")
|
|
57
|
-
|
|
58
|
-
# Test JPEG format
|
|
59
|
-
size, buffer = get_file_size_bytes(img, "JPEG", quality=95)
|
|
60
|
-
assert size > 0
|
|
61
|
-
assert isinstance(buffer, io.BytesIO)
|
|
62
|
-
|
|
63
|
-
# Test PNG format
|
|
64
|
-
size, buffer = get_file_size_bytes(img, "PNG")
|
|
65
|
-
assert size > 0
|
|
66
|
-
assert isinstance(buffer, io.BytesIO)
|
|
67
|
-
|
|
68
|
-
# Test WEBP format
|
|
69
|
-
size, buffer = get_file_size_bytes(img, "WEBP", quality=95)
|
|
70
|
-
assert size > 0
|
|
71
|
-
assert isinstance(buffer, io.BytesIO)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
def test_get_file_size_bytes_invalid():
|
|
75
|
-
# Create test image
|
|
76
|
-
img = Image.new("RGB", (100, 100), color="red")
|
|
77
|
-
|
|
78
|
-
# Test invalid format
|
|
79
|
-
with pytest.raises(Exception):
|
|
80
|
-
get_file_size_bytes(img, "INVALID")
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
def test_img_formats():
|
|
84
|
-
# Test supported formats
|
|
85
|
-
assert "jpg" in IMG_FORMATS
|
|
86
|
-
assert "jpeg" in IMG_FORMATS
|
|
87
|
-
assert "png" in IMG_FORMATS
|
|
88
|
-
assert "webp" in IMG_FORMATS
|
|
89
|
-
assert len(IMG_FORMATS) == 4
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
def test_get_file_size_bytes_png_with_quality():
|
|
93
|
-
img = Image.new("RGB", (50, 50), color="blue")
|
|
94
|
-
size, buffer = get_file_size_bytes(img, "PNG", quality=80)
|
|
95
|
-
assert size > 0
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
def test_get_output_path_with_jpeg_extension():
|
|
99
|
-
assert get_output_path("image.jpg", format_name="jpeg") == "image_resized.jpg"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|