douyin_image 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e4088cc1ef94e00f058e41880fd6854bf25bce7ce4b6fb037ffb942226891040
4
+ data.tar.gz: 07e1a172c1d86bbff4a8aa8dc953c4450033d85f177664047939b0a7cb4bb6ea
5
+ SHA512:
6
+ metadata.gz: f1b0c0a6e025668074a51ada46a91e5e3daf11d23d24ab262ded310e467b8c40001e872ec270b205af3aa5f8b63191d1d5e50d57f41b781c1c1afb30af50edda
7
+ data.tar.gz: b9576bca23321152651df2db941606da1183ecc01bc191b9f49f38ffb0da348494b430e1fa49e4f99d84bcf449bf7ec695806bb6392df551f9aa7d25a6118f52
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /.idea/
9
+ /.DS_Store
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler
6
+ install: bundle install
7
+ script:
8
+ - bundle exec rake test
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at lanyuejin1108@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in douyin_image.gemspec
6
+
7
+ gem 'ruby-vips', '~> 2.0.13'
8
+
9
+ gemspec
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ douyin_image (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ffi (1.9.25)
10
+ minitest (5.11.3)
11
+ rake (10.5.0)
12
+ ruby-vips (2.0.13)
13
+ ffi (~> 1.9)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ bundler (~> 1.16)
20
+ douyin_image!
21
+ minitest (~> 5.0)
22
+ rake (~> 10.0)
23
+ ruby-vips (~> 2.0.13)
24
+
25
+ BUNDLED WITH
26
+ 1.16.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,101 @@
1
+ # DouyinImage
2
+
3
+ 生成抖音风格的图片。Have Fun!😊
4
+
5
+ ## 安装
6
+
7
+ 在你的Gemfile中添加下列代码:
8
+
9
+ ```ruby
10
+ gem 'douyin_image'
11
+ ```
12
+
13
+ 然后执行下列命令:
14
+
15
+ ```shell
16
+ $ bundle
17
+ ```
18
+
19
+ 或者是使用下列命令直接安装gem:
20
+
21
+ ```shell
22
+ $ gem install douyin_image
23
+ ```
24
+
25
+ ## 使用
26
+
27
+ ### 方法调用
28
+
29
+ 当你需要在项目中使用时,可以通过下列代码进行调用:
30
+
31
+ ```ruby
32
+ # generate Douyin style image with the input image
33
+ #
34
+ # @param file_path String - image file path
35
+ # @param output_path String - output directory, default: same with input file
36
+ # options:
37
+ # offset: the R channel image offset, default: 8
38
+ # part_count: the small part's count, default: rand(5..10)
39
+ # part_width: the small part's width, default: rand(10..30)
40
+ # part_height: the small part's height, default: rand(10..30)
41
+ #
42
+ # Example:
43
+ # DouyinImage.generate('./test.jpg', './test/', {offset: 10})
44
+ #
45
+
46
+ DouyinImage.generate(file_path, output_path=nil, options={})
47
+ ```
48
+
49
+ ### 命令行
50
+
51
+ 你也可以通过下列命令查看支持的选项以及操作
52
+
53
+ ```shell
54
+ $ douyin --help
55
+
56
+ Usage: douyin [options]
57
+
58
+ Specific options:
59
+ -i, --input IMAGE_PATH the input image's path
60
+ -o, --output [OUTPUT_PATH] the custom output path
61
+ -f, --offset [OFFSET] the R channel image offset
62
+ -c, --count [COUNT] the small part's count
63
+ -w, --width [WIDTH] the small part's width
64
+ -h, --height [HEIGHT] the small part's height
65
+
66
+ Common options:
67
+ --help Show the help message
68
+ --version Show version
69
+ ```
70
+
71
+
72
+
73
+ ## 例子
74
+
75
+ ```ruby
76
+ 命令行调用:
77
+ $ bundle exec douyin -i './tmp/input.jpg' -o './tmp/' -f 20 -c 10
78
+
79
+ 或者是:
80
+ DouyinImage.generate('./tmp/input.jpg', './tmp/', {offset: 20, part_count: 10})
81
+ ```
82
+
83
+ ### 输入图片
84
+
85
+ ![](./tmp/input.jpg)
86
+
87
+
88
+
89
+ 输出结果
90
+
91
+ ![](./tmp/input_douyin.jpg)
92
+
93
+ ## 如何贡献
94
+
95
+ 1. Fork it
96
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
97
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
98
+ 4. Push to the branch (`git push origin my-new-feature`)
99
+ 5. Create new Pull Request
100
+
101
+ 欢迎贡献相关代码或是反馈使用时遇到的问题👏,另外请记得为你的代码编写测试。
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "douyin_image"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,111 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "optparse"
5
+ require "douyin_image"
6
+
7
+ class DouyinOptparser
8
+
9
+ class Parser
10
+ attr_accessor :input_path,
11
+ :output_path,
12
+ :offset,
13
+ :part_count,
14
+ :part_width,
15
+ :part_height
16
+
17
+ def initialize
18
+ self.input_path = nil
19
+ self.output_path = nil
20
+ self.offset = 8
21
+ self.part_count = rand(5..10)
22
+ self.part_width = rand(10..30)
23
+ self.part_height = rand(10..30)
24
+ end
25
+
26
+ def define_options(parser)
27
+ parser.banner = "Usage: douyin [options]"
28
+ parser.separator ""
29
+ parser.separator "Specific options:"
30
+
31
+ input_path_option(parser)
32
+ output_path_option(parser)
33
+ offset_option(parser)
34
+ part_count_option(parser)
35
+ part_width_option(parser)
36
+ part_height_option(parser)
37
+
38
+ parser.separator ""
39
+ parser.separator "Common options:"
40
+
41
+ parser.on_tail("--help", "Show the help message") do
42
+ puts parser
43
+ exit
44
+ end
45
+ # Another typical switch to print the version.
46
+ parser.on_tail("--version", "Show version") do
47
+ puts DouyinImage::VERSION
48
+ exit
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def input_path_option(parser)
55
+ parser.on("-i", "--input IMAGE_PATH", "the input image's path") do |input|
56
+ self.input_path = input
57
+ end
58
+ end
59
+
60
+ def output_path_option(parser)
61
+ parser.on("-o", "--output [OUTPUT_PATH]", "the custom output path") do |output|
62
+ self.output_path = output
63
+ end
64
+ end
65
+
66
+ def offset_option(parser)
67
+ parser.on("-f", "--offset [OFFSET]", "the R channel image offset") do |offset|
68
+ self.offset = offset
69
+ end
70
+ end
71
+
72
+ def part_count_option(parser)
73
+ parser.on("-c", "--count [COUNT]", "the small part's count") do |count|
74
+ self.part_count = count
75
+ end
76
+ end
77
+
78
+ def part_width_option(parser)
79
+ parser.on("-w", "--width [WIDTH]", "the small part's width") do |width|
80
+ self.part_width = width
81
+ end
82
+ end
83
+
84
+ def part_height_option(parser)
85
+ parser.on("-h", "--height [HEIGHT]", "the small part's height") do |height|
86
+ self.part_height = height
87
+ end
88
+ end
89
+
90
+ end
91
+
92
+ attr_reader :parser, :options
93
+ def parse(args)
94
+ @options = Parser.new
95
+ @args = OptionParser.new do |parser|
96
+ @options.define_options(parser)
97
+ parser.parse!(args)
98
+ end
99
+ @options
100
+ end
101
+ end
102
+
103
+ parser = DouyinOptparser.new
104
+ options = parser.parse(ARGV)
105
+
106
+ DouyinImage.generate(options.input_path, options.output_path, {
107
+ offset: options.offset,
108
+ part_count: options.part_count,
109
+ part_width: options.part_width,
110
+ part_height: options.part_height
111
+ })
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "douyin_image/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "douyin_image"
8
+ spec.version = DouyinImage::VERSION
9
+ spec.authors = ["renyijiu"]
10
+ spec.email = ["lanyuejin1108@gmail.com"]
11
+
12
+ spec.summary = %q{A gem to generate douyin style image.}
13
+ spec.description = %q{A gem to generate douyin style image.Have fun!😊}
14
+ spec.homepage = "https://github.com/renyijiu/douyin_image"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "bin"
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ end
@@ -0,0 +1,47 @@
1
+ require "vips"
2
+ require "douyin_image/image"
3
+ require "douyin_image/version"
4
+
5
+ module DouyinImage
6
+
7
+ class << self
8
+
9
+ # generate Douyin style image with the input image
10
+ #
11
+ # @param file_path String - image file path
12
+ # @param output_path String - output directory, default: same with input file
13
+ # options:
14
+ # offset: the R channel image offset, default: 8
15
+ # part_count: the small part's count, default: rand(5..10)
16
+ # part_width: the small part's width, default: rand(10..30)
17
+ # part_height: the small part's height, default: rand(10..30)
18
+ #
19
+ # Example:
20
+ # DouyinImage.generate('./test.jpg', './test/', {offset: 10})
21
+ #
22
+ def generate(file_path, output_path=nil, options={})
23
+ raise ArgumentError, 'File not exist, please check the file path' unless File.file?(file_path)
24
+ output_path = get_output_filepath(file_path, output_path)
25
+
26
+ Image.new.generate(file_path, output_path, options)
27
+ puts "图片生成完成,去看看吧😊!#{output_path}"
28
+ end
29
+
30
+ protected
31
+
32
+ def get_output_filepath(file_path, output_path)
33
+ output_path ||= File.dirname(file_path)
34
+ raise ArgumentError, 'Output path is not valid' unless File.directory?(output_path)
35
+
36
+ basename = File.basename(file_path)
37
+ filename = basename.gsub(/^(.+?)(\.\w+)$/, '\1_douyin\2')
38
+
39
+ file = File.join(output_path, filename)
40
+ raise ArgumentError, "#{file}, file is exist, change another directory" if File.file?(file)
41
+
42
+ file
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,59 @@
1
+
2
+ module DouyinImage
3
+ class Image
4
+
5
+ def generate(file_path, output_path, options={})
6
+ offset = (options.delete(:offset) || 8).to_i.abs
7
+
8
+ im = Vips::Image.new_from_file(file_path)
9
+ im_r = im * [1, 0, 0] # 获取 R 值数据
10
+ im_gb = im * [0, 1, 1] # 获取 GB 值数据
11
+
12
+ im_r = im_r.embed(offset, offset, im.width, im.height)
13
+
14
+ # 生成图片数据
15
+ target_im = im_r.add(im_gb)
16
+ target_im = random_part_offset(target_im, options)
17
+
18
+ # 优化偏移导致的图片边框颜色问题
19
+ target_im = target_im.embed(-offset, -offset, im.width-offset, im.height-offset)
20
+ target_im.write_to_file(output_path)
21
+ end
22
+
23
+ private
24
+
25
+ def random_part_offset(image, options={})
26
+ parts = get_random_parts(image, options)
27
+
28
+ parts.each do |part|
29
+ x, y, width, height = part
30
+
31
+ crop_mode = [:none, :centre, :entropy, :attention].sample
32
+ im = image.smartcrop(width, height, interesting: crop_mode)
33
+
34
+ image = image.draw_image(im, x, y, mode: :set)
35
+ end
36
+
37
+ image
38
+ end
39
+
40
+ # 随机获取图片切片位置以及大小信息
41
+ def get_random_parts(image, options={})
42
+ # 图片切割份数
43
+ part_count = options.fetch(:part_count, rand(5..10)).to_i.abs
44
+
45
+ part_count.times.map do |index|
46
+ tmp_height = (image.height / part_count).to_i
47
+
48
+ x = rand(1...image.width)
49
+ y = rand(1...tmp_height) + index * tmp_height
50
+
51
+ part_width = options.fetch(:part_width, rand(10..30)).to_i.abs
52
+ part_height = options.fetch(:part_height, rand(10..30)).to_i.abs
53
+
54
+ [x, y, part_width, part_height]
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module DouyinImage
2
+ VERSION = "0.1.0"
3
+ end
Binary file
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: douyin_image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - renyijiu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: "A gem to generate douyin style image.Have fun!\U0001F60A"
56
+ email:
57
+ - lanyuejin1108@gmail.com
58
+ executables:
59
+ - console
60
+ - douyin
61
+ - setup
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/douyin
75
+ - bin/setup
76
+ - douyin_image.gemspec
77
+ - lib/douyin_image.rb
78
+ - lib/douyin_image/image.rb
79
+ - lib/douyin_image/version.rb
80
+ - tmp/.DS_Store
81
+ - tmp/input.jpg
82
+ - tmp/input_douyin.jpg
83
+ - tmp/test.jpg
84
+ homepage: https://github.com/renyijiu/douyin_image
85
+ licenses: []
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.7.6
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: A gem to generate douyin style image.
107
+ test_files: []