Straight Rectangles on Canvas
Project description
SROC
Straight Rectangles on Canvas
SROC (Straight Rectangles on Canvas) is a Python library for manipulating straight rectangles and handling interactions with a canvas. It provides classes and methods for creating, modifying, and analyzing rectangles, as well as handling image tiling and canvas scanning operations.
Features
- Rect: A class to represent a rectangle with various utility methods for manipulation and calculation.
- Rects: Base class to manage a collection of Rect objects with various utility methods for manipulation and calculation.
- CactusRects: Specialized class derived from Rects to create and handle collections of adjacent rectangles.
- RectCanvas: Base class to represent a canvas with overlayed rectangles and provide various utility methods for manipulation and visualization.
- RectScanner: Specialized class derived from RectCanvas to scan and detect filled rectangles from the underlying canvas and extract as rectangles.
- CanvasTiler: Specialized class derived from RectCanvas to handle tiling operations on the canvas.
Usage
Rect Class
The Rect class represents a rectangle and provides various methods for manipulation and calculation.
from sroc import Rect
# Create a rectangle from a bounding box (x0, y0, x1, y1)
# Same as Rect().fromBox()
rect1 = Rect(box=(0, 0, 100, 100))
# Create a rectangle from a bounding box with specific order
# ('xy' is the default, 'yx, 'xx', 'yy' are supported)
rect1 = Rect(box=(0, 0, 100, 100), order="yx")
# Create a rectangle from tuple of n points (y1, x1, y0, x0)
# Same as Rect().fromPoints().
rect2 = Rect(points=((0, 0), (100, 100)))
# Get the width and height of the rectangle
width = rect1.width()
height = rect1.height()
# Rotate the rectangle by 90 degrees
rect1.rotate(90)
# Find distance between rect1 and rect2
distance = rect1.getDistFromRect(rect2, reference='border', type="manhattan")
# Get a tuple with the 4 vertex as y,x points
rect1_points = rect1.to4points(order="yx", sequence="maxmin")
Viewport
Rectangle viewport can be set both at initialization passing vp_xmax and vp_ymax or calling
setViewportLimit(vp_xmax, vp_ymax).
Rectangle coordinates can be set or retrieved as fractions of the viewport setting use_vp_fractions=True.
Rects Class
The Rects class manages a collection of Rect objects and provides methods for manipulation and calculation.
from sroc import Rect
from sroc import Rects
# Create a collection of rectangles
rects1 = Rects()
# Add a rectangle to the collection
rects1.addRect(Rect(box=(0, 0, 100, 100)))
# Add a rectangle from bounding box or tuples of n points (same methods from Rect can be used)
rects2 = Rects().fromBox([(0, 0, 100, 100), (0, 100, 100, 200)])
# Simplify the collection of rectangles (will merge rects to (0, 0, 100, 200))
rects2.simplifyRects(tolerance=5)
# Moves rectangles from rect2 to rect1
rects1.moveRectsFrom(rects2)
# Get a list of the rectangles as 'xy' 'minmax' bounding boxes
rectangles = rects1.toBox()
CactusRects Class
The CactusRects class is a specialized class derived from Rects to handle specific operations with tolerance and
strategy.
from sroc import CactusRects
# Create a CactusRects object with a seed rectangle
cactus_rects = CactusRects(seedRect=Rect(box=(0, 0, 100, 100)), tolerance=5, strategy="full")
# Add a rectangle to the collection
cactus_rects.addRect(Rect(box=(50, 50, 150, 150)))
RectCanvas Class
The RectCanvas class represents a canvas with rectangles and provides various methods for manipulation and
visualization.
from sroc import RectCanvas
# Create a RectCanvas object from an image path
canvas = RectCanvas(image_path="path/to/image.png")
# Get the original canvas
original_canvas = canvas.getOriginalCanvas()
# Get the current canvas with modifications
current_canvas = canvas.getCurrentCanvas(show_rects=True, show_labels=True)
RectScanner Class
The RectScanner class is derived from RectCanvas to scan and detect filled rectangles in an image.
from sroc.overlay import RectScanner
# Create a RectScanner object from an image path
scanner = RectScanner(image_path="path/to/image.png")
# Scan the image for filled rectangles
filled_rects = scanner.scanFilledRects(color_treshold=200, min_lenght=20)
CanvasTiler Class
The CanvasTiler class is derived from RectCanvas to handle tiling operations on the canvas.
from sroc.overlay import CanvasTiler
# Create a CanvasTiler object from an image path
tiler = CanvasTiler(image_path="path/to/image.png", tile_width=640, tile_height=640)
# Save the tiles to an output path
tiler.saveTiles(output_path="path/to/output")
# Join the tiles from an input path and save the result
tiler.joinTiles(tiles_path="path/to/tiles", output_path="path/to/output")
License
This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.
Author
Giovanni Cascione - ing.cascione@gmail.com
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sroc-0.2.3.tar.gz.
File metadata
- Download URL: sroc-0.2.3.tar.gz
- Upload date:
- Size: 26.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.12.3 Linux/6.8.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6b462ac5574f545872904555edf06e754da1a0bf61a089f4dab769adb8fd09a
|
|
| MD5 |
64241c32edfbf0c13206520ae885f21b
|
|
| BLAKE2b-256 |
c3eeee36b4c7c8b793dcf30220738137a14227a5c02f065a1208cf818a8aed52
|
File details
Details for the file sroc-0.2.3-py3-none-any.whl.
File metadata
- Download URL: sroc-0.2.3-py3-none-any.whl
- Upload date:
- Size: 27.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.12.3 Linux/6.8.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dca2079386180b5ded8241278dcdf545d04cb892f1d95bfa5aab7eb9b1eb7c1a
|
|
| MD5 |
e2a0ae93e026bab7d4a9d7484e8f135d
|
|
| BLAKE2b-256 |
c26b437f32b5e2a503478795df69f98ad4334d0500ede6a2cc42efebb6299aca
|