Skip to content

Commit 2b47722

Browse files
authored
Remove stdout from to_json (#97)
1 parent a2c8992 commit 2b47722

7 files changed

Lines changed: 14 additions & 41 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
# Released
1010

11+
## 5.0.1 - 22/08/2025
12+
13+
### Changed
14+
- **to_json() API**: Removed `stdout` parameter from `to_json()` method - method now only returns JSON data without printing
15+
1116
## 5.0.0 - 22/08/2025
1217

1318
### Added

Pylette/cmd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def handle_json_export(
118118
for i, result in enumerate(successful_results, 1):
119119
if result.palette is not None:
120120
filename = output_dir / f"palette_{i:03d}.json"
121-
result.palette.to_json(filename=str(filename), colorspace=colorspace, stdout=False)
121+
result.palette.to_json(filename=str(filename), colorspace=colorspace)
122122

123123
typer.echo(f"✓ Exported {len(successful_results)} palettes to {output_dir}")
124124

@@ -136,7 +136,7 @@ def handle_json_export(
136136

137137
for result in successful_results:
138138
if result.palette is not None:
139-
palette_data = result.palette.to_json(filename=None, colorspace=colorspace, stdout=False)
139+
palette_data = result.palette.to_json(filename=None, colorspace=colorspace)
140140
if palette_data is not None:
141141
palettes_list.append(palette_data)
142142

Pylette/src/palette.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ def to_json(
118118
filename: str | None = None,
119119
colorspace: ColorSpace = ColorSpace.RGB,
120120
include_metadata: bool = True,
121-
stdout: bool = True,
122121
) -> dict[str, object] | None:
123122
"""
124123
Exports the palette to JSON format.
@@ -127,7 +126,6 @@ def to_json(
127126
filename (str | None): File to save to. If None, returns the dictionary.
128127
colorspace (Literal["rgb", "hsv", "hls"]): Color space to use.
129128
include_metadata (bool): Whether to include palette metadata.
130-
stdout (bool): Whether to print to stdout.
131129
132130
Returns:
133131
dict | None: The palette data as a dictionary if filename is None.
@@ -187,10 +185,6 @@ def to_json(
187185

188186
palette_data["metadata"] = metadata_dict
189187

190-
# Print to stdout if requested
191-
if stdout:
192-
print(json.dumps(palette_data, indent=2))
193-
194188
# Save to file if filename provided
195189
if filename is not None:
196190
with open(filename, "w") as f:
@@ -205,7 +199,6 @@ def export(
205199
filename: str,
206200
colorspace: ColorSpace = ColorSpace.RGB,
207201
include_metadata: bool = True,
208-
stdout: bool = False,
209202
) -> None:
210203
"""
211204
Export palette to JSON format.
@@ -214,14 +207,13 @@ def export(
214207
filename (str): File to save to (extension will be added automatically if not present).
215208
colorspace (ColorSpace): Color space to use.
216209
include_metadata (bool): Whether to include metadata.
217-
stdout (bool): Whether to print to stdout.
218210
"""
219211

220212
# Add .json extension if not present
221213
if not filename.endswith(".json"):
222214
filename = f"{filename}.json"
223215

224-
self.to_json(filename=filename, colorspace=colorspace, include_metadata=include_metadata, stdout=stdout)
216+
self.to_json(filename=filename, colorspace=colorspace, include_metadata=include_metadata)
225217

226218
def __str__(self):
227219
return "".join(["({}, {}, {}, {}) \n".format(c.rgb[0], c.rgb[1], c.rgb[2], c.freq) for c in self.colors])

README.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,6 @@ The table automatically adapts to show the chosen colorspace (RGB, HSV, or HLS).
218218

219219
## Working with Transparent Images
220220

221-
## Working with Transparent Images
222-
223221
Handle transparency in both CLI and Python:
224222

225223
```bash
@@ -272,25 +270,3 @@ Options:
272270
--no-stdout Suppress table output
273271
--help Show help message
274272
```
275-
276-
```shell
277-
pylette --help
278-
279-
Usage: pylette [OPTIONS]
280-
281-
╭─ Options ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
282-
│ --filename PATH [default: None] │
283-
│ --image-url TEXT [default: None] │
284-
│ --mode [KM|MC] [default: KM] │
285-
│ --n INTEGER [default: 5] │
286-
│ --sort-by [frequency|luminance] [default: luminance] │
287-
│ --stdout --no-stdout [default: stdout] │
288-
│ --out-filename PATH [default: None] │
289-
│ --display-colors --no-display-colors [default: no-display-colors] │
290-
│ --colorspace [rgb|hsv|hls] [default: rgb] │
291-
│ --alpha-mask-threshold INTEGER RANGE [0<=x<=255] Alpha threshold for transparent image masking (0-255). Pixels with alpha below this value are excluded. [default: None] │
292-
│ --install-completion Install completion for the current shell. │
293-
│ --show-completion Show completion for the current shell, to copy it or customize the installation. │
294-
│ --help Show this message and exit. │
295-
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
296-
```

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "pylette"
3-
version = "5.0.0"
3+
version = "5.0.1"
44
description = "A Python library for extracting color palettes from images."
55
authors = [
66
{name = "Ivar Stangeby"}

tests/integration/test_json_export.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def test_json_semantic_format_all_colorspaces(self, test_image):
271271
palette = extract_colors(image=test_image, palette_size=2)
272272

273273
# Test RGB colorspace
274-
rgb_data = palette.to_json(filename=None, colorspace=ColorSpace.RGB, stdout=False)
274+
rgb_data = palette.to_json(filename=None, colorspace=ColorSpace.RGB)
275275
assert rgb_data["colorspace"] == "rgb"
276276

277277
for color_data in rgb_data["colors"]:
@@ -292,7 +292,7 @@ def test_json_semantic_format_all_colorspaces(self, test_image):
292292
assert color_data["hex"] == expected_hex
293293

294294
# Test HSV colorspace
295-
hsv_data = palette.to_json(filename=None, colorspace=ColorSpace.HSV, stdout=False)
295+
hsv_data = palette.to_json(filename=None, colorspace=ColorSpace.HSV)
296296
assert hsv_data["colorspace"] == "hsv"
297297

298298
for color_data in hsv_data["colors"]:
@@ -311,7 +311,7 @@ def test_json_semantic_format_all_colorspaces(self, test_image):
311311
assert len(color_data["rgb"]) == 3
312312

313313
# Test HLS colorspace
314-
hls_data = palette.to_json(filename=None, colorspace=ColorSpace.HLS, stdout=False)
314+
hls_data = palette.to_json(filename=None, colorspace=ColorSpace.HLS)
315315
assert hls_data["colorspace"] == "hls"
316316

317317
for color_data in hls_data["colors"]:
@@ -329,7 +329,7 @@ def test_json_no_duplication_for_rgb_colorspace(self, test_image):
329329
"""Test that RGB colorspace doesn't duplicate RGB values."""
330330
palette = extract_colors(image=test_image, palette_size=2)
331331

332-
rgb_data = palette.to_json(filename=None, colorspace=ColorSpace.RGB, stdout=False)
332+
rgb_data = palette.to_json(filename=None, colorspace=ColorSpace.RGB)
333333

334334
for color_data in rgb_data["colors"]:
335335
# Should only have rgb field, not both rgb and values

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)