class

CrImage::GrayscaleImage

Inherits CrImage::Format::WebP < CrImage::Format::JPEG < CrImage::Operation::MaskApply < CrImage::Format::Save < CrImage::Operation::VerticalBlur < CrImage::Operation::Pad < CrImage::Operation::Rotate < CrImage::Operation::HorizontalBlur < CrImage::Operation::HistogramEqualize < CrImage::Operation::GaussianBlur < CrImage::Operation::Draw < CrImage::Operation::Crop < CrImage::Operation::Contrast < CrImage::Operation::ChannelSwap < CrImage::Operation::Brightness < CrImage::Operation::BoxBlur < CrImage::Operation::BilinearResize < CrImage::Format::PNG < CrImage::Format::PPM < CrImage::Image < Reference < Object

An image in Grayscale. These image types are the easiest to perform feature and information extraction from, where there is only one channel to examine, and so has methods for constructing Masks from (see #threshold below).

An RGBAImage would become a GrayscaleImage this way:

image.to_gray
Woman with black turtleneck and white background Woman in black turtleneck on white background in grayscale

Constructors

from_jpeg(image_data : Bytes) : self

Read image_data as JPEG encoded bytes

from_jpeg(io : IO) : self

Construct an Image from reading in bytes from io

from_png(image_data : Bytes) : self

Read image_data and PNG encoded bytes

from_png(io : IO) : self

Construct an Image by reading bytes from io

from_ppm(image_data : Bytes) : self

Read image_data as PPM encoded bytes

from_ppm(io : IO) : self

Read bytes from io as PPM encoded

from_webp(image_data : Bytes) : self

Read image_data as WebP encoded bytes

from_webp(io : IO) : self

Read bytes from io as WebP encoded

new(red : Array(UInt8), green : Array(UInt8), blue : Array(UInt8), alpha : Array(UInt8), width : Int32, height : Int32)

Create a GrayscaleImage from a set of color channels (delegates to RGBAImage#to_gray)

Source
new(gray : Array(UInt8), alpha : Array(UInt8), width : Int32, height : Int32)
Source
new(gray : Array(UInt8), width : Int32, height : Int32)

Create a GrayscaleImage with only an Array(UInt8) (alpha channel initialized as 255 throughout)

Source
new(gray : Array(UInt8), width : Int32)

Create a GrayscaleImage with only an Array(UInt8) (alpha channel initialized as 255 throughout)

Source

Instance methods

<(*args, **options)
Source
<(*args, **options, &)
Source
<=(*args, **options)
Source
<=(*args, **options, &)
Source
==(*args, **options)
Source
==(*args, **options, &)
Source
>(*args, **options)
Source
>(*args, **options, &)
Source
>=(*args, **options)
Source
>=(*args, **options, &)
Source
[](x : Int32, y : Int32) : Pixel

Return a Pixel representing this cell in the image.

Source
[](channel_type : ChannelType) : Array(UInt8)

Return the Array(UInt8) corresponding to channel_type

Source
[]=(channel_type : ChannelType, channel : Array(UInt8)) : Array(UInt8)

Set the underlying Array(UInt8) of channel_type to the new channel.

Warning: this method does not check the size of the incoming array, and if it's a different size from what the current image represents, this could break it. We recommend against using this method except for from other methods that will be updating the width and height immediately after.

Source
alpha

Return alpha channel

Source
alpha=(alpha : Array(UInt8))
Source
blue

Return the "blue" channel (returns gray)

Source
clone

Create a new GrayscaleImage as a copy of this one

Source
cross_correlate(map : Map, *, edge_policy : EdgePolicy = EdgePolicy::Repeat) : FloatMap
Source
cross_correlate(*args, **options)
Source
cross_correlate(*args, **options, &)
Source
cross_correlate_fft(map : Map)
Source
each_channel

Run provided block with the ChannelType::Gray channels and channel types.

Source
each_color_channel

Run provided block with the ChannelType::Gray channels and channel types.

Source
gray
Source
gray=(gray : Array(UInt8))
Source
green

Return the "green" channel (returns gray)

Source
height

Height of image

Source
height=(height : Int32)
Source
invert

Invert grayscale pixels (replace each pixel will 255 - p for all p in @gray).

Woman in black turtleneck on white background in grayscale

Becomes

Woman in black turtleneck on white background in inverted grayscale
Source
invert!

Invert grayscale pixels (replace each pixel will 255 - p for all p in @gray). Modifies self.

Woman in black turtleneck on white background in grayscale

Becomes

Woman in black turtleneck on white background in inverted grayscale
Source
mask_from(*args, **options)
Source
mask_from(*args, **options, &)
Source
mean(*args, **options)
Source
mean(*args, **options, &)
Source
red

Return the "red" channel (returns gray)

Source
size

Return the number of pixels this image contains

Source
threshold(threshold : Int) : Mask

Construct a simple threshold Mask containing all pixels with a UInt8 value greater than threshold Given sample image:

Woman with black turtleneck and white background
image
  .to_gray                       # convert color image to grayscale one
  .threshold(128)                # generate a mask using threshold operator
  .to_gray                       # convert mask to grayscale image
  .save("threshold_example.jpg") # save mask as grayscale
Black and white silhouette with background and woman's face as white, hair and sweater black
Source
to_a

Receive a copy of the underlying Array(UInt8) corresponding to the ChannelType::Gray channel

Source
to_gray

Returns self

Source
to_imap

Convert this image into a IntMap

Source
to_map!

Convert this image into a UInt8Map. Does not clone underlying gray channel, both this image and map share the same data.

Source
to_pluto
Source
to_rgba(color_map : Hash(UInt8, Color), *, default : Color = Color.default) : RGBAImage

Convert this grayscale image to an RGBA one using the provided color map.

The provided map should have a key for all 0-255 possible gray pixel values, otherwise the default Color will be used instead (default is black).

colors = 256.times.to_a.map { |i| {i.to_u8, CrImage::Color.random} }.to_h
gray_image.to_rgba(colors).save("to_rgba_color_map_sample.jpg")
Woman in black turtleneck on white background in grayscale

Becomes

Random colored pixels in rough outline of woman
Source
to_rgba

Convert this GrayscaleImage to an RGBAImage.

No color will be provided, all pixels will remain gray.

Source
to_stumpy
Source
width

Width of image

Source
width=(width : Int32)
Source

Nested types