Guest User

Untitled

a guest
Feb 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. ## image_to_aa - from image file to ascii art text
  2. ##
  3. ## - module: image_to_aa
  4. ## config:
  5. ## file: convert file path
  6. ## size_ratio: convert ascii art size ratio (1:normal size, 2:half size)
  7. ##
  8. require 'aalib'
  9.  
  10. def image_to_aa(config, data)
  11. file = config["file"]
  12. size_ratio = config["size_ratio"]
  13. generate_aa(file, size_ratio)
  14. end
  15.  
  16. # アスキーアートを生成
  17. def generate_aa(file, size_ratio)
  18. cmd_name = get_cmd_name(file)
  19. ratio = get_ratio(size_ratio)
  20. cmd = "#{cmd_name} #{file} | ppmtopgm | pgmtopbm | pbmtoascii #{ratio}"
  21. `#{cmd}`
  22. end
  23.  
  24. # 拡張子から変換コマンドを選択
  25. def get_cmd_name(file)
  26. ext = File.extname(file)
  27. if /.*\.(jpg|jpeg|JPG|JPEG)\z/ =~ ext
  28. return "jpegtopnm"
  29. elsif /.*\.(png|PNG)\z/ =~ ext
  30. return "pngtopnm"
  31. elsif /.*\.(gif|GIF)\z/ =~ ext
  32. return "giftopnm"
  33. elsif /.*\.(bmp|BMP)\z/ =~ ext
  34. return "bmptopnm"
  35. else
  36. raise ArgumentError, "file format invalid!!"
  37. end
  38. end
  39.  
  40. # AAサイズ比率選択
  41. def get_ratio(size_ratio)
  42. if size_ratio == 1
  43. return ""
  44. elsif size_ratio == 2
  45. return "-2x4"
  46. else
  47. return ""
  48. end
  49. end
Add Comment
Please, Sign In to add comment