File: prix.py

package info (click to toggle)
pypng 0.20220715.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 704 kB
  • sloc: python: 4,816; sh: 204; makefile: 18
file content (66 lines) | stat: -rw-r--r-- 1,753 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# prix.py

"""
prix (Python Raster Image eXtensions)

A collection of routines useful for raster image processing,
mostly not associate with the PNG format, but
using datatypes from PyPNG.
"""

# https://pypi.org/project/pypng/
import png


def window(image, tl, br):
    """image is a png.Image instance (or like one).
    A new png.Image instance is returned that
    represents a windowed image.
    The window is an axis aligned rectangle with opposite corners
    at *tl* and *br* (each being an (x,y) pair).

    As in ImageMagick (0,0) is the top-left.

    Coordinates are usually integers, but can be passed as
    `None` in which case the relevant image boundary will be
    used (0 for left, image width for right, 0 for top, image
    height for bottom).
    """

    width, height = image.info["size"]

    left, top = tl
    right, bottom = br

    if top is None:
        top = 0
    if left is None:
        left = 0
    if bottom is None:
        bottom = height
    if right is None:
        right = width

    if not (0 <= left < right <= width):
        raise NotImplementedError()
    if not (0 <= top < bottom <= height):
        raise NotImplementedError()
    # Compute left and right index bounds for each row,
    # given that each row is a flat row of values.
    l = left * image.info["planes"]
    r = right * image.info["planes"]

    def itercrop():
        """An iterator to perform the crop."""

        for i, row in enumerate(image.rows):
            if i < top:
                continue
            if i >= bottom:
                # Same as "raise StopIteration"
                return
            yield row[l:r]

    info = dict(image.info, size=(right - left, bottom - top))
    return png.Image(itercrop(), info)