File: dtype.py

package info (click to toggle)
python-imgviz 1.7.6%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,056 kB
  • sloc: python: 3,131; makefile: 25
file content (13 lines) | stat: -rw-r--r-- 381 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np


def bool2ubyte(img):
    assert img.dtype == bool, "img dtype must be bool"
    return img.astype(np.uint8) * 255


def float2ubyte(img):
    assert np.issubdtype(img.dtype, float), "img dtype must be float"
    assert img.min() >= 0, "img.min() must be >= 0"
    assert img.max() <= 1, "img.max() must be <= 1"
    return (img * 255).round().astype(np.uint8)