Last active
October 14, 2021 14:20
-
-
Save oakho/3c955242c83b88398a941c411a7649ad to your computer and use it in GitHub Desktop.
Image Process
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
#!/usr/bin/env ruby | |
RMAGICK_ENABLE_MANAGED_MEMORY = true | |
require 'fileutils' | |
require 'pathname' | |
require 'RMagick' | |
require 'slop' | |
require 'parallel' | |
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC) | |
$captions = { | |
'N' => "Nacelle vendue séparément", | |
'C' => "Coloris non contractuel", | |
'A' => "JE TAIME" | |
} | |
include Magick | |
opts = Slop.parse do |o| | |
o.string '-o', '--output', 'a output path', default: 'processed' | |
o.string '--fuzz', 'fuzz', default: '5%' | |
o.on '-h', '--help' do | |
puts o | |
exit | |
end | |
end | |
path = opts.arguments[0] || '.' | |
directory_name = opts.arguments[1] || opts[:output] | |
fuzz = opts[:fuzz] | |
def caption_for(file) | |
$captions[file[/-(\w)?.\w+$/i, 1]] | |
end | |
def skip?(file) | |
!File.basename(file).include?("_") | |
end | |
directory_name = "#{Pathname.new(path).basename}-#{directory_name}" | |
puts "Processing #{Pathname.new(path).basename} ..." | |
FileUtils.mkdir_p("#{directory_name}") if !Dir.exist?(directory_name) | |
FileUtils.mkdir_p("#{directory_name}-flattened") if !Dir.exist?("#{directory_name}-flattened") | |
FileUtils.cp_r("#{path}/.", "#{directory_name}") | |
puts "=======================================================" | |
Parallel.map(Dir["#{directory_name}/**/*.{jpg,jpeg,png}"]) do |file| | |
puts "Item: #{file}, Worker: #{Parallel.worker_number}" | |
# force all files to jpg by changing ext | |
new_file = Pathname.new(file).sub_ext('.jpg') | |
if file.include?('**') | |
if skip?(file) | |
FileUtils.cp(file, "#{directory_name}-flattened/#{File.basename(new_file).gsub('**', '')}") | |
puts "#{new_file} copied to flattened folder..." | |
end | |
puts "#{new_file} skipped..." | |
next | |
end | |
blank = Image.new(1000, 1000) | |
caption = caption_for(file) | |
puts "Caption for #{new_file}: #{caption}" | |
img = ImageList.new(file) | |
img.fuzz = fuzz | |
img.trim!(true) | |
img.resize_to_fit!(950, 950) | |
img.border!(25, 25, '#fff') | |
blank.composite!(img, Magick::CenterGravity, 0, 0, Magick::OverCompositeOp) | |
if caption | |
text = Magick::Image.read("caption:* #{caption}") do | |
self.size = '1000x1000' | |
self.background_color = 'none' | |
self.pointsize = 15 | |
self.font = 'Helvetica' | |
self.fill = '#666' | |
self.gravity = Magick::SouthWestGravity | |
end.first | |
blank.composite!(text, Magick::SouthWestGravity, 25, 25, Magick::OverCompositeOp) | |
end | |
# as we clone source directory, clean up any unprocessed files because ext was renamed | |
# this happens because we copied a .png file while renaming it to .jpg, | |
# the .png source file remains but we don't want to keep item | |
if new_file != file | |
FileUtils.rm(file) | |
end | |
blank.write(new_file) { self.define('jpeg', 'extent', '100kb') } | |
if skip?(file) | |
blank.write("#{directory_name}-flattened/#{File.basename(new_file)}") { self.define('jpeg', 'extent', '100kb') } | |
puts "#{new_file} copied to flattened folder..." | |
end | |
blank.destroy! | |
puts "#{new_file} processed..." | |
end | |
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC) | |
elapsed = ending - starting | |
puts "=======================================================" | |
puts "Finished in #{elapsed} seconds" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install Homebrew :
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install CMD :
Update CMD :
Usage :