Skip to content

Instantly share code, notes, and snippets.

@Coro365

Coro365/waifu2x.rb

Last active Jul 14, 2018
Embed
What would you like to do?
This script can process multiple files and directories with waifu2x-converter-cpp
require "ruby-progressbar"
require "shellwords"
require "find"
include Find
WAIF2X_CONVERTER_CPP_DIR = __dir__
WAIF2X_CONVERTER_CPP = WAIF2X_CONVERTER_CPP_DIR + "/waifu2x-converter-cpp"
def waifu2x
files = get_file_list(ARGV, ["jpeg","jpg"])
puts("waifu2x-converter-cpp")
puts("Convreting 0 of #{files.size} items: ")
pgbar = ProgressBar.create(:total => files.size, :format => '%e [%w>>%i] %a')
files.each_with_index do |file, i|
status(file, files.size, i)
Dir.chdir(WAIF2X_CONVERTER_CPP_DIR) do
file = Shellwords.escape(file)
system("(#{WAIF2X_CONVERTER_CPP} -i #{file}) > /dev/null")
end
pgbar.increment
end
end
def status(file, n, i)
printf("\e[1A") #up
print ("Convreting #{i+1} of #{n} item: #{File.basename(file)}".ljust(100))
printf("\r\e[1B") #begining of line and down
end
def get_file_list(path_list, file_type)
file_list = Array.new
path_list.each do |path|
path = File.expand_path(path)
if FileTest.directory? path
find(path){|e| file_list.push(e)}
elsif FileTest.file? path
file_list.push(path)
else
raise 'file path error!: #{path}'
end
end
#ignore dotfile
file_list.reject! { |e| File.basename(e) =~ /^\./ }
unless file_type.empty?
file_list.reject! { |e| File.extname(e) !~ Regexp.union(file_type) }
end
return file_list
end
waifu2x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.