imgbytesizer 0.2.0__tar.gz → 0.2.1__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: imgbytesizer
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: A CLI tool to resize images to a target file size.
5
5
  Author-email: pyyupsk <contact@fasu.dev>
6
6
  License: MIT License
@@ -48,7 +48,7 @@ imgbytesizer image.jpg 500KB
48
48
  imgbytesizer large_photo.jpg 500KB
49
49
 
50
50
  # Resize and convert to WebP format
51
- imgbytesizer image.png 250KB -f webp
51
+ imgbytesizer image.png 1MB -f webp
52
52
 
53
53
  # Specify output file path
54
54
  imgbytesizer photo.jpg 1MB -o compressed_photo.jpg
@@ -95,37 +95,45 @@ The tool prioritizes maintaining the highest possible quality while meeting the
95
95
  ## Example Output
96
96
 
97
97
  ```
98
+ ❯ imgbytesizer image.png 1MB -f webp
99
+
98
100
  ◐ Opening image.png
99
101
  File: image.png
100
102
  Format: WEBP
101
- Dimensions: 1920 × 1080 pixels
102
- Size: 1.09 MB
103
- Target size: 250.00 KB
103
+ Dimensions: 1024 × 1024 pixels
104
+ Size: 1.95 MB
105
+ Target size: 1.00 MB
104
106
  Trying quality adjustment without resizing...
105
- Testing quality 95 |█████████████████████░░░░░░░░░| 70% Size: 145.37 KB
106
- ✓ Found optimal quality: 95 (size: 145.37 KB)
107
+ Testing quality 100 |█████████████████░░░░░░░░░░░░░| 58% Size: 476.46 KB
108
+ ✓ Found optimal quality: 100 (size: 476.46 KB)
109
+ Trying combined scaling and quality approach...
110
+ Trying scale factor 1.00 (1024×1024)...
111
+ Quality 100 |██████████████████████████████| 100% Size: 476.46 KB
112
+ Trying scale factor 1.25 (1280×1280)...
113
+ Quality 100 |██████████████████████████████| 100% Size: 680.51 KB
114
+ Trying scale factor 1.50 (1536×1536)...
115
+ Quality 100 |██████████████████████████████| 100% Size: 903.38 KB
116
+ Trying scale factor 2.00 (2048×2048)...
117
+ Quality 96 |██████████████████████████████| 100% Size: 1011.68 KB
118
+
119
+ ✓ Combined approach success: scale=2.00, size=1011.68 KB
107
120
  Adjusting to exact target size...
108
- Optimizing quality for exact size match...
109
- Testing quality 96 |██████████████████████████████| 100%
110
-
111
- ✓ Quality adjusted to 96: 146.07 KB
112
- Adding 103.93 KB padding...
113
- ✓ File padded to exact size
121
+ ✓ Adjusted to exact size: 1.00 MB
114
122
 
115
123
  ╭─────────────┬────────────┬───────────────╮
116
124
  │ Metric │ Original │ Processed │
117
125
  ├─────────────┼────────────┼───────────────┤
118
- │ Dimensions │ 1920×1080 │ 1920×1080 │
126
+ │ Dimensions │ 1024×1024 │ 2048×2048 │
119
127
  ├─────────────┼────────────┼───────────────┤
120
- │ Size │ 1.09 MB │ 250.00 KB │
128
+ │ Size │ 1.95 MB │ 1.00 MB │
121
129
  ├─────────────┼────────────┼───────────────┤
122
- │ Target Size │ │ 250.00 KB │
130
+ │ Target Size │ │ 1.00 MB │
123
131
  ├─────────────┼────────────┼───────────────┤
124
132
  │ Difference │ │ 0 B (0.0%) │
125
133
  ├─────────────┼────────────┼───────────────┤
126
- │ Reduction │ │ 77.6% smaller │
134
+ │ Reduction │ │ 48.8% smaller │
127
135
  ╰─────────────┴────────────┴───────────────╯
128
- Time taken: 3.77 seconds
136
+ Time taken: 28.12 seconds
129
137
  Output file: image_resized.webp
130
138
  ```
131
139
 
@@ -34,7 +34,7 @@ imgbytesizer image.jpg 500KB
34
34
  imgbytesizer large_photo.jpg 500KB
35
35
 
36
36
  # Resize and convert to WebP format
37
- imgbytesizer image.png 250KB -f webp
37
+ imgbytesizer image.png 1MB -f webp
38
38
 
39
39
  # Specify output file path
40
40
  imgbytesizer photo.jpg 1MB -o compressed_photo.jpg
@@ -81,37 +81,45 @@ The tool prioritizes maintaining the highest possible quality while meeting the
81
81
  ## Example Output
82
82
 
83
83
  ```
84
+ ❯ imgbytesizer image.png 1MB -f webp
85
+
84
86
  ◐ Opening image.png
85
87
  File: image.png
86
88
  Format: WEBP
87
- Dimensions: 1920 × 1080 pixels
88
- Size: 1.09 MB
89
- Target size: 250.00 KB
89
+ Dimensions: 1024 × 1024 pixels
90
+ Size: 1.95 MB
91
+ Target size: 1.00 MB
90
92
  Trying quality adjustment without resizing...
91
- Testing quality 95 |█████████████████████░░░░░░░░░| 70% Size: 145.37 KB
92
- ✓ Found optimal quality: 95 (size: 145.37 KB)
93
+ Testing quality 100 |█████████████████░░░░░░░░░░░░░| 58% Size: 476.46 KB
94
+ ✓ Found optimal quality: 100 (size: 476.46 KB)
95
+ Trying combined scaling and quality approach...
96
+ Trying scale factor 1.00 (1024×1024)...
97
+ Quality 100 |██████████████████████████████| 100% Size: 476.46 KB
98
+ Trying scale factor 1.25 (1280×1280)...
99
+ Quality 100 |██████████████████████████████| 100% Size: 680.51 KB
100
+ Trying scale factor 1.50 (1536×1536)...
101
+ Quality 100 |██████████████████████████████| 100% Size: 903.38 KB
102
+ Trying scale factor 2.00 (2048×2048)...
103
+ Quality 96 |██████████████████████████████| 100% Size: 1011.68 KB
104
+
105
+ ✓ Combined approach success: scale=2.00, size=1011.68 KB
93
106
  Adjusting to exact target size...
94
- Optimizing quality for exact size match...
95
- Testing quality 96 |██████████████████████████████| 100%
96
-
97
- ✓ Quality adjusted to 96: 146.07 KB
98
- Adding 103.93 KB padding...
99
- ✓ File padded to exact size
107
+ ✓ Adjusted to exact size: 1.00 MB
100
108
 
101
109
  ╭─────────────┬────────────┬───────────────╮
102
110
  │ Metric │ Original │ Processed │
103
111
  ├─────────────┼────────────┼───────────────┤
104
- │ Dimensions │ 1920×1080 │ 1920×1080 │
112
+ │ Dimensions │ 1024×1024 │ 2048×2048 │
105
113
  ├─────────────┼────────────┼───────────────┤
106
- │ Size │ 1.09 MB │ 250.00 KB │
114
+ │ Size │ 1.95 MB │ 1.00 MB │
107
115
  ├─────────────┼────────────┼───────────────┤
108
- │ Target Size │ │ 250.00 KB │
116
+ │ Target Size │ │ 1.00 MB │
109
117
  ├─────────────┼────────────┼───────────────┤
110
118
  │ Difference │ │ 0 B (0.0%) │
111
119
  ├─────────────┼────────────┼───────────────┤
112
- │ Reduction │ │ 77.6% smaller │
120
+ │ Reduction │ │ 48.8% smaller │
113
121
  ╰─────────────┴────────────┴───────────────╯
114
- Time taken: 3.77 seconds
122
+ Time taken: 28.12 seconds
115
123
  Output file: image_resized.webp
116
124
  ```
117
125
 
@@ -0,0 +1,7 @@
1
+ """
2
+ imgbytesizer: A CLI tool to resize images to a target file size.
3
+ """
4
+
5
+ __version__: str = "0.2.1"
6
+ __author__: str = "pyyupsk"
7
+ __description__: str = "Resize images to match a target file size"
@@ -4,22 +4,28 @@ Terminal formatting utilities for imgbytesizer.
4
4
 
5
5
  import sys
6
6
  import logging
7
+ from typing import Tuple, Optional
7
8
  from tabulate import tabulate
8
9
 
9
10
  from .logger import Colors
10
11
 
11
12
 
12
- def format_filesize(size_bytes, precision=2):
13
+ def format_filesize(size_bytes: int | None, precision: int = 2) -> str:
13
14
  """Format file size in a human-readable format."""
15
+ if size_bytes is None:
16
+ return "N/A"
17
+
14
18
  if size_bytes < 1024:
15
19
  return f"{size_bytes} B"
16
20
  elif size_bytes < 1024 * 1024:
17
- return f"{size_bytes/1024:.{precision}f} KB"
21
+ return f"{size_bytes / 1024:.{precision}f} KB"
18
22
  else:
19
- return f"{size_bytes/(1024*1024):.{precision}f} MB"
23
+ return f"{size_bytes / (1024 * 1024):.{precision}f} MB"
20
24
 
21
25
 
22
- def print_progress_bar(progress, total, prefix="", suffix="", length=30):
26
+ def print_progress_bar(
27
+ progress: int, total: int, prefix: str = "", suffix: str = "", length: int = 30
28
+ ) -> None:
23
29
  """Print a progress bar with percentage."""
24
30
  if not sys.stdout.isatty():
25
31
  return # Don't print progress bars if not in a terminal
@@ -40,7 +46,7 @@ def print_progress_bar(progress, total, prefix="", suffix="", length=30):
40
46
  print()
41
47
 
42
48
 
43
- def print_result(name, value, status=None):
49
+ def print_result(name: str, value: str, status: Optional[str] = None) -> None:
44
50
  """Print a name-value pair with optional status color."""
45
51
  value_color = Colors.ENDC
46
52
  if status == "good":
@@ -56,7 +62,7 @@ def print_result(name, value, status=None):
56
62
  print(f" {name}: {value}")
57
63
 
58
64
 
59
- def print_processing_step(step, description):
65
+ def print_processing_step(step: int, description: str) -> None:
60
66
  """Print a processing step with a spinner."""
61
67
  if not sys.stdout.isatty():
62
68
  logger = logging.getLogger("imgbytesizer")
@@ -72,8 +78,12 @@ def print_processing_step(step, description):
72
78
 
73
79
 
74
80
  def print_comparison_table(
75
- original_size, original_dimensions, final_size, final_dimensions, target_size
76
- ):
81
+ original_size: int,
82
+ original_dimensions: Tuple[int, int],
83
+ final_size: int,
84
+ final_dimensions: Tuple[int, int],
85
+ target_size: int,
86
+ ) -> None:
77
87
  """Print comparison table between original and processed image."""
78
88
  if not sys.stdout.isatty():
79
89
  # Use simpler output for non-terminal environments
@@ -1,20 +1,21 @@
1
1
  import sys
2
2
  import logging
3
+ from typing import Dict, ClassVar
3
4
 
4
5
 
5
6
  # ANSI Color Codes for beautiful terminal output
6
7
  class Colors:
7
- BLUE = "\033[94m"
8
- CYAN = "\033[96m"
9
- GREEN = "\033[92m"
10
- YELLOW = "\033[93m"
11
- RED = "\033[91m"
12
- ENDC = "\033[0m"
13
- BOLD = "\033[1m"
14
- UNDERLINE = "\033[4m"
8
+ BLUE: ClassVar[str] = "\033[94m"
9
+ CYAN: ClassVar[str] = "\033[96m"
10
+ GREEN: ClassVar[str] = "\033[92m"
11
+ YELLOW: ClassVar[str] = "\033[93m"
12
+ RED: ClassVar[str] = "\033[91m"
13
+ ENDC: ClassVar[str] = "\033[0m"
14
+ BOLD: ClassVar[str] = "\033[1m"
15
+ UNDERLINE: ClassVar[str] = "\033[4m"
15
16
 
16
17
  @staticmethod
17
- def supports_color():
18
+ def supports_color() -> bool:
18
19
  """Check if the terminal supports color output."""
19
20
  # Add proper color support detection for different platforms
20
21
  return sys.stdout.isatty()
@@ -23,7 +24,7 @@ class Colors:
23
24
  class ColoredFormatter(logging.Formatter):
24
25
  """Custom formatter for colored console output."""
25
26
 
26
- FORMATS = {
27
+ FORMATS: ClassVar[Dict[int, str]] = {
27
28
  logging.DEBUG: Colors.BLUE + "%(message)s" + Colors.ENDC,
28
29
  logging.INFO: "%(message)s",
29
30
  logging.WARNING: Colors.YELLOW + "%(message)s" + Colors.ENDC,
@@ -31,13 +32,13 @@ class ColoredFormatter(logging.Formatter):
31
32
  logging.CRITICAL: Colors.RED + Colors.BOLD + "%(message)s" + Colors.ENDC,
32
33
  }
33
34
 
34
- def format(self, record):
35
+ def format(self, record: logging.LogRecord) -> str:
35
36
  log_fmt = self.FORMATS.get(record.levelno)
36
37
  formatter = logging.Formatter(log_fmt)
37
38
  return formatter.format(record)
38
39
 
39
40
 
40
- def setup_logger():
41
+ def setup_logger() -> logging.Logger:
41
42
  """Setup and return the application logger."""
42
43
  logger = logging.getLogger("imgbytesizer")
43
44
 
@@ -12,7 +12,7 @@ from .resizer import resize_to_target_filesize
12
12
  from .utils import parse_filesize, IMG_FORMATS
13
13
 
14
14
 
15
- def main():
15
+ def main() -> int:
16
16
  """Main entry point for the CLI."""
17
17
  # Setup logger
18
18
  logger = setup_logger()