collagist 1.0.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 +7 -0
- data/lib/collagist.rb +107 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 716f5e47630a5448378fe5cded457bf85dd99067
|
4
|
+
data.tar.gz: abcac1ecde9fa13f69b4290368bac5b2f57518a9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7ac1ec374c8770aaa27cd32ecfd838e17c1f1739de394427b79937c902e57ee1c5104e3afa01d4f54b62d4157b233203be374f0ff75b93bbcd4018d1b88d50e2
|
7
|
+
data.tar.gz: 9dc028059a609f0d40b7a4e1a3855492d47f03954ae7b6f33e48bc64281c2d7b226ce16518d92a1925ce677decedf2a42d31aeb2c16728b3bdc6c6bae6d43efb
|
data/lib/collagist.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'RMagick'
|
2
|
+
|
3
|
+
class Collagist
|
4
|
+
attr_accessor :outfile, :max_width, :border_width, :background_colour
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@outfile = "out.png"
|
8
|
+
@max_width = 1200
|
9
|
+
@right_most_point = 0
|
10
|
+
@border_width = 2
|
11
|
+
@background_colour = "#333"
|
12
|
+
end
|
13
|
+
|
14
|
+
def build_from_folder(path)
|
15
|
+
@images = Dir[path]
|
16
|
+
build
|
17
|
+
end
|
18
|
+
|
19
|
+
def build_from_filenames(paths_array)
|
20
|
+
@images = paths_array
|
21
|
+
build
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def build
|
26
|
+
@new_image_height = get_mean_height
|
27
|
+
prepare_canvas
|
28
|
+
|
29
|
+
x_off, row = 0, 0
|
30
|
+
|
31
|
+
# Handle entries one by one
|
32
|
+
@images.sort.each do |entry|
|
33
|
+
content = File.open(entry).read
|
34
|
+
|
35
|
+
img = Magick::Image::from_blob(content)[0]
|
36
|
+
|
37
|
+
width = img.columns
|
38
|
+
height = img.rows
|
39
|
+
|
40
|
+
# scale old image to new height
|
41
|
+
factor = @new_image_height.to_f / height
|
42
|
+
new_image_width = (width * factor).to_i
|
43
|
+
|
44
|
+
img = img.resize_to_fill!(new_image_width, @new_image_height)
|
45
|
+
|
46
|
+
# add it to canvas
|
47
|
+
if x_off + new_image_width + @border_width > @max_width
|
48
|
+
row += 1
|
49
|
+
x_off = 0
|
50
|
+
end
|
51
|
+
|
52
|
+
required_height = ((row + 1) * @new_image_height + (row + 1) * @border_width + @border_width).ceil.to_i
|
53
|
+
|
54
|
+
if @canvas.rows < required_height
|
55
|
+
@canvas = @canvas.extent(@max_width, required_height)
|
56
|
+
end
|
57
|
+
|
58
|
+
@canvas.composite!(
|
59
|
+
img,
|
60
|
+
x_off + @border_width,
|
61
|
+
(row*@new_image_height + (row+1)*@border_width).to_i,
|
62
|
+
Magick::OverCompositeOp
|
63
|
+
)
|
64
|
+
|
65
|
+
x_off += new_image_width + @border_width
|
66
|
+
|
67
|
+
if x_off > @right_most_point
|
68
|
+
@right_most_point = x_off
|
69
|
+
end
|
70
|
+
|
71
|
+
img.destroy!
|
72
|
+
GC.start
|
73
|
+
end
|
74
|
+
|
75
|
+
trim_image
|
76
|
+
|
77
|
+
outfile = File.open(@outfile, 'w')
|
78
|
+
outfile.write(@canvas.to_blob)
|
79
|
+
outfile.close
|
80
|
+
end
|
81
|
+
|
82
|
+
def prepare_canvas
|
83
|
+
@canvas = Magick::Image.new(@max_width, 1)
|
84
|
+
@canvas.color_reset!(@background_colour.to_s)
|
85
|
+
@canvas.background_color = @background_colour.to_s
|
86
|
+
@canvas.format = "JPEG"
|
87
|
+
end
|
88
|
+
|
89
|
+
def get_mean_height
|
90
|
+
total = 0
|
91
|
+
@images.each do |entry|
|
92
|
+
content = File.open(entry).read
|
93
|
+
image = Magick::Image::from_blob(content)[0]
|
94
|
+
total += image.rows
|
95
|
+
end
|
96
|
+
new_height = (total / @images.size).to_i
|
97
|
+
new_height = [300, new_height].min
|
98
|
+
return new_height
|
99
|
+
end
|
100
|
+
|
101
|
+
def trim_image
|
102
|
+
@canvas.resize_to_fill!(
|
103
|
+
@right_most_point + @border_width,
|
104
|
+
@canvas.rows,
|
105
|
+
Magick::NorthWestGravity)
|
106
|
+
end
|
107
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: collagist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jsrn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rmagick
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.13'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.13.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.13'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.13.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rspec
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '3.1'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 3.1.0
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.1'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 3.1.0
|
53
|
+
description: Attractive tiling of images from a path name or array of filenames.
|
54
|
+
email: james.srn@gmail.com
|
55
|
+
executables: []
|
56
|
+
extensions: []
|
57
|
+
extra_rdoc_files: []
|
58
|
+
files:
|
59
|
+
- lib/collagist.rb
|
60
|
+
homepage: https://github.com/jsrn/collagist
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.4.5
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Attractive image tiling.
|
84
|
+
test_files: []
|