Last active
August 20, 2017 10:47
-
-
Save shanecelis/ee7a6d9b7abafb1ff07fad2a2b6deaf2 to your computer and use it in GitHub Desktop.
Takes a screenshot of a inspector window from Unity and crops out the largest component. Useful for generating images for documentation. Like so: https://twitter.com/shanecelis/status/899218702805209088
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/local/bin/ruby1.8 | |
# | |
# crop-component | |
# | |
# Takes a screenshot of a inspector window from Unity and crops out the largest | |
# component. Useful for generating documentation. | |
# | |
# Original code Copyright (c) 2017 Shane Celis[1] | |
# Licensed under the MIT License[2] | |
# | |
# This comment generated by code-cite[3]. | |
# | |
# [1]: https://twitter.com/shanecelis | |
# [2]: https://opensource.org/licenses/MIT | |
# [3]: https://github.com/shanecelis/code-cite | |
require 'RMagick' | |
require 'optparse' | |
# http://ruby-doc.org/stdlib-2.4.1/libdoc/optparse/rdoc/OptionParser.html | |
options = {} | |
OptionParser.new do |opts| | |
opts.banner = "usage: crop-component [-v] <src> <dest>" | |
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| | |
options[:verbose] = v | |
end | |
end.parse! | |
if ARGV.length != 2 then | |
puts "usage: crop-component <src> <dest>" | |
puts "Crops the largest component shown in a unity inspector window." | |
exit 2 | |
end | |
file = ARGV[0] | |
dest = ARGV[1] | |
img = Magick::Image::read(file).first | |
width = img.columns | |
puts "width ", width, " yeah" | |
lastRow = -1 | |
matchCount = 0 | |
# 7a7a7a | |
#lineColor = Magick::Pixel.new(122, 122, 122, 255) | |
# lineColor = Magick::Pixel.new(31354, 31354, 31354, 0) | |
lineColor = Magick::Pixel.new(29812, 29812, 29812, 0) | |
#backgroundColor = Magick::Pixel.new(49858, 49858, 49858, 0) | |
rows = [] | |
img.each_pixel do | pixel, c, r | | |
if r <= 82 | |
next | |
end | |
# puts pixel | |
if lastRow != r then | |
p = matchCount.to_f/width | |
if p > 0.9 then | |
puts "#{p} proportion match on row #{r}" | |
rows.push(r) | |
end | |
matchCount = 0 | |
lastRow = r | |
end | |
if (pixel <=> lineColor) == 0 then | |
matchCount += 1 | |
end | |
end | |
rowDeltas = rows.each_cons(2).map { |a, b| b - a } | |
i = rowDeltas.rindex(rowDeltas.max) | |
# puts "max on index #{i}" | |
# puts rows | |
if i != nil && i < rows.length then | |
top = rows[i] | |
bottom = rows[i + 1] - 1 | |
cropped = img.crop(0, top, width, bottom - top) | |
w = cropped.columns | |
h = cropped.rows | |
cropped.resize!(250, 250.0/w * h)#, cropped.columns) | |
cropped.write dest | |
else | |
puts "Problem" | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment