Skip to content

Commit 7f9263e

Browse files
author
Nutchanon Ninyawee
committed
Add CLI, improve image generation, and enhance resolution handling
- Implemented Command Line Interface (CLI) for capturing screenshots - Added support for pixel and physical (cm) dimensions in image generation - Introduced UnitConverter for precise unit conversions - Enhanced resolution handling with DPI and scale factor support - Added comprehensive test coverage for new features - Updated project configuration and dependencies - Improved documentation and usage examples in README
1 parent b76e484 commit 7f9263e

File tree

14 files changed

+1127
-1383
lines changed

14 files changed

+1127
-1383
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
### Added
11+
12+
- Command Line Interface (CLI) for capturing screenshots
13+
- Support for both pixel and physical (cm) dimensions in CLI
14+
- CLI options for scale factor and DPI settings
15+
16+
### Fixed
17+
18+
- Fixed unit conversion in UnitConverter class to properly handle Pint quantity objects
19+
- Improved DPI and resolution handling for more accurate image sizing
20+
21+
### Changed
22+
23+
- Adjusted default device scale factor to 1.5 (150% zoom) for better image quality

CONTRIBUTING.md

Lines changed: 0 additions & 51 deletions
This file was deleted.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 codustry
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ a robust, modern and high performance Python library for generating image from a
1717
</div>
1818

1919
## Highlight
20-
* All Basics, Async ready, Sync support, Fully Typed
21-
* Builtin batch generator for higher performance
22-
* Build on top of the giant `playwright`, simplified of headless browser and driver installation
23-
* Accept html/css string, and You can use with `jinja2` for html templating
20+
21+
- All Basics, Async ready, Sync support, Fully Typed
22+
- Builtin batch generator for higher performance
23+
- Build on top of the giant `playwright`, simplified of headless browser and driver installation
24+
- Accept html/css string, and You can use with `jinja2` for html templating
2425

2526
### TODO
26-
- Maybe you would like to add [gitmoji](https://gitmoji.carloscuesta.me/) to commit names. This is really funny. 😄
2727

28+
- Maybe you would like to add [gitmoji](https://gitmoji.carloscuesta.me/) to commit names. This is really funny. 😄
2829

2930
For building and deployment:
3031

@@ -48,6 +49,11 @@ For creating your open source community:
4849
pip install -U snap-html
4950

5051
python -m playwright install
52+
53+
# Install system dependencies for Linux (required for browsers)
54+
sudo playwright install-deps
55+
# Or manually install dependencies:
56+
sudo apt-get install libwoff1 libevent-2.1-7t64 libgstreamer-plugins-base1.0-0 libgstreamer-gl1.0-0 libgstreamer-plugins-bad1.0-0 libenchant-2-2 libsecret-1-0 libhyphen0 libmanette-0.2-0
5157
```
5258

5359
or install with `Poetry`
@@ -247,7 +253,7 @@ You can see the list of available releases on the [GitHub Releases](https://gith
247253

248254
We follow [Semantic Versions](https://semver.org/) specification.
249255

250-
We use [`Release Drafter`](https://github.com/marketplace/actions/release-drafter). As pull requests are merged, a draft release is kept up-to-date listing the changes, ready to publish when youre ready. With the categories option, you can categorize pull requests in release notes using labels.
256+
We use [`Release Drafter`](https://github.com/marketplace/actions/release-drafter). As pull requests are merged, a draft release is kept up-to-date listing the changes, ready to publish when you're ready. With the categories option, you can categorize pull requests in release notes using labels.
251257

252258
For Pull Request this labels are configured, by default:
253259

@@ -288,4 +294,104 @@ This project is licensed under the terms of the `MIT` license. See [LICENSE](htt
288294
This project was generated with [`python-package-template`](https://github.com/TezRomacH/python-package-template).
289295

290296
## Alternative
297+
291298
1. <https://github.com/vgalin/html2image>
299+
300+
## Usage
301+
302+
### Basic Usage
303+
304+
```python
305+
from snap_html import generate_image_sync
306+
from pathlib import Path
307+
308+
# Capture from URL
309+
screenshot = generate_image_sync(
310+
"https://www.example.com",
311+
resolution={"width": 1920, "height": 1080},
312+
output_file=Path("screenshot.png")
313+
)
314+
315+
# Capture with physical dimensions (in centimeters)
316+
screenshot = generate_image_sync(
317+
"https://www.example.com",
318+
resolution={
319+
"cm_width": 21, # A4 width
320+
"cm_height": 29.7, # A4 height
321+
"dpi": 300 # Print quality DPI
322+
},
323+
output_file=Path("high_quality.png")
324+
)
325+
```
326+
327+
### Resolution Options
328+
329+
The library supports two ways to specify output resolution:
330+
331+
1. Pixel Dimensions:
332+
333+
```python
334+
resolution = {
335+
"width": 1920, # Width in pixels
336+
"height": 1080 # Height in pixels
337+
}
338+
```
339+
340+
2. Physical Dimensions:
341+
342+
```python
343+
resolution = {
344+
"cm_width": 21, # Width in centimeters
345+
"cm_height": 29.7, # Height in centimeters
346+
"dpi": 300 # Dots per inch (optional, defaults to 300)
347+
}
348+
```
349+
350+
### Batch Processing
351+
352+
For better performance when capturing multiple screenshots:
353+
354+
```python
355+
from snap_html import generate_image_batch_sync
356+
357+
screenshots = generate_image_batch_sync(
358+
targets=["https://example1.com", "https://example2.com"],
359+
resolution={"width": 1920, "height": 1080},
360+
output_files=["screenshot1.png", "screenshot2.png"]
361+
)
362+
```
363+
364+
## CLI Usage
365+
366+
snap-html provides a command-line interface for quick image captures:
367+
368+
```bash
369+
# Basic usage with pixel dimensions
370+
snap-html capture https://example.com -o screenshot.png --width 1920 --height 1080
371+
372+
# Using physical dimensions (e.g., A4 paper size)
373+
snap-html capture input.html --cm-width 21.0 --cm-height 29.7 --dpi 300 -o output.png
374+
375+
# Capture with custom scale factor
376+
snap-html capture https://example.com -o screenshot.png --width 1024 --height 768 --scale 2.0
377+
```
378+
379+
### CLI Options
380+
381+
```
382+
Options:
383+
-o, --output PATH Output image file path
384+
-w, --width INTEGER Output width in pixels
385+
-h, --height INTEGER Output height in pixels
386+
--cm-width FLOAT Output width in centimeters
387+
--cm-height FLOAT Output height in centimeters
388+
--dpi INTEGER DPI for cm-based resolution [default: 300]
389+
--scale FLOAT Browser scale factor (zoom level) [default: 1.0]
390+
--help Show this message and exit.
391+
```
392+
393+
The CLI supports three types of input:
394+
395+
1. URLs (e.g., https://example.com)
396+
2. HTML files (e.g., page.html)
397+
3. Raw HTML strings

cookiecutter-config-file.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)