Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require "optparse"
- require "fileutils"
- require "pathname"
- options = {}
- OptionParser.new do |opts|
- opts.banner = "Usage: #{$0} volume[.zip] volume.pdf"
- opts.on("-o", "--output-file OUTPUT", "Name of volume file.") { |o| options[:output_file] = o }
- opts.on("-n", "--volume-number NUMBER", "Number of Jojo volume.") { |o| options[:volume_number] = o }
- end.parse!
- # Default options
- options[:output_file] ||= "jj_volume.pdf"
- options[:volume_number] ||= "x"
- TMP_IMGS_DIR = options[:tmp_imgs_dir] ||= "tmp_imgs"
- EXT = options[:ext] ||= "jpg"
- pages_location = "#{Dir.home}/tmp/volume_#{options[:volume_number]}"
- if ARGV.size == 1 && File.extname(ARGV[0]) == ".zip"
- `unzip -d #{pages_location} #{ARGV[0]}`
- puts "Unzipped #{ARGV[0]}"
- Dir.chdir(pages_location)
- FileUtils.remove_dir("__MACOSX") if Dir.exist?("__MACOSX")
- elsif ARGV.size == 1 && File.directory?(ARGV[0])
- Dir.chdir(ARGV[0])
- else
- $stderr.puts "Image jpgs must be in a directory or zip file."
- end
- Dir.mkdir(TMP_IMGS_DIR) unless Dir.exist?(TMP_IMGS_DIR)
- extracted_dirs = Pathname.new(pages_location)
- .children.select { |c| c.directory? }
- .collect { |p| p.to_s }
- extracted_dirs.each do |dir_name|
- Dir.foreach(dir_name) do |item|
- next if item == "." || item == ".."
- FileUtils.mv("#{dir_name}/#{item}", ".")
- end
- puts "Moved files out of #{dir_name}."
- Dir.rmdir(dir_name)
- puts "Deleted empty #{dir_name}."
- end
- imgs = `ls -v *.#{EXT}`.split("\n")
- formatted_img_num = 0
- imgs.each do |img|
- img_dimensions = `identify #{img}`.split(" ")[2]
- # Jojolion colored img_dimensions == "1773x1400" or "1774x1400"
- if img_dimensions == "1774x1400"
- `convert -crop 50%x100% +repage #{img} #{TMP_IMGS_DIR}/tmp_img.#{EXT}`
- # Switches image order because manga is designed to be read right to left
- File.rename("#{TMP_IMGS_DIR}/tmp_img-1.#{EXT}", "#{TMP_IMGS_DIR}/formatted_img-#{formatted_img_num}.#{EXT}")
- File.rename("#{TMP_IMGS_DIR}/tmp_img-0.#{EXT}", "#{TMP_IMGS_DIR}/formatted_img-#{formatted_img_num += 1}.#{EXT}")
- else
- File.rename(img, "#{TMP_IMGS_DIR}/formatted_img-#{formatted_img_num}.#{EXT}")
- end
- formatted_img_num += 1
- puts "Converted #{img}..."
- end
- formatted_imgs = Dir.glob("#{TMP_IMGS_DIR}/formatted_img-*")
- .sort_by { |f| f.split("-").last.to_i }.join(" ")
- # formatted_imgs = `ls -v #{TMP_IMGS_DIR}/formatted_img-*`.split("\n").join(" ")
- if ARGV.size == 2
- `convert #{formatted_imgs} ../#{ARGV[1]}`
- else
- `convert #{formatted_imgs} ../jj_volume.pdf`
- end
- # puts `ls -v #{TMP_IMGS_DIR}`
- unless $? == 0
- puts "Something went wrong with converting the formatted images to a pdf!"
- return 1
- end
- puts "Jojo volume converted."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement