Divide long web page screenshots into blocks to input models with shorter contexts. 将长网页截图进行区块分割,用于输入上下文较短的模型
Project description
Introduction
This project is used to split the long screenshot of web pages into several parts based on the height of the text. The main idea is to find the low variation region of the image, and then find the split line in the low variation region. The output are small but complete images of the web page, which can be used to generate web pages using Screen-to-code or to train models. More results can be found in the images directory.
Getting started
Install the dependencies
pip install opencv-python numpy
Using in the command line
Obtain the height of the split line of the image
python master.py --file_path path/to/image.jpg --split True --height_threshold 102 --variation_threshold 0.5 --color_threshold 100 --color_variation_threshold 15 --merge_threshold 350
Draw the split lines on the image
python spliter.py --image_file path/to/image.jpg --hl [100,200] --color (0,255,0)
For details, please refer to the help information
python master.py --help
python spliter.py --help
split_heights function
The split_heights
function is used to split an image into several parts based on various thresholds. It takes the following parameters:
file_path
: The path of the image file.split
: A boolean indicating whether to split the image.height_threshold
: The height threshold of the low variation region.variation_threshold
: The variation threshold of the low variation region.color_threshold
: The threshold of the color difference.color_variation_threshold
: The threshold of the color difference variation.merge_threshold
: The threshold of the least distance between two lines.
The function returns a list of heights of the split lines if split
is False
, or the path of the split image if split
is True
.
Example usage
from master import split_heights
# Split the image at 'path/to/image.jpg' into several parts
split_image_path = split_heights(
file_path='path/to/image.jpg',
split=True,
height_threshold=102,
variation_threshold=0.5,
color_threshold=100,
color_variation_threshold=15,
merge_threshold=350
)
print(f"The split image is saved at {split_image_path}")
In this example, the image at 'path/to/image.jpg' is split into several parts based on the provided thresholds. The split image is saved at the path returned by the function.
draw_line_from_file function
The draw_line_from_file
function is used to draw lines on an image at specified heights. It takes the following parameters:
image_file
: The path of the image file.heights
: A list of heights at which to draw the lines.color
: The color of the lines to be drawn. The default color is red(0, 0, 255)
.
The function reads the image from the provided file path, draws lines at the specified heights, and then saves the modified image to a new file. The new file is saved in the result
directory with the same name as the original file, but with 'result' appended before the file extension.
If the function encounters an error while reading the image file (for example, if the file path contains '.' or Chinese characters), it raises an exception.
Example usage
from spliter import draw_line_from_file
# Draw lines on the image at 'path/to/image.jpg' at heights 100 and 200
result_image_path = draw_line_from_file(
image_file='path/to/image.jpg',
heights=[100, 200],
color=(0, 255, 0) # Draw the lines in green
)
print(f"The modified image is saved at {result_image_path}")
In this example, the image at 'path/to/image.jpg' is modified by drawing green lines at heights 100 and 200. The modified image is saved at the path returned by the function.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Web_page_Screenshot_Segmentation-1.0.0.tar.gz
.
File metadata
- Download URL: Web_page_Screenshot_Segmentation-1.0.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96eee1bb6954d7163d310c4cbbca7a4f3f3f1e879a817af08c9582dd099caad7 |
|
MD5 | b564ef02ea21e8395e6861397c8103bd |
|
BLAKE2b-256 | 523ac357827da86f9111b533be6e1412fed04dcc25a27a8b7872e5163fe8b599 |
File details
Details for the file Web_page_Screenshot_Segmentation-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: Web_page_Screenshot_Segmentation-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf59fa186c53c76ea0fa3abe78bb2ce42d542b21b1cb3913c247c4c551cdda1c |
|
MD5 | ef3f669a19924d610075353482961964 |
|
BLAKE2b-256 | 304201267d13c000e98809dc94b6109180a1355a002d535249e867b85af1d21a |