Skip to main content

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

sroc-0.1.0.tar.gz (25.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sroc-0.1.0-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

Details for the file sroc-0.1.0.tar.gz.

File metadata

  • Download URL: sroc-0.1.0.tar.gz
  • Upload date:
  • Size: 25.2 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

Hashes for sroc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0fbc161e40e97ba53d25def97c8d83645d885c4e1dd9340bd0042ac403eca1a6
MD5 b56187844cde383cd11ad1c6f4c8d881
BLAKE2b-256 29a65351bf2b41ad5ccef5d3c79117fbb35b940c785a3394e4a41eee44807ae3

See more details on using hashes here.

File details

Details for the file sroc-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sroc-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 26.5 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

Hashes for sroc-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b6b58fb885ec3c5fe5d363534cb6689ef3cfe4dfe507bdf0e5e0685e3ed4a1a2
MD5 528679f46203375fbc3ca64429290807
BLAKE2b-256 f538c857c9da798a70050b64044ee79837e91719b120c674083fe09518e260c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page