Skip to content

Grain Generation

grain_generation

Functions to simulate film grain and to export film grain overlays.

Classes:

  • ExportGrainDialog

    Dialog for exporting random grain overlays as images or video.

Functions:

ExportGrainDialog

ExportGrainDialog(parent=None)

Bases: QDialog

Dialog for exporting random grain overlays as images or video.

gaussian_noise

gaussian_noise(shape, cached=False)

Computes gaussian noise. If cached noise is used a random crop is used to not get identical noise every time.

gaussian_noise_cache cached

gaussian_noise_cache(shape)

Computes and pre caches gaussian noise.

generate_grain

generate_grain(
    shape,
    scale: float,
    grain_size_mm=0.006,
    bw_grain=False,
    cached=False,
    grain_sigma=0.3,
    **kwargs,
) -> ndarray

Computes random grain somewhat efficiently with the grain smoothing inspired by the paper Simulating Film Grain using the Noise-Power Spectrum by Ian Stephenson and Arthur Saunders.

Parameters:

  • shape

    The shape of the output ndarry.

  • scale

    (float) –

    The pixel scale in pixels per mm.

  • grain_size_mm

    The diameter of the individual grains in mm.

  • bw_grain

    Is the grain monochromatic or per channel?

  • cached

    Should cached noise be used for grain generation?

  • grain_sigma

    The grain size distribution standard variance.

  • **kwargs

Returns:

  • ndarray

    An ndarray that is essentially a grain overlay.

generate_grain_frame

generate_grain_frame(
    width, height, channels, scale, grain_size_mm=0.06, std_div=0.001, grain_sigma=0.3
) -> ndarray

Generates a single frame of grain scaled to 8 bit int values.

grain_kernel

grain_kernel(pixel_size_mm: float, grain_size_mm=0.006, grain_sigma=0.3) -> ndarray

Computes a convolution kernel for film grain. Based on the paper Simulating Film Grain using the Noise-Power Spectrum by Ian Stephenson and Arthur Saunders.

Parameters:

  • pixel_size_mm

    (float) –

    The side length of the pixels in mm.

  • grain_size_mm

    The grain diameter in mm.

  • grain_sigma

    The grain size distribution standard variance

output_file

output_file(noise: ndarray, file_format: str, filename: str, ext: str)

Writes the noise array to an image or video file. Formats specified in .file_formats.

two_component_params

two_component_params(
    grain_size_mm: float, sigma: float, p=2
) -> tuple[float, float, float, float]

Return :math:d_1, :math:d_2, :math:w_1, :math:w_2 for a 2-component approximation of :math:LogNormal(\mu, \sigma^2). - :math:d_1 = \exp(\mu - \sigma) - :math:d_2 = \exp(\mu + \sigma) - Split point :math:= geometric mean :math:= \exp(\mu) - Weights :math:w_1, w_2 derived for weighting :math:~ d^p \cdot \text{pdf}(d) (:math:p=0 \rightarrow number weighting, :math:p=2 \rightarrow area weighting, :math:p=3 \rightarrow volume/scattering)

All returned as floats: :math:(d_1, d_2, w_1, w_2).