Image.Random#
- classmethod Image.Random(*, size: int | Sequence[int] | None = None, colororder: str | None = None, dtype: Dtype | None = None, maxval: int | float | None = None, pdf: np.ndarray | None = None, like: Image | None = None) Self[source]#
Create image with random pixel values
- Parameters:
The short-name aliases
'int','float','double','half'are also accepted and resolve to the Toolbox’s own defaults (uint8,float32,float64,float16respectively) – this matters because NumPy’s ownnp.dtype('float')resolves tofloat64, notfloat32. SeeDTYPE_ALIASES.- Parameters:
maxval (same as
dtype, optional) – maximum value for random values, defaults to Nonepdf (1D or 2D array_like, optional) – probability density function for pixel values, defaults to None
like (
Imageor None, optional) – template image supplying defaultsize,dtypeandcolororderwhen those are not given explicitly
- Returns:
image of random values
- Return type:
The dimensions can be specified by a single scalar, a 2-tuple or a 3-tuple. If a single scalar is given the image is square. If a 2-tuple is given the image has one plane, if a 3-tuple is given the last element specifies the number of planes.
Creates a new image where pixels are initialized to random values:
for an integer image the values are uniformly distributed in the range 0 to
maxval. Ifmaxvalis not given then the values span the range 0 to the maximum positive value of the datatype. For a floating image the values span the range 0.0 to 1.0.if
pdfis given then the pixel values are drawn from the given probability density function, which should be normalized to sum to 1. Ifpdfis a 1D array then the same pdf is used for all planes, if it is a 2D array then each column gives the pdf for the corresponding plane. This option is only supported foruint8images, and if given thenmaxvalis ignored.
Examples
>>> from machinevisiontoolbox import Image >>> img = Image.Random(size=3) >>> img.print() 200 64 128 78 20 106 181 155 211 >>> img = Image.Random(size=3, colororder='RGB') >>> img.print() plane R: 176 132 236 96 67 177 128 225 117 plane G: 147 243 68 107 100 43 86 167 230 plane B: 15 204 119 157 163 57 86 38 9 >>> img.red().print() 176 132 236 96 67 177 128 225 117 >>> img = Image.Random(size=3, dtype='float32') >>> img.print <bound method Image.print of Image(size=(3, 3), dtype=float32)> >>> Image.Random(size=100).disp() <matplotlib.image.AxesImage object at 0x7411847a64b0>
(
Source code,png,hires.png,pdf)
We could, for example, create a random image with the same histogram as an existing image:
>>> from machinevisiontoolbox import Image >>> img = Image.Read("street.png") >>> h = img.hist() >>> h.plot('pdf') >>> img2 = Image.Random(size=img.size, pdf=h.pdf) >>> img2.hist().plot('pdf')
(
Source code,png,hires.png,pdf)
See also