slice_rename 0.3.0 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d9ccdfae67abd4149444be16e8d5ab35f8c34ff2
4
- data.tar.gz: 09649ed5652c83d1b4554bc39565574daae3d2eb
3
+ metadata.gz: 7a30f22e2c72925fe806e794b4da03e2ce8ee51b
4
+ data.tar.gz: 11a40905eddc45d4cc4e3a6cf3f5917c3a92ca7f
5
5
  SHA512:
6
- metadata.gz: 0e62eda7b7534ac40c6ec42099c947c262fa299730eb78b4dd294de4f85ea7f6bedd2c5f39c7065c8537ff2507c5aaea5fae0faded4ab5f74fd7fe8c18a2a8bf
7
- data.tar.gz: 1bdafecf17d09c8a739fab7d9aeff36bd6e3b73fe203483305771f521d5ec8d863bb16bfe194c88778f5d53be172330bae6fb6857ed7ddfcf7e4d9199c9e8ede
6
+ metadata.gz: a787e7d28d6acf3261455194ce80bc1db732feef67babec7d9e0f31c6c4baa13c1f4371bf20abe8623e25980490c7ebcc115b68a6643b4fc71aed382076fdace
7
+ data.tar.gz: dbee5b26778837f1a34cb83731eea93313b5980b2fbba6bcf92d44025e2e43e536a0f18d1ac64bd4848fc9732d09d59144a4d8f3b4c3d2a26e96d6bce64f341f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slice_rename (0.2.0)
4
+ slice_rename (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -9,6 +9,7 @@ which aren't useful for anything.
9
9
 
10
10
  ```
11
11
  -c, --config MANDATORY Configuration file (see below)
12
+ -k, --combine Combine all slices to one image
12
13
  -d, --debug Output debug info
13
14
  -v, --version Display the version
14
15
  -h, --help Display this message
@@ -19,6 +20,17 @@ See the example which is located at `spec/fixtures/config_example.yml`. The
19
20
  suffixes aren't mandatory, if you skip them or don't provide enough it will use
20
21
  an auto generated suffix instead (`_00`).
21
22
 
23
+ The option called `collapse_padding` specifies if the padding between images
24
+ are collapsed into eachother. Meaning that if the `padding` setting is 2 and
25
+ `collapse_padding` is true then it expects all images to be separated by 2
26
+ pixels. If `collapse_padding` is false it will instead expect 2 pixels around
27
+ each image resulting in 4 pixels between images but only 2 around the whole
28
+ image.
29
+
30
+ Images created by slice_rename do not collapse the padding so
31
+ `collapse_padding` defaults to `false`. If you need the old behaviour simply
32
+ set it to `true` in your config file.
33
+
22
34
  The sample image is part of a duty free asset pack from
23
35
  [kenney.nl](http://kenney.nl/).
24
36
 
data/Rakefile CHANGED
@@ -24,4 +24,12 @@ namespace :run do
24
24
  task :missing do
25
25
  ruby '-I ./lib bin/slice_rename'
26
26
  end
27
+
28
+ task :combine do
29
+ ruby '-I ./lib bin/slice_rename --combine --debug spec/fixtures/animals.png -c spec/fixtures/config_example.yml'
30
+ end
31
+
32
+ task :char do
33
+ ruby '-I ./lib bin/slice_rename spec/fixtures/char4/char4.png -d -c spec/fixtures/config_new.yml'
34
+ end
27
35
  end
@@ -1,5 +1,5 @@
1
1
  require 'slice_rename/cli'
2
2
 
3
3
  module SliceRename
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -2,11 +2,13 @@ require 'optparse'
2
2
 
3
3
  require 'slice_rename/config'
4
4
  require 'slice_rename/slicer'
5
+ require 'slice_rename/combiner'
5
6
 
6
7
  module SliceRename
7
8
  class Cli
8
9
  def self.perform
9
10
  config = SliceRename::Config.new
11
+ combine = false
10
12
 
11
13
  parser = OptionParser.new do |opts|
12
14
  opts.banner = "Usage: file [options]"
@@ -15,6 +17,10 @@ module SliceRename
15
17
  config.load v
16
18
  end
17
19
 
20
+ opts.on("-k", "--combine", "Combine the images instead of slicing them") do |v|
21
+ combine = true
22
+ end
23
+
18
24
  opts.on("-d", "--debug", "Output debug info") do |v|
19
25
  config.debug = true
20
26
  end
@@ -46,7 +52,11 @@ module SliceRename
46
52
  config.path = arguments.first
47
53
  end
48
54
 
49
- SliceRename::Slicer.slice_image config
55
+ if combine
56
+ SliceRename::Combiner.combine_images config
57
+ else
58
+ SliceRename::Slicer.slice_image config
59
+ end
50
60
  end
51
61
  end
52
62
  end
@@ -0,0 +1,54 @@
1
+ require 'mini_magick'
2
+
3
+ module SliceRename
4
+ class Combiner
5
+ def self.combine_images(config)
6
+ extension = File.extname config.path
7
+ name = File.basename config.path, extension
8
+ path = File.dirname config.path
9
+
10
+ images = []
11
+ output_name = "#{path}/#{name}#{extension}"
12
+ geometry = "#{config.width}x#{config.height}\>+0+0"
13
+ border = "#{config.padding}x#{config.padding}"
14
+ tile = "#{config.columns}x#{config.rows}"
15
+
16
+ config.suffixes.each do |suffix|
17
+ if suffix != nil
18
+ images << "#{path}/#{name}#{suffix}#{extension}"
19
+ else
20
+ images << 'null:'
21
+ end
22
+ end
23
+
24
+ if config.debug
25
+ puts "Input: #{images.join(', ')}"
26
+ puts "Output: #{output_name}"
27
+ puts "Geometry: #{geometry}"
28
+ end
29
+
30
+ save_combination(images, output_name, geometry, tile, border)
31
+ end
32
+
33
+ private
34
+
35
+ def self.open_image(path)
36
+ MiniMagick::Image.open path
37
+ end
38
+
39
+ def self.save_combination(input_names, output_name, geometry, tile, border)
40
+ MiniMagick::Tool::Montage.new do |montage|
41
+ input_names.each do |file_name|
42
+ montage << file_name
43
+ end
44
+
45
+ montage.geometry geometry
46
+ montage.tile tile
47
+ montage.border border
48
+ montage.bordercolor "none"
49
+ montage.background "none"
50
+ montage << output_name
51
+ end
52
+ end
53
+ end
54
+ end
@@ -3,7 +3,7 @@ require 'yaml'
3
3
  module SliceRename
4
4
  class Config
5
5
  attr_accessor :path, :debug
6
- attr_reader :rows, :columns, :width, :height, :padding
6
+ attr_reader :rows, :columns, :width, :height, :padding, :collapse_padding
7
7
 
8
8
  def initialize
9
9
  @path = ''
@@ -14,6 +14,7 @@ module SliceRename
14
14
  @height = 16
15
15
  @padding = 0
16
16
  @suffixes = []
17
+ @collapse_padding = false
17
18
  end
18
19
 
19
20
  def load(config_path)
@@ -25,12 +26,13 @@ module SliceRename
25
26
  @height = config.fetch('height', @height)
26
27
  @padding = config.fetch('padding', @padding)
27
28
  @suffixes = config.fetch('suffixes', @suffixes)
29
+ @collapse_padding = config.fetch('collapse_padding', @collapse_padding)
28
30
  end
29
31
 
30
32
  def suffixes
31
33
  count = @rows * @columns
32
34
 
33
- if @suffixes.count < count
35
+ if @suffixes.empty?
34
36
  @suffixes = []
35
37
 
36
38
  for i in 0..(count - 1)
@@ -3,7 +3,6 @@ require 'mini_magick'
3
3
  module SliceRename
4
4
  class Slicer
5
5
  def self.slice_image(config)
6
-
7
6
  extension = File.extname config.path
8
7
  name = File.basename config.path, extension
9
8
  path = File.dirname config.path
@@ -14,15 +13,25 @@ module SliceRename
14
13
  # Not sure why we need to reload the image each time.
15
14
  image = open_image config.path
16
15
 
17
- crop = "#{config.width}x#{config.height}+#{x * config.width + (x + 1) * config.padding}+#{y * config.height + (y + 1) * config.padding}"
18
- output_name = "#{path}/#{name}#{config.suffixes[i]}#{extension}"
16
+ unless config.suffixes[i].nil?
17
+ pos_x = (x * config.width) + (x * config.padding) + ((x + 1) * config.padding)
18
+ pos_y = (y * config.height) + (y * config.padding) + ((y + 1) * config.padding)
19
19
 
20
- if config.debug
21
- puts crop
22
- puts output_name
23
- end
20
+ if config.collapse_padding
21
+ pos_x -= (x * config.padding)
22
+ pos_y -= (y * config.padding)
23
+ end
24
+
25
+ crop = "#{config.width}x#{config.height}+#{pos_x}+#{pos_y}"
26
+ output_name = "#{path}/#{name}#{config.suffixes[i]}#{extension}"
24
27
 
25
- save_slice image, output_name, crop
28
+ if config.debug
29
+ puts "Output: #{output_name}"
30
+ puts "Crop: #{crop}"
31
+ end
32
+
33
+ save_slice image, output_name, crop
34
+ end
26
35
 
27
36
  i += 1
28
37
  end
Binary file
@@ -4,6 +4,7 @@ slice_rename_config:
4
4
  width: 175
5
5
  height: 175
6
6
  padding: 2
7
+ collapse_padding: true
7
8
  suffixes:
8
9
  - _elephant
9
10
  - _giraffe
@@ -0,0 +1,36 @@
1
+ require 'slice_rename/combiner'
2
+
3
+ RSpec.describe SliceRename::Combiner do
4
+ let(:config) { SliceRename::Config.new }
5
+ let(:suffixes) {
6
+ [
7
+ 'apples',
8
+ 'oranges',
9
+ 'pears',
10
+ 'bananas'
11
+ ]
12
+ }
13
+
14
+ before do
15
+ config.instance_variable_set :@path, 'fruits.png'
16
+ config.instance_variable_set :@rows, 2
17
+ config.instance_variable_set :@columns, 2
18
+ config.instance_variable_set :@suffixes, suffixes
19
+ end
20
+
21
+ describe '.combine_images' do
22
+ it 'combines the images and saves them as a single image' do
23
+ expect(SliceRename::Combiner).to(
24
+ receive(:save_combination).exactly(1).times do |input_names, output_name, geometry, tile, border|
25
+ input_names.each_with_index do |name, index|
26
+ expect(name).to include suffixes[index]
27
+ end
28
+
29
+ expect(output_name).to include config.path
30
+ end
31
+ )
32
+
33
+ SliceRename::Combiner.combine_images config
34
+ end
35
+ end
36
+ end
@@ -8,7 +8,7 @@ RSpec.describe SliceRename::Slicer do
8
8
  'potatoes',
9
9
  'carrots',
10
10
  'onions',
11
- 'selery'
11
+ 'celery'
12
12
  ]
13
13
  }
14
14
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slice_rename
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zoee Silcock
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-18 00:00:00.000000000 Z
11
+ date: 2016-09-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Slice an image and give each slice a specific name.
14
14
  email: mrzoee@gmail.com
@@ -30,11 +30,13 @@ files:
30
30
  - bin/slice_rename
31
31
  - lib/slice_rename.rb
32
32
  - lib/slice_rename/cli.rb
33
+ - lib/slice_rename/combiner.rb
33
34
  - lib/slice_rename/config.rb
34
35
  - lib/slice_rename/slicer.rb
35
36
  - slice_rename.gemspec
36
37
  - spec/fixtures/animals.png
37
38
  - spec/fixtures/config_example.yml
39
+ - spec/slice_rename/combiner_spec.rb
38
40
  - spec/slice_rename/config_spec.rb
39
41
  - spec/slice_rename/sanity_spec.rb
40
42
  - spec/slice_rename/slicer_spec.rb