photo_mosaic 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +44 -0
  3. data/CHANGELOG.md +5 -0
  4. data/CODE_OF_CONDUCT.md +84 -0
  5. data/Gemfile +16 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +52 -0
  8. data/Rakefile +12 -0
  9. data/bin/console +15 -0
  10. data/bin/setup +8 -0
  11. data/docs/.DS_Store +0 -0
  12. data/docs/img/.DS_Store +0 -0
  13. data/docs/img/eltz_flowers.png +0 -0
  14. data/lib/photo_mosaic/image.rb +60 -0
  15. data/lib/photo_mosaic/mini_magick_io.rb +60 -0
  16. data/lib/photo_mosaic/mini_magick_xform.rb +43 -0
  17. data/lib/photo_mosaic/mosaic.rb +55 -0
  18. data/lib/photo_mosaic/progress_bar.rb +26 -0
  19. data/lib/photo_mosaic/version.rb +5 -0
  20. data/lib/photo_mosaic.rb +22 -0
  21. data/photo_mosaic.gemspec +31 -0
  22. data/spec/.DS_Store +0 -0
  23. data/spec/images/.DS_Store +0 -0
  24. data/spec/images/cropped.png +0 -0
  25. data/spec/images/mosaic.png +0 -0
  26. data/spec/images/target.png +0 -0
  27. data/spec/images/tiles/.DS_Store +0 -0
  28. data/spec/images/tiles/squares_bw/black.png +0 -0
  29. data/spec/images/tiles/squares_bw/white.png +0 -0
  30. data/spec/images/tiles/squares_color/blue.png +0 -0
  31. data/spec/images/tiles/squares_color/brass.png +0 -0
  32. data/spec/images/tiles/squares_color/brown.png +0 -0
  33. data/spec/images/tiles/squares_color/green.png +0 -0
  34. data/spec/images/tiles/squares_color/grey.png +0 -0
  35. data/spec/images/tiles/squares_color/orange.png +0 -0
  36. data/spec/images/tiles/squares_color/red.png +0 -0
  37. data/spec/images/tiles/squares_color/white.png +0 -0
  38. data/spec/images/tiles/squares_color/yellow.png +0 -0
  39. data/spec/lib/photo_mosaic/image_spec.rb +41 -0
  40. data/spec/lib/photo_mosaic/local_image_spec.rb +56 -0
  41. data/spec/lib/photo_mosaic/mosaic_spec.rb +24 -0
  42. data/spec/spec_helper.rb +18 -0
  43. metadata +154 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 13f10720acf94a17f2bf43ee60c8a4717b748300bbf12f2204c936ba99230b9a
4
+ data.tar.gz: cea9f388cb21808b34651252f6afe6131ec11c1ed30824c2a262eadb1a711bf7
5
+ SHA512:
6
+ metadata.gz: 57e8f216f453be8fb30dfbad8d36409a743b659bc51d48e7a5f6a62fe321b6907f3e68459b02b96ecb576b37241e7c5153ed2dbec2d9ef3bab0042f53c4004fe
7
+ data.tar.gz: 94b3fdf5ab558cb754f054b7be2a70e3c87ad0e4c5288a49f133d7c48ae3ca53b7b8cfac44eefb9cd1c563fd3b49e859f8f1de7789a6e936314ef7fa410512f0
data/.gitignore ADDED
@@ -0,0 +1,44 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Documentation cache and generated files:
20
+ /.yardoc/
21
+ /_yardoc/
22
+ /doc/
23
+ /rdoc/
24
+
25
+ ## Environment normalization:
26
+ /.bundle/
27
+ /vendor/bundle
28
+ /lib/bundler/man/
29
+
30
+ # for a library or gem, you might want to ignore these files since the code is
31
+ # intended to run in multiple environments; otherwise, check them in:
32
+ Gemfile.lock
33
+ .ruby-version
34
+ .ruby-gemset
35
+
36
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
37
+ .rubocop-https?--*
38
+
39
+ # Used by RubyMine
40
+ /.idea/
41
+ .rspec_status
42
+
43
+ /lib/main.rb
44
+ .DS_Store
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.3] - 2021-05-06
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in photo_mosaic.gemspec
6
+ gemspec
7
+
8
+ gem "aws-sdk-s3", "~> 1.94.1"
9
+ gem "mini_magick", "~> 4.11.0"
10
+ gem "rake", "~> 13.0"
11
+ gem "rspec", "~> 3.0"
12
+ gem "rubocop", "~> 1.7"
13
+ gem "ruby-progressbar", "~> 1.11"
14
+ gem "simplecov", "~> 0.21", require: false, group: :test
15
+ gem "wisper", "~> 2.0.1"
16
+ gem "wisper-rspec", "~>1.1.0", require: false, group: :test
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Axel Molina
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # PhotoMosaic
2
+ Create beautiful photo mosaics from your own images.
3
+
4
+ ![Alt text](docs/img/eltz_flowers.png "Eltz Castle made of flower tiles")
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'photo_mosaic'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself with:
18
+
19
+ $ gem install photo_mosaic
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require "photo_mosaic"
25
+
26
+ PhotoMosaic.subscribe(PhotoMosaic::ProgressBar.new)
27
+
28
+ PhotoMosaic.create(
29
+ original_image: "./images/castle.png",
30
+ tile_images: Dir["./images/tile_images/flowers/*"],
31
+ output_image: "./images/castle_mosaic.png",
32
+ tile_width: 25,
33
+ tile_height: 25
34
+ );
35
+ ```
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ To install this gem onto your local machine, run `bundle exec rake install`.
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/axel/photo_mosaic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/photo_mosaic/blob/master/CODE_OF_CONDUCT.md).
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
49
+
50
+ ## Code of Conduct
51
+
52
+ Everyone interacting in the PhotoMosaic project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/photo_mosaic/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "photo_mosaic"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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
data/docs/.DS_Store ADDED
Binary file
Binary file
Binary file
@@ -0,0 +1,60 @@
1
+ module PhotoMosaic
2
+ class Image
3
+ include Wisper::Publisher
4
+ include MiniMagickXform
5
+ include MiniMagickIo
6
+
7
+ attr_reader :pixels, :width, :height, :foreign_image
8
+
9
+ def initialize(pixels, foreign_image = nil)
10
+ @pixels = pixels
11
+ @width = pixels.first.size
12
+ @height = pixels.size
13
+ @foreign_image = foreign_image
14
+ super
15
+ end
16
+
17
+ def ==(other)
18
+ pixels == other.pixels
19
+ end
20
+
21
+ def find_match(images)
22
+ min_msd = nil
23
+ closest = nil
24
+ images.each do |other|
25
+ msd = msd(other)
26
+ if min_msd.nil? || msd < min_msd
27
+ min_msd = msd
28
+ closest = other
29
+ end
30
+ end
31
+ closest
32
+ end
33
+
34
+ def msd(other)
35
+ pixels_a = pixels
36
+ pixels_b = other.pixels
37
+ mse = 0
38
+ pixels_a.each_with_index do |row, j|
39
+ row.each_with_index do |pixel_a, i|
40
+ pixel_b = pixels_b[j][i] || pixel_a
41
+ mse += (pixel_a[0] - pixel_b[0])**2 + (pixel_a[1] - pixel_b[1])**2 + (pixel_a[2] - pixel_b[2])**2
42
+ end
43
+ end
44
+ mse
45
+ end
46
+
47
+ def squares(tile_width, tile_height)
48
+ tile_count = (width / tile_width) * (height / tile_height)
49
+ Enumerator.new(tile_count) do |yielder|
50
+ column_indexes = (0...width).to_a
51
+ @pixels.each_slice(tile_height) do |tile_rows|
52
+ column_indexes.each_slice(tile_width) do |tile_column_indexes|
53
+ tile = tile_rows.map { |row| row[tile_column_indexes.first..tile_column_indexes.last] }
54
+ yielder << self.class.new(tile)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,60 @@
1
+ module PhotoMosaic
2
+ module MiniMagickIo
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ base.include InstanceMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ include Wisper::Publisher
10
+
11
+ def import(image_path)
12
+ image_paths = Array(image_path)
13
+ images = Enumerator.new(image_paths.size) do |yielder|
14
+ image_paths.each do |path|
15
+ image = read(path)
16
+ if image_paths.size > 1
17
+ broadcast(:image_import, {broadcaster: self, paths: image_paths, current_path: path})
18
+ end
19
+ yielder << image
20
+ end
21
+ end
22
+ images.size > 1 ? images : images.first
23
+ end
24
+
25
+ def read(path)
26
+ image = image_cache[path]
27
+ if image.nil?
28
+ foreign_image = MiniMagick::Image.open(path)
29
+ pixels = foreign_image.get_pixels
30
+ image = new(pixels, foreign_image)
31
+ image_cache[path] = image
32
+ end
33
+ image
34
+ end
35
+
36
+ def image_cache
37
+ @image_cache ||= {}
38
+ end
39
+ end
40
+
41
+ module InstanceMethods
42
+ def initialize(pixels, foreign_image = nil)
43
+ @foreign_image = foreign_image || export(pixels)
44
+ end
45
+
46
+ def write(path)
47
+ foreign_image.write(path)
48
+ self
49
+ end
50
+
51
+ def export(pixels)
52
+ dimension = [pixels.first.size, pixels.size]
53
+ map = "rgb"
54
+ depth = 8
55
+ mime_type = "png"
56
+ MiniMagick::Image.get_image_from_pixels(pixels, dimension, map, depth, mime_type)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,43 @@
1
+ module PhotoMosaic
2
+ module MiniMagickXform
3
+ def self.included(base)
4
+ base.include InstanceMethods
5
+ end
6
+
7
+ module InstanceMethods
8
+ def fit(target_width, target_height)
9
+ x_offset, y_offset = 0, 0
10
+ crop_width, crop_height = width, height
11
+ target_aspect_ratio = target_width.fdiv(target_height)
12
+ self_aspect_ratio = width.fdiv(height)
13
+ if target_aspect_ratio < self_aspect_ratio
14
+ crop_width = (target_aspect_ratio * height).round
15
+ x_offset = (width - crop_width) / 2
16
+ elsif target_aspect_ratio > self_aspect_ratio
17
+ crop_height = (target_height.fdiv(target_width) * width).round
18
+ y_offset = (height - crop_height) / 2
19
+ end
20
+ # noinspection RubyResolve
21
+ foreign_image.crop("#{crop_width}x#{crop_height}+#{x_offset}+#{y_offset}")
22
+ resize(target_width, target_height)
23
+ initialize(foreign_image.get_pixels, foreign_image)
24
+ self
25
+ end
26
+
27
+ def resize(width, height)
28
+ foreign_image.resize("#{width}x#{height}")
29
+ initialize(foreign_image.get_pixels, foreign_image)
30
+ self
31
+ end
32
+
33
+ def shave(x_shave, y_shave)
34
+ target_width = width - x_shave
35
+ target_height = height - y_shave
36
+ # noinspection RubyResolve
37
+ foreign_image.crop("#{target_width}x#{target_height}+0+0")
38
+ initialize(foreign_image.get_pixels, foreign_image)
39
+ self
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,55 @@
1
+ module PhotoMosaic
2
+ class Mosaic
3
+ include Wisper::Publisher
4
+
5
+ def initialize(original_image, tile_images, tile_width, tile_height)
6
+ @tile_width = tile_width
7
+ @tile_height = tile_height
8
+ @original_image = original_image.shave(original_image.width % tile_width, original_image.height % tile_height)
9
+ @tile_images = tile_images.lazy.map { |img| img.fit(tile_width, tile_height) }
10
+ end
11
+
12
+ def join_tiles
13
+ rows = []
14
+ tiles.each_with_index do |tile, index|
15
+ row_index = index / width
16
+ rows[row_index] = if rows[row_index].nil?
17
+ tile.pixels
18
+ else
19
+ merge_pixels(rows[row_index], tile.pixels)
20
+ end
21
+ end
22
+ Image.new(rows.reduce(&:+))
23
+ end
24
+
25
+ def height
26
+ @original_image.height / @tile_height
27
+ end
28
+
29
+ def size
30
+ width * height
31
+ end
32
+
33
+ def tiles
34
+ squares = @original_image.squares(@tile_width, @tile_height)
35
+ Enumerator.new do |yielder|
36
+ squares.each do |square|
37
+ yielder << square.find_match(@tile_images)
38
+ broadcast(:image_find_match, {broadcaster: self, total: size})
39
+ end
40
+ end
41
+ end
42
+
43
+ def width
44
+ @original_image.width / @tile_width
45
+ end
46
+
47
+ private
48
+
49
+ def merge_pixels(pixels_a, pixels_b)
50
+ pixels_a.each_with_index.map do |row, index|
51
+ row + (pixels_b[index])
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,26 @@
1
+ module PhotoMosaic
2
+ class ProgressBar
3
+ def image_import(event)
4
+ @image_count ||= event[:paths].size
5
+ @import_progress_bar ||= ::ProgressBar.create(
6
+ total: @image_count,
7
+ length: 150,
8
+ format: "%a %e %P% Processed: %c/%C %t"
9
+ )
10
+ @import_progress_bar.title = "Importing tile images: " + event[:current_path]
11
+ @import_progress_bar.increment
12
+ system("clear") if @import_progress_bar.finished?
13
+ end
14
+
15
+ def image_find_match(event)
16
+ @mosaic_size ||= event[:total]
17
+ @mosaic_progress_bar ||= ::ProgressBar.create(
18
+ total: @mosaic_size,
19
+ length: 150,
20
+ format: "%a %e %P% Processed: %c/%C tiles %t: |\e[0;34m%B\e[0m|"
21
+ )
22
+ @mosaic_progress_bar.title = "Finding matches"
23
+ @mosaic_progress_bar.increment
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PhotoMosaic
4
+ VERSION = "0.1.3"
5
+ end
@@ -0,0 +1,22 @@
1
+ require "mini_magick"
2
+ require "ruby-progressbar"
3
+ require "wisper"
4
+ require "zeitwerk"
5
+
6
+ loader = Zeitwerk::Loader.for_gem
7
+ loader.setup
8
+
9
+ module PhotoMosaic
10
+ def self.create(original_image:, tile_images:, output_image:, tile_width:, tile_height:)
11
+ original_image = Image.import(original_image)
12
+ tile_images = Image.import(tile_images)
13
+ mosaic = Mosaic.new(original_image, tile_images, tile_width, tile_height)
14
+ mosaic_image = mosaic.join_tiles
15
+ mosaic_image.write(output_image)
16
+ mosaic
17
+ end
18
+
19
+ def self.subscribe(listener)
20
+ Wisper.subscribe(listener)
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "photo_mosaic/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "photo_mosaic"
8
+ spec.version = PhotoMosaic::VERSION
9
+ spec.authors = ["Axel Molina"]
10
+ spec.email = ["axel.molina@gmail.com"]
11
+ spec.summary = "Creates Photo Mosaics out of many small images to match a target image"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.0.1")
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = File.directory?(".git") ? `git ls-files`.split($/) : Dir.glob("**/*")
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ # Uncomment to register a new dependency of your gem
23
+ spec.add_dependency "bundler"
24
+ spec.add_dependency "zeitwerk"
25
+ spec.add_dependency "mini_magick"
26
+ spec.add_dependency "ruby-progressbar"
27
+ spec.add_dependency "wisper"
28
+
29
+ # For more information and docs about making a new gem, checkout our
30
+ # guide at: https://bundler.io/guides/creating_gem.html
31
+ end
data/spec/.DS_Store ADDED
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,41 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ module PhotoMosaic
4
+
5
+ RSpec.describe Image do
6
+ after(:each) { Image.image_cache.clear }
7
+
8
+ before do
9
+ @pixels_a = [[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]].freeze
10
+ @pixels_b = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]].freeze
11
+ @pixels_c = [[[1, 2, 3], [3, 4, 5]], [[7, 8, 9], [10, 11, 12]]].freeze
12
+ end
13
+
14
+ let(:image_a){ Image.new(@pixels_a) }
15
+ let(:image_b){ Image.new(@pixels_b) }
16
+ let(:image_c){ Image.new(@pixels_c) }
17
+
18
+ context "#find_match" do
19
+ it "finds the closest matching image using msd" do
20
+ closest = image_a.find_match([image_b, image_c])
21
+ expect(closest).to be(image_c)
22
+ end
23
+ end
24
+
25
+ context "#msd" do
26
+ it "computes the mean squared deviation between two images" do
27
+ expect(image_a.msd(image_b)).to eq(650) # the sum of the square of their differences
28
+ end
29
+ end
30
+
31
+ context "tiles" do
32
+ it "returns an enumerator that provides successive tiles as images (row-by-row)" do
33
+ tiles = image_b.squares(1, 2)
34
+ expect(tiles.next.pixels).to eq([[[1, 2, 3]], [[7, 8, 9]]])
35
+ expect(tiles.next.pixels).to eq([[[4, 5, 6]], [[10, 11, 12]]])
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,56 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ module PhotoMosaic
4
+ TARGET_IMAGE_PATHS = %w[./spec/images/target.png ./spec/images/target2.jpg]
5
+
6
+ RSpec.describe Image do
7
+
8
+ after(:each) { Image.image_cache.clear }
9
+
10
+ let(:original) { "./spec/images/target.png" }
11
+ let(:cropped) { "./spec/images/cropped.png" }
12
+
13
+ context "@import" do
14
+ it "returns the same object from cache if the image was imported previously", skip_before: true do
15
+ image = Image.import(original)
16
+ another_image = Image.import(original)
17
+ expect(another_image).to be(image)
18
+ end
19
+
20
+ it "returns an Enumerator if a collection of paths is provided", skip_before: true do
21
+ images = Image.import([original, original])
22
+ expect(images).to be_a_kind_of(Enumerator)
23
+ expect(images.next).to be_a(Image)
24
+ end
25
+
26
+ # it "broadcasts an event when an image is imported", skip_before: true do
27
+ # expect { Image.import(TARGET_IMAGE_PATHS) }.to broadcast(:image_import)
28
+ # end
29
+ end
30
+
31
+ context "@read" do
32
+ it "loads an image from a local file", skip_before: true do
33
+ image = Image.send(:read, original)
34
+ expect(image).to be_a(Image)
35
+ end
36
+ end
37
+
38
+ context "#new" do
39
+ it "creates and assigns an underlying MiniMagick::Image if none is provided" do
40
+ pixels = [[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]]
41
+ image = Image.new(pixels)
42
+ expect(image).to be_a(Image)
43
+ expect(image.foreign_image).to be_a(MiniMagick::Image)
44
+ end
45
+ end
46
+
47
+ context "#fit" do
48
+ it "crops by cutting along a single dimension and resizing" do
49
+ original_image = Image.import(original)
50
+ target_image = Image.import(cropped)
51
+ cropped_image = original_image.fit(target_image.width, target_image.height)
52
+ expect(cropped_image).to eq(target_image)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,24 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ module PhotoMosaic
4
+
5
+ RSpec.describe Mosaic do
6
+
7
+ let(:original) { "./spec/images/target.png" }
8
+ let(:mosaic) { "./spec/images/mosaic.png" }
9
+
10
+ context "compose_image" do
11
+ it "generates a photo mosaic of the target image" do
12
+ original_image = Image.import(original)
13
+ reference_composite = Image.import(mosaic)
14
+ tile_width, tile_height = 100, 100
15
+ tile_images = Image.import(Dir["./spec/images/tiles/squares_bw/*"])
16
+ mosaic = Mosaic.new(original_image, tile_images, tile_width, tile_height)
17
+ mosaic_image = mosaic.join_tiles
18
+ expect(mosaic_image).to eq(reference_composite)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "photo_mosaic"
4
+ require "simplecov"
5
+ require "wisper/rspec/matchers"
6
+
7
+ RSpec.configure do |config|
8
+ config.include(Wisper::RSpec::BroadcastMatcher)
9
+ # Enable flags like --only-failures and --next-failure
10
+ config.example_status_persistence_file_path = ".rspec_status"
11
+
12
+ # Disable RSpec exposing methods globally on `Module` and `main`
13
+ config.disable_monkey_patching!
14
+
15
+ config.expect_with :rspec do |c|
16
+ c.syntax = :expect
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: photo_mosaic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Axel Molina
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-07 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: '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: zeitwerk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mini_magick
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ruby-progressbar
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: wisper
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - axel.molina@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - CHANGELOG.md
92
+ - CODE_OF_CONDUCT.md
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - docs/.DS_Store
100
+ - docs/img/.DS_Store
101
+ - docs/img/eltz_flowers.png
102
+ - lib/photo_mosaic.rb
103
+ - lib/photo_mosaic/image.rb
104
+ - lib/photo_mosaic/mini_magick_io.rb
105
+ - lib/photo_mosaic/mini_magick_xform.rb
106
+ - lib/photo_mosaic/mosaic.rb
107
+ - lib/photo_mosaic/progress_bar.rb
108
+ - lib/photo_mosaic/version.rb
109
+ - photo_mosaic.gemspec
110
+ - spec/.DS_Store
111
+ - spec/images/.DS_Store
112
+ - spec/images/cropped.png
113
+ - spec/images/mosaic.png
114
+ - spec/images/target.png
115
+ - spec/images/tiles/.DS_Store
116
+ - spec/images/tiles/squares_bw/black.png
117
+ - spec/images/tiles/squares_bw/white.png
118
+ - spec/images/tiles/squares_color/blue.png
119
+ - spec/images/tiles/squares_color/brass.png
120
+ - spec/images/tiles/squares_color/brown.png
121
+ - spec/images/tiles/squares_color/green.png
122
+ - spec/images/tiles/squares_color/grey.png
123
+ - spec/images/tiles/squares_color/orange.png
124
+ - spec/images/tiles/squares_color/red.png
125
+ - spec/images/tiles/squares_color/white.png
126
+ - spec/images/tiles/squares_color/yellow.png
127
+ - spec/lib/photo_mosaic/image_spec.rb
128
+ - spec/lib/photo_mosaic/local_image_spec.rb
129
+ - spec/lib/photo_mosaic/mosaic_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage:
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: 3.0.1
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubygems_version: 3.2.15
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Creates Photo Mosaics out of many small images to match a target image
154
+ test_files: []