lovely-numpy
  1. ๐Ÿ–ผ๏ธ Image utils
  2. ๐Ÿ”ฒ Pad and frame
  • ๐Ÿ’Ÿ Lovely NumPy
  • ๐Ÿ”Ž Array Representations
    • ๐Ÿงพ View as a summary
    • ๐Ÿ–Œ๏ธ View as RGB images
    • ๐Ÿ“Š View as a histogram
    • ๐Ÿ“บ View channels
  • ๐Ÿ–ผ๏ธ Image utils
    • ๐ŸŽจ color mapping
    • ๐Ÿ”ฒ Pad and frame
    • ๐Ÿ Image grid
  • โœจ Misc
    • ๐Ÿ‘๏ธ Lo and behold!
    • ๐ŸŽญ Matplotlib integration
    • ๐Ÿค” Config

On this page

  • pad_frame
  • pad_frame_gutters
  1. ๐Ÿ–ผ๏ธ Image utils
  2. ๐Ÿ”ฒ Pad and frame

๐Ÿ”ฒ Pad and frame


pad_frame


def pad_frame(
    t:ndarray, # torch.Tensor,  # 3D+ image tensor, [...,H,W,C]
    frame_px:int=1, # Number of pixels to pad each side.
    val:float=0, # Value to pad with.
):

Pad H and W dimensitons of an image tensor with val of thickness frame_px

Examples

pad_frame(np.zeros((3,3,1)), frame_px=2, val=1).transpose(-1,0,1).astype(int)
array([[[1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1],
        [1, 1, 0, 0, 0, 1, 1],
        [1, 1, 0, 0, 0, 1, 1],
        [1, 1, 0, 0, 0, 1, 1],
        [1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1]]])
# 1px black frame inside 2px white frame (gutter).
pad_frame(pad_frame(np.ones((3,3,1))), frame_px=2, val=1 ).transpose(-1,0,1).astype(int)
array([[[1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 0, 0, 0, 0, 0, 1, 1],
        [1, 1, 0, 1, 1, 1, 0, 1, 1],
        [1, 1, 0, 1, 1, 1, 0, 1, 1],
        [1, 1, 0, 1, 1, 1, 0, 1, 1],
        [1, 1, 0, 0, 0, 0, 0, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1]]])

pad_frame_gutters


def pad_frame_gutters(
    t:ndarray, # 3D+ Tensor image tensor, [...,H,W,C]
    gutter_px:int=3, # Write gutter in pixels.
    frame_px:int=1, # Black frame, in pixels
):

Add a black frame and white gutters around an image

Examples

Lo(image)
array[3, 196, 196] n=115248 (0.9Mb) xโˆˆ[-4.053e-09, 1.000] ฮผ=0.361 ฯƒ=0.248
Lo(pad_frame_gutters(image.transpose(1, 2, 0), gutter_px=15, frame_px=3))
array[232, 232, 3] n=161472 (1.2Mb) xโˆˆ[-4.053e-09, 1.000] ฮผ=0.500 ฯƒ=0.359
def to_pil(x):
    return Image.fromarray(np.uint8(x*255))

to_pil(pad_frame_gutters(image.transpose(1, 2, 0), gutter_px=15, frame_px=3))