waifu2x 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4470dbafba59b80455cbac8d5b1e704784fba5f3
4
+ data.tar.gz: a7135ee6ebc190dfbe34ef231b619f4cd0dc9e87
5
+ SHA512:
6
+ metadata.gz: 501949667feb47f21b7e648e916198a4aa09a25da0a523da9e149531701bf03e58c91a856964231c8a8142752320a3a41f75efceefeb710c1eeb92eded66688f
7
+ data.tar.gz: 88e8b65d9c265446b40857bc3e9384c743cd71188dbca4e2e51c67aa825730c5c6ae54fdd2d5db89b17f04ea41e264242fbe080d3fb118a3bd1d86b8f58f13d4
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in waifu2x.gemspec
4
+ gemspec
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2015, Nihad Abbasov <mail@narkoz.me>
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+
10
+ 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
+ POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,49 @@
1
+ # Waifu2x [![Build Status](https://travis-ci.org/NARKOZ/waifu2x.svg)](https://travis-ci.org/NARKOZ/waifu2x)
2
+
3
+ Waifu2x is a ruby wrapper and CLI for
4
+ [waifu2x](https://github.com/nagadomi/waifu2x), which provides Noise Reduction
5
+ and 2x Upscaling for anime style images.
6
+
7
+ ## Installation
8
+
9
+ Install it from rubygems:
10
+
11
+ ```sh
12
+ gem install waifu2x
13
+ ```
14
+
15
+ Or add to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'waifu2x'
19
+ ```
20
+
21
+ and run:
22
+
23
+ ```sh
24
+ bundle install
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ # convert image.png with 2x upscaling
31
+ Waifu2x.convert('image.png', nil, { scale: 2 })
32
+
33
+ # convert image.png to converted.png without noise reduction and with 2x upscaling
34
+ Waifu2x.convert('image.png', 'converted.png', { noise: 0, scale: 2 })
35
+ ```
36
+
37
+ ## CLI usage
38
+
39
+ ```sh
40
+ # convert sample.jpg with 2x upscaling
41
+ waifu2x sample.jpg -s 2
42
+
43
+ # convert sample.jpg to output.jpg without noise reduction and with 2x upscaling
44
+ waifu2x sample.jpg output.jpg -n 0 -s 2
45
+ ```
46
+
47
+ ## License
48
+
49
+ Released under the BSD 2-clause license. See LICENSE.txt for details.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'waifu2x'
5
+ require 'pry'
6
+
7
+ Pry.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'waifu2x'
4
+ require 'optparse'
5
+
6
+ options = {}
7
+
8
+ optparse = OptionParser.new do |opts|
9
+ opts.banner = 'Usage: waifu2x image <filename>'
10
+ opts.separator ''
11
+ opts.separator 'Options:'
12
+
13
+ opts.on_tail('-n', '--noise NOISE', 'Noise reduction (0 - none, 1 - low, 2 - high), default: 2') do |noise|
14
+ options[:noise] = noise.to_i
15
+ end
16
+
17
+ opts.on_tail('-s', '--scale SCALE', 'Upscaling (0 - none, 1 - 1.6x, 2 - 2x), default: 0') do |scale|
18
+ options[:scale] = scale.to_i
19
+ end
20
+
21
+ opts.on_tail('-h', '--help', 'Prints this help message') do
22
+ puts opts
23
+ exit
24
+ end
25
+
26
+ opts.on_tail('-v', '--version', 'Prints version') do
27
+ puts "Waifu2x Version #{Waifu2x::VERSION}"
28
+ exit
29
+ end
30
+ end
31
+
32
+ begin
33
+ optparse.parse!
34
+ rescue OptionParser::ParseError
35
+ puts optparse
36
+ exit 1
37
+ end
38
+
39
+ if ARGV.empty?
40
+ puts optparse
41
+ exit 1
42
+ end
43
+
44
+ image = ARGV[0]
45
+ filename = ARGV[1]
46
+ start_time = Time.now.to_i
47
+
48
+ puts 'Processing...'
49
+
50
+ begin
51
+ waifu2x = Waifu2x.convert(image, filename, options)
52
+ rescue Waifu2x::Error => e
53
+ abort e.message
54
+ end
55
+
56
+ finish_time = Time.now.to_i
57
+ duration = finish_time - start_time
58
+
59
+ puts "Upload took ~#{duration} seconds. Created file #{waifu2x}"
@@ -0,0 +1,62 @@
1
+ require 'waifu2x/version'
2
+ require 'waifu2x/errors'
3
+ require 'typhoeus'
4
+
5
+ module Waifu2x
6
+ USER_AGENT = "Waifu2x Ruby Gem #{Waifu2x::VERSION}".freeze
7
+ API_ENDPOINT = 'http://waifu2x.udp.jp/api'.freeze
8
+
9
+ # Creates a new image
10
+ #
11
+ # @param [String] image Image filename to convert
12
+ # @param [String, nil] filename Output image filename with extension
13
+ # @param [Hash] options
14
+ # @option options [Integer] :noise Noise reduction (0 - none, 1 - low, 2 - high), default: 2
15
+ # @option options [Integer] :scale Upscaling (0 - none, 1 - 1.6x, 2 - 2x), default: 0
16
+ # @return [String] Output image filename
17
+ def self.convert(image, filename=nil, options={})
18
+ noise = options[:noise] || 2
19
+ scale = options[:scale] || 0
20
+
21
+ validate_options! noise, scale
22
+
23
+ begin
24
+ file = File.open(image)
25
+ rescue
26
+ raise InvalidImage, "Can't open image file: #{image}"
27
+ end
28
+
29
+ response = Typhoeus.post(API_ENDPOINT,
30
+ headers: { 'User-Agent' => USER_AGENT },
31
+ body: { noise: noise.to_s, scale: scale.to_s, file: file }
32
+ )
33
+
34
+ raise InvalidImage, response.body if response.body == 'ERROR: unsupported image format.'
35
+ raise Waifu2x::ServerError, "Request to Waifu2x API failed with response code: #{response.code}" if response.code != 200
36
+
37
+ filename = output_filename(file) if filename.to_s.strip.empty?
38
+
39
+ File.write(filename, response.body)
40
+ filename
41
+ end
42
+
43
+ # @private
44
+ def self.output_filename(file)
45
+ file_ext = File.extname(file)
46
+ orig_name = File.basename(file, file_ext)
47
+ timestamp = Time.now.to_i
48
+
49
+ "#{orig_name}_#{timestamp}#{file_ext}"
50
+ end
51
+ private_class_method :output_filename
52
+
53
+ # @private
54
+ def self.validate_options!(noise, scale)
55
+ valid_options = [0, 1, 2]
56
+
57
+ unless valid_options.include?(noise) && valid_options.include?(scale)
58
+ raise InvalidArgument, 'Valid noise and scale options: 0, 1 or 2'
59
+ end
60
+ end
61
+ private_class_method :validate_options!
62
+ end
@@ -0,0 +1,13 @@
1
+ module Waifu2x
2
+ # Custom error class for rescuing from all Waifu2x errors.
3
+ class Error < StandardError; end
4
+
5
+ # Raised when invalid image file is passed.
6
+ class InvalidImage < Error; end
7
+
8
+ # Raised when invalid option is passed.
9
+ class InvalidArgument < Error; end
10
+
11
+ # Raised when Waifu2x API server doesn't return the HTTP status code 200 (OK).
12
+ class ServerError < Error; end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Waifu2x
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'waifu2x/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "waifu2x"
8
+ spec.version = Waifu2x::VERSION
9
+ spec.authors = ["Nihad Abbasov"]
10
+ spec.email = ["mail@narkoz.me"]
11
+
12
+ spec.summary = %q{Ruby wrapper and CLI for waifu2x}
13
+ spec.description = %q{Noise Reduction and 2x Upscaling for anime style images}
14
+ spec.homepage = "https://github.com/NARKOZ/waifu2x"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'typhoeus'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "pry", "~> 0.10"
26
+ spec.add_development_dependency "minitest"
27
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: waifu2x
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nihad Abbasov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Noise Reduction and 2x Upscaling for anime style images
84
+ email:
85
+ - mail@narkoz.me
86
+ executables:
87
+ - waifu2x
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - exe/waifu2x
100
+ - lib/waifu2x.rb
101
+ - lib/waifu2x/errors.rb
102
+ - lib/waifu2x/version.rb
103
+ - waifu2x.gemspec
104
+ homepage: https://github.com/NARKOZ/waifu2x
105
+ licenses: []
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.8
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Ruby wrapper and CLI for waifu2x
127
+ test_files: []