lib/dnn/image.rb in ruby-dnn-1.1.5 vs lib/dnn/image.rb in ruby-dnn-1.1.6

- old
+ new

@@ -62,9 +62,22 @@ raise ImageWriteError, "Extension '#{ext}' is not support." end raise ImageWriteError, "Image write failed." if res == 0 end + # Create an image from binary. + # @param [String] bin binary data. + # @param [Integer] height Image height. + # @param [Integer] width Image width. + # @param [Integer] channel Image channel. + def self.from_binary(bin, height, width, channel = DNN::Image::RGB) + expected_size = height * width * channel + unless bin.size == expected_size + raise ImageError, "binary size is #{bin.size}, but expected binary size is #{expected_size}" + end + Numo::UInt8.from_binary(bin).reshape(height, width, channel) + end + # Resize the image. # @param [Numo::UInt8] img Image to resize. # @param [Integer] out_height Image height to resize. # @param [Integer] out_width Image width to resize. def self.resize(img, out_height, out_width)