class CrImage::GrayscaleImage

Overview

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

Included Modules

Extended Modules

Defined in:

cr-image/grayscale_image.cr
jpeg.cr
pluto.cr
png.cr
stumpy.cr
webp.cr

Constructors

Instance Method Summary

Instance methods inherited from module CrImage::Format::WebP

to_webp(io : IO, *, lossy : Bool = false, quality : Int32 = 100) : Nil to_webp

Instance methods inherited from module CrImage::Format::JPEG

to_jpeg(io : IO, quality : Int32 = 100) : Nil to_jpeg

Instance methods inherited from module CrImage::Operation::MaskApply

apply(mask : Mask) : self
apply(mask : Mask, &block : UInt8, ChannelType, Int32, Int32 -> UInt8 | Nil) : self
apply
, apply!(mask : Mask) : self
apply!(mask : Mask, &block : UInt8, ChannelType, Int32, Int32 -> UInt8 | Nil) : self
apply!
, apply_color(mask : Mask, color : Color) : self apply_color, apply_color!(mask : Mask, color : Color) : self apply_color!

Instance methods inherited from module CrImage::Format::Save

save(filename : String) : self save

Instance methods inherited from module CrImage::Operation::VerticalBlur

vertical_blur(value : Int32) : self vertical_blur, vertical_blur!(value : Int32) : self vertical_blur!

Instance methods inherited from module CrImage::Operation::Pad

pad(all : Int32 = 0, *, top : Int32 = 0, bottom : Int32 = 0, left : Int32 = 0, right : Int32 = 0, pad_type : EdgePolicy = EdgePolicy::Black) : self pad, pad!(all : Int32 = 0, *, top : Int32 = 0, bottom : Int32 = 0, left : Int32 = 0, right : Int32 = 0, pad_type : EdgePolicy = EdgePolicy::Black) : self pad!

Instance methods inherited from module CrImage::Operation::Rotate

rotate(degrees : Float64, *, center_x : Int32 = width // 2, center_y : Int32 = height // 2, radius : Int32 = -1, pad : Bool = false, pad_type : EdgePolicy = EdgePolicy::Black) : self rotate, rotate!(degrees : Float64, *, center_x : Int32 = width // 2, center_y : Int32 = height // 2, radius : Int32 = -1, pad : Bool = false, pad_type : EdgePolicy = EdgePolicy::Black) : self rotate!

Instance methods inherited from module CrImage::Operation::HorizontalBlur

horizontal_blur(k : Int32) : self horizontal_blur, horizontal_blur!(k : Int32) : self horizontal_blur!

Instance methods inherited from module CrImage::Operation::HistogramEqualize

histogram(channel_type : ChannelType) : Histogram histogram, histogram_equalize : self histogram_equalize, histogram_equalize! : self histogram_equalize!

Instance methods inherited from module CrImage::Operation::GaussianBlur

gaussian_blur(sigma : Int32) : self gaussian_blur, gaussian_blur!(sigma : Int32) : self gaussian_blur!

Instance methods inherited from module CrImage::Operation::Draw

draw_circle(x : Int, y : Int, radius : Int, color : Color, *, fill : Bool = false) : self
draw_circle(region : Region, radius : Int, color : Color, *, fill : Bool = false) : self
draw_circle
, draw_circle!(center_x : Int, center_y : Int, radius : Int, color : Color, *, fill : Bool = false) : self
draw_circle!(region : Region, color : Color, *, fill : Bool = false, radius : Int32 | Nil = nil) : self
draw_circle!
, draw_line(x1 : Int32, y1 : Int32, x2 : Int32, y2 : Int32, color : Color) : self draw_line, draw_line!(x1 : Int32, y1 : Int32, x2 : Int32, y2 : Int32, color : Color) : self draw_line!, draw_square(x : Int, y : Int, box_width : Int, box_height : Int, color : Color, *, fill : Bool = false) : self
draw_square(region : Region, color : Color, *, fill : Bool = false) : self
draw_square
, draw_square!(x : Int, y : Int, box_width : Int, box_height : Int, color : Color, *, fill : Bool = false) : self
draw_square!(region : Region, color : Color, *, fill : Bool = false) : self
draw_square!

Instance methods inherited from module CrImage::Operation::Crop

[](xrange : Range, yrange : Range) : self [], crop(x : Int32, y : Int32, new_width : Int32, new_height : Int32) : self
crop(region : Region) : self
crop
, crop!(x : Int32, y : Int32, new_width : Int32, new_height : Int32) : self
crop!(region : Region) : self
crop!

Instance methods inherited from module CrImage::Operation::Contrast

contrast(value : Float64) : self contrast, contrast!(value : Float64) : self contrast!

Instance methods inherited from module CrImage::Operation::ChannelSwap

channel_swap(a : ChannelType, b : ChannelType) : self channel_swap, channel_swap!(a : ChannelType, b : ChannelType) : self channel_swap!

Instance methods inherited from module CrImage::Operation::Brightness

brightness(value : Float64) : self brightness, brightness!(value : Float64) : self brightness!

Instance methods inherited from module CrImage::Operation::BoxBlur

box_blur(value : Int32) : self box_blur, box_blur!(value : Int32) : self box_blur!

Instance methods inherited from module CrImage::Operation::BilinearResize

bilinear_resize(width : Int32, height : Int32) : self bilinear_resize, bilinear_resize!(width : Int32, height : Int32) : self bilinear_resize!

Instance methods inherited from module CrImage::Format::PNG

to_png(io : IO) : Nil to_png

Instance methods inherited from module CrImage::Format::PPM

to_ppm(io : IO) : Nil to_ppm

Instance methods inherited from class CrImage::Image

[](channel_type : ChannelType) : Array(UInt8) [], []=(channel_type : ChannelType, channel : Array(UInt8)) : Array(UInt8) []=, alpha : Array(UInt8) alpha, blue : Array(UInt8) blue, each_channel(& : Array(UInt8), ChannelType -> Nil) : Nil each_channel, each_color_channel(& : Array(UInt8), ChannelType -> Nil) : Nil each_color_channel, green : Array(UInt8) green, height : Int32 height, red : Array(UInt8) red, size : Int32 size, width : Int32 width

Macros inherited from class CrImage::Image

subsclasses_include(mod) subsclasses_include

Constructor Detail

def self.from_jpeg(image_data : Bytes) : self #

Read image_data as JPEG encoded bytes


def self.from_jpeg(io : IO) : self #

Construct an Image from reading in bytes from io


def self.from_png(image_data : Bytes) : self #

Read image_data and PNG encoded bytes


def self.from_png(io : IO) : self #

Construct an Image by reading bytes from io


def self.from_ppm(image_data : Bytes) : self #

Read image_data as PPM encoded bytes


def self.from_ppm(io : IO) : self #

Read bytes from io as PPM encoded


def self.from_webp(image_data : Bytes) : self #

Read image_data as WebP encoded bytes


def self.from_webp(io : IO) : self #

Read bytes from io as WebP encoded


def self.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)


[View source]
def self.new(gray : Array(UInt8), alpha : Array(UInt8), width : Int32, height : Int32) #

[View source]
def self.new(gray : Array(UInt8), width : Int32, height : Int32) #

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


[View source]
def self.new(gray : Array(UInt8), width : Int32) #

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


[View source]

Instance Method Detail

def <(*args, **options) #

[View source]
def <(*args, **options, &) #

[View source]
def <=(arg) #

[View source]
def ==(arg) #
Description copied from class Reference

Returns false (other can only be a Value here).


[View source]
def >(*args, **options) #

[View source]
def >(*args, **options, &) #

[View source]
def >=(arg) #

[View source]
def [](x : Int32, y : Int32) : Pixel #

Return a Pixel representing this cell in the image.


[View source]
def [](channel_type : ChannelType) : Array(UInt8) #

Return the Array(UInt8) corresponding to channel_type


[View source]
def []=(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.


[View source]
def alpha : Array(UInt8) #

Return #alpha channel


[View source]
def alpha=(alpha : Array(UInt8)) #

[View source]
def blue : Array(UInt8) #

Return the "#blue" channel (returns #gray)


[View source]
def clone : GrayscaleImage #

Create a new GrayscaleImage as a copy of this one


[View source]
def cross_correlate(map : Map, *, edge_policy : EdgePolicy = EdgePolicy::Repeat) : FloatMap #

[View source]
def cross_correlate(*args, **options) #

[View source]
def cross_correlate(*args, **options, &) #

[View source]
def cross_correlate_fft(map : Map) #

[View source]
def each_channel(& : Array(UInt8), ChannelType -> Nil) : Nil #

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


[View source]
def each_color_channel(& : Array(UInt8), ChannelType -> Nil) : Nil #

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


[View source]
def gray : Array(UInt8) #

[View source]
def gray=(gray : Array(UInt8)) #

[View source]
def green : Array(UInt8) #

Return the "#green" channel (returns #gray)


[View source]
def height : Int32 #
Description copied from class CrImage::Image

Height of image


[View source]
def height=(height : Int32) #

[View source]
def 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

[View source]
def 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

[View source]
def mask_from(*args, **options) #

[View source]
def mask_from(*args, **options, &) #

[View source]
def mean(*args, **options) #

[View source]
def mean(*args, **options, &) #

[View source]
def red : Array(UInt8) #

Return the "#red" channel (returns #gray)


[View source]
def size : Int32 #

Return the number of pixels this image contains


[View source]
def 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

[View source]
def to_a : Array(UInt8) #

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


[View source]
def to_gray : GrayscaleImage #

Returns self


[View source]
def to_imap : IntMap #

Convert this image into a IntMap


[View source]
def to_map! : UInt8Map #

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


[View source]
def to_pluto : Pluto::ImageGA #

[View source]
def 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

[View source]
def to_rgba : RGBAImage #

Convert this GrayscaleImage to an RGBAImage.

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


[View source]
def to_stumpy : StumpyCore::Canvas #

[View source]
def width : Int32 #
Description copied from class CrImage::Image

Width of image


[View source]
def width=(width : Int32) #

[View source]