13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/dnn/lib/image_io.rb', line 13
def self.write(file_name, img, quality: 100)
if img.shape.length == 2
img = Numo::UInt8[img, img, img].transpose(1, 2, 0).clone
elsif img.shape[2] == 1
img = img.shape(img.shape[0], img.shape[1])
img = Numo::UInt8[img, img, img].transpose(1, 2, 0).clone
end
h, w, ch = img.shape
bin = img.to_binary
case file_name
when /\.png$/i
stride_in_bytes = w * ch
Stb.stbi_write_png(file_name, w, h, ch, bin, stride_in_bytes)
when /\.bmp$/i
Stb.stbi_write_bmp(file_name, w, h, ch, bin)
when /\.jpg$/i, /\.jpeg/i
Stb.stbi_write_jpg(file_name, w, h, ch, bin, quality)
end
rescue => ex
raise ImageIO::WriteError.new(ex.message)
end
|