Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require "pathname"
- require "tempfile"
- def strip_alpha!(input_path, output_path)
- bmp = File.open(input_path, "rb").read
- header = bmp[0, 54]
- image = bmp[54..-3]
- final = bmp[-2..-1]
- raise unless image.size % 4 # WTF last 2 bytes
- image_out = ""
- (image.size/4).times do |i|
- r,g,b,a = image[i*4, 4].unpack("CCCC")
- image_out << [r,g,b,255].pack("CCCC")
- end
- open(output_path, "wb") do |fh|
- fh.write header
- fh.write image_out
- fh.write final
- end
- end
- def convert_screenshot!(input_path)
- output_path = Pathname(File.basename(input_path, ".bmp") + ".png")
- input_path = Pathname(input_path)
- return if output_path.exist?
- mid_file = Tempfile.new(['foo', '.bmp'])
- strip_alpha!(input_path, mid_file.path)
- system "convert", mid_file.path, output_path.to_s
- end
- ARGV.each do |path|
- convert_screenshot!(path)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement