Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## image_to_aa - from image file to ascii art text
- ##
- ## - module: image_to_aa
- ## config:
- ## file: convert file path
- ## size_ratio: convert ascii art size ratio (1:normal size, 2:half size)
- ##
- require 'aalib'
- def image_to_aa(config, data)
- file = config["file"]
- size_ratio = config["size_ratio"]
- generate_aa(file, size_ratio)
- end
- # アスキーアートを生成
- def generate_aa(file, size_ratio)
- cmd_name = get_cmd_name(file)
- ratio = get_ratio(size_ratio)
- cmd = "#{cmd_name} #{file} | ppmtopgm | pgmtopbm | pbmtoascii #{ratio}"
- `#{cmd}`
- end
- # 拡張子から変換コマンドを選択
- def get_cmd_name(file)
- ext = File.extname(file)
- if /.*\.(jpg|jpeg|JPG|JPEG)\z/ =~ ext
- return "jpegtopnm"
- elsif /.*\.(png|PNG)\z/ =~ ext
- return "pngtopnm"
- elsif /.*\.(gif|GIF)\z/ =~ ext
- return "giftopnm"
- elsif /.*\.(bmp|BMP)\z/ =~ ext
- return "bmptopnm"
- else
- raise ArgumentError, "file format invalid!!"
- end
- end
- # AAサイズ比率選択
- def get_ratio(size_ratio)
- if size_ratio == 1
- return ""
- elsif size_ratio == 2
- return "-2x4"
- else
- return ""
- end
- end
Add Comment
Please, Sign In to add comment