Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.75 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require "optparse"
  3. require "fileutils"
  4. require "pathname"
  5.  
  6. options = {}
  7. OptionParser.new do |opts|
  8.   opts.banner = "Usage: #{$0} volume[.zip] volume.pdf"
  9.  
  10.   opts.on("-o", "--output-file OUTPUT", "Name of volume file.") { |o| options[:output_file] = o }
  11.   opts.on("-n", "--volume-number NUMBER", "Number of Jojo volume.") { |o| options[:volume_number] = o }
  12. end.parse!
  13.  
  14. # Default options
  15. options[:output_file] ||= "jj_volume.pdf"
  16. options[:volume_number] ||= "x"
  17. TMP_IMGS_DIR = options[:tmp_imgs_dir] ||= "tmp_imgs"
  18. EXT = options[:ext] ||= "jpg"
  19.  
  20. pages_location = "#{Dir.home}/tmp/volume_#{options[:volume_number]}"
  21.  
  22. if ARGV.size == 1 && File.extname(ARGV[0]) == ".zip"
  23.   `unzip -d #{pages_location} #{ARGV[0]}`
  24.   puts "Unzipped #{ARGV[0]}"
  25.   Dir.chdir(pages_location)
  26.   FileUtils.remove_dir("__MACOSX") if Dir.exist?("__MACOSX")
  27. elsif ARGV.size == 1 && File.directory?(ARGV[0])
  28.   Dir.chdir(ARGV[0])
  29. else
  30.   $stderr.puts "Image jpgs must be in a directory or zip file."
  31. end
  32.  
  33. Dir.mkdir(TMP_IMGS_DIR) unless Dir.exist?(TMP_IMGS_DIR)
  34.  
  35. extracted_dirs = Pathname.new(pages_location)
  36.                    .children.select { |c| c.directory? }
  37.                    .collect { |p| p.to_s }
  38. extracted_dirs.each do |dir_name|
  39.   Dir.foreach(dir_name) do |item|
  40.     next if item == "." || item == ".."
  41.  
  42.     FileUtils.mv("#{dir_name}/#{item}", ".")
  43.   end
  44.   puts "Moved files out of #{dir_name}."
  45.  
  46.   Dir.rmdir(dir_name)
  47.   puts "Deleted empty #{dir_name}."
  48. end
  49.  
  50. imgs = `ls -v *.#{EXT}`.split("\n")
  51. formatted_img_num = 0
  52. imgs.each do |img|
  53.   img_dimensions = `identify #{img}`.split(" ")[2]
  54.  
  55.   # Jojolion colored img_dimensions == "1773x1400" or "1774x1400"
  56.   if img_dimensions == "1774x1400"
  57.     `convert -crop 50%x100% +repage #{img} #{TMP_IMGS_DIR}/tmp_img.#{EXT}`
  58.  
  59.     # Switches image order because manga is designed to be read right to left
  60.     File.rename("#{TMP_IMGS_DIR}/tmp_img-1.#{EXT}", "#{TMP_IMGS_DIR}/formatted_img-#{formatted_img_num}.#{EXT}")
  61.     File.rename("#{TMP_IMGS_DIR}/tmp_img-0.#{EXT}", "#{TMP_IMGS_DIR}/formatted_img-#{formatted_img_num += 1}.#{EXT}")
  62.   else
  63.     File.rename(img, "#{TMP_IMGS_DIR}/formatted_img-#{formatted_img_num}.#{EXT}")
  64.   end
  65.  
  66.   formatted_img_num += 1
  67.  
  68.   puts "Converted #{img}..."
  69. end
  70.  
  71. formatted_imgs = Dir.glob("#{TMP_IMGS_DIR}/formatted_img-*")
  72.                    .sort_by { |f| f.split("-").last.to_i }.join(" ")
  73. # formatted_imgs = `ls -v #{TMP_IMGS_DIR}/formatted_img-*`.split("\n").join(" ")
  74. if ARGV.size == 2
  75.   `convert #{formatted_imgs} ../#{ARGV[1]}`
  76. else
  77.   `convert #{formatted_imgs} ../jj_volume.pdf`
  78. end
  79. # puts `ls -v #{TMP_IMGS_DIR}`
  80. unless $? == 0
  81.   puts "Something went wrong with converting the formatted images to a pdf!"
  82.   return 1
  83. end
  84.  
  85. puts "Jojo volume converted."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement