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
-
.imread(filename) ⇒ Numo::UInt8
Loads an image from file.
-
.imsave(filename, image, quality: nil) ⇒ Boolean
Saves an image to file.
Class Method Details
.imread(filename) ⇒ Numo::UInt8
Loads an image from file.
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.
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 |