Module: Magro::IO

Defined in:
lib/magro/io.rb,
ext/magro/magro.c,
ext/magro/magro.c

Overview

IO module provides functions for input and output of image file.

Class Method Summary collapse

Class Method Details

.imread(filename) ⇒ Numo::UInt8

Loads an image from file.

Parameters:

  • filename (String)

    File path or URL of image file to be loaded. Currently, the following file formats are support: Portbale Network Graphics (*.png) and JPEG files (*.jpeg, *.jpg, *jpe).

Returns:

  • (Numo::UInt8)

    (shape: [height, width, n_channels]) Loaded image.

Raises:

  • (ArgumentError)

    This error is raised when filename is not String.

  • (IOError)

    This error is raised when failed to read image file.

  • (NoMemoryError)

    If memory allocation of image data fails, this error is raised.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/magro/io.rb', line 19

def imread(filename) # rubocop:disable Metrics/AbcSize
  raise ArgumentError, 'Expect class of filename to be String.' unless filename.is_a?(String)

  unless url?(filename)
    return case filename.downcase
           when /\.(jpeg|jpg|jpe)$/
             read_jpg(filename)
           when /\.png$/
             read_png(filename)
           end
  end

  uri = URI.parse(filename)
  ext = File.extname(uri.path).downcase
  raise IOError, 'Failed to detect file extension from given URL.' unless /\.(jpeg|jpg|jpe|png)$/.match?(ext)

  uri.open do |file|
    temp = Tempfile.new(['magro_', ext])
    temp.binmode
    temp.write(file.read)
    temp.rewind
    imread(temp.path)
  end
end

.imsave(filename, image, quality: nil) ⇒ Boolean

Saves an image to file.

Parameters:

  • filename (String)

    Path to image file to be saved. Currently, the following file formats are support: Portbale Network Graphics (*.png) and JPEG files (*.jpeg, *.jpg, *jpe).

  • image (Numo::UInt8)

    (shape: [height, width, n_channels]) Image data to be saved.

  • quality (Integer) (defaults to: nil)

    Quality parameter of jpeg image that takes a value between 0 to 100.

Returns:

  • (Boolean)

    true if file save is successful.

Raises:

  • (ArgumentError)

    If filename is not String or image is not Numo::NArray, this error is raised.

  • (IOError)

    This error is raised when failed to write image file.

  • (NoMemoryError)

    If memory allocation of image data fails, this error is raised.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/magro/io.rb', line 54

def imsave(filename, image, quality: nil)
  raise ArgumentError, 'Expect class of filename to be String.' unless filename.is_a?(String)
  raise ArgumentError, 'Expect class of image to be Numo::NArray.' unless image.is_a?(Numo::NArray)

  if /\.(jpeg|jpg|jpe)$/.match?(filename.downcase)
    unless quality.nil?
      raise ArgumentError, 'Expect class of quality to be Numeric.' unless quality.is_a?(Numeric)
      raise ArgumentError, 'Range of quality value between 0 to 100.' unless quality.between?(0, 100)
    end
    return save_jpg(filename, image, quality)
  end

  return save_png(filename, image) if /\.png$/.match?(filename.downcase)

  false
end