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)


source

rgb

 rgb (x:numpy.ndarray, denorm:Any=None, cl:Any=True, gutter_px:int=3,
      frame_px:int=1, scale:int=1, view_width:int=966, clip:bool=True,
      ax:Optional[matplotlib.axes._axes.Axes]=None)
Type Default Details
x ndarray Array to display. [[โ€ฆ], C,H,W] or [[โ€ฆ], H,W,C]
denorm Any None Reverse per-channel normalizatoin
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 itegers please.
view_width int 966 target width of the image
clip bool True Selently clip RGB values to [0, 1]
ax Optional None Matplotlib axes
Returns RGBProxy
rgb(image)

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)

# 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)