lovely-numpy
  1. ๐Ÿ”Ž Array Representations
  2. ๐Ÿ–Œ๏ธ View as RGB images
  • ๐Ÿ’Ÿ 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

  • rgb
  1. ๐Ÿ”Ž Array Representations
  2. ๐Ÿ–Œ๏ธ View as RGB images

๐Ÿ–Œ๏ธ View as RGB images

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(6, 2))
plt.close(fig)
ax1.set_xticks([]); ax1.set_yticks([]); ax2.set_xticks([]);
ax2.set_yticks([]); ax3.set_xticks([]); ax3.set_yticks([])

np.random.seed(1337)
r = np.random.rand(10, 10, 3)

x = (r*256).astype(np.uint8)
fig_rgb(x, scale=10, ax=ax1)

x = r.astype(np.float16)
fig_rgb(x, scale=10, ax=ax2)

fig_rgb((r > 0.5), scale=10, ax=ax3)


rgb


def rgb(
    x:ndarray, # Array to display. [[...], C,H,W] or [[...], H,W,C]
    denorm:Any=None, # Reverse per-channel normalization
    cl:Any=True, # Channel-last
    gutter_px:int=3, # If more than one tensor -> tile with this gutter width
    frame_px:int=1, # If more than one tensor -> tile with this frame width
    scale:int=1, # Stretch the image. Only integers please.
    view_width:int=966, # target width of the image
    clip:bool=True, # Silently clip RGB values to [0, 1]
    ax:Optional=None, # Matplotlib axes
)->RGBProxy:
two_images = np.stack([image]*2)
lo(two_images)
array[2, 196, 196, 3] f32 n=230496 (0.9Mb) xโˆˆ[-2.118, 2.640] ฮผ=-0.388 ฯƒ=1.073
in_stats = (    (0.485, 0.456, 0.406),  # Mean
                (0.229, 0.224, 0.225) ) # std
rgb(two_images, denorm=in_stats)

# We also have presets
rgb(two_images, denorm='imagenet')

tenchman_01 = image * np.array(in_stats[1]) + np.array(in_stats[0]) # This image is in the range [0 .. 1] (+/- eps)
tenchman_minus1_1 = tenchman_01 * 2 - 1 # This one is in the range [-1 .. 1] - also somewhat common
rgb(tenchman_minus1_1, denorm='symmetric') # 'symmetric' does the conversion

rgb(image, denorm='minmax') # 'minmax' scales the full range of the inputs to [0..1].

# Make 8 images with progressively higher brightness and stack them 2x2x2.
eight_images = (np.stack([image]*8) + np.linspace(-2, 2, 8)[:,None,None,None])
eight_images = (eight_images
                     *np.array(in_stats[1])
                     +np.array(in_stats[0])
                ).clip(0,1).reshape(2,2,2,196,196,3)

lo(eight_images)
array[2, 2, 2, 196, 196, 3] n=921984 (7.0Mb) xโˆˆ[0., 1.000] ฮผ=0.382 ฯƒ=0.319
rgb(eight_images)

# You can do channel-first too (default in PyTorch):
# Also, can scale up the image
rgb(image.transpose(-1, 0, 1), cl=False, scale=2)