-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-processing.rb
More file actions
82 lines (63 loc) · 1.84 KB
/
Copy pathimage-processing.rb
File metadata and controls
82 lines (63 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'pry'
require 'pathname'
require 'mini_magick'
require 'fileutils'
puts "the source dir:"
SOURCE_DIR = Pathname "#{ENV['HOME']}/Downloads/products-image"
DEST_DIR = Pathname "#{ENV['HOME']}/Downloads/products-dest"
class ImageProcessor
include MiniMagick
def initialize(in_path, out_path)
@in = in_path
@out = out_path
end
def to_all
to_web
to_taobao
end
def to_web
# do reflections on image itself with image[:dimensions]
image = Image.open @in
image.resize '620x600'
image.write @out + "web-#{name(image)}"
puts "saved processed file to #@out web-#{name(image)}"
end
def to_taobao
image = Image.open @in
image.resize '1240x1200' if image[:width] > 1240
# print water logo
logo_height = logo_width = image[:width] * 0.15
logo = MiniMagick::Image.open(SOURCE_DIR + '0.water-logo.png')
logo.resize "#{logo_width}x#{logo_height}"
result = image.composite(logo) do |c|
c.gravity "SouthEast"
end
result.write @out + "taobao-#{name(image)}"
puts "saved processed file to #@out taobao-#{name(image)}"
end
private
def name(image)
[image[:dimensions].join('x'), @in.basename.to_s].join('-')
end
end
def process(path)
return unless path.extname =~ /png|jpg|jpeg|gif/
# move up from the 2.export folder
folder = path.dirname.relative_path_from(SOURCE_DIR).parent
dest_folder = DEST_DIR + folder
FileUtils.mkdir_p dest_folder unless dest_folder.exist?
processor = ImageProcessor.new(path, dest_folder)
processor.to_all
end
def walk(root_path)
root_path.children.each do |path|
if path.directory?
walk(path)
else
process(path) if path.to_s =~ /export/
end
end
end
walk(SOURCE_DIR)
system("jpegoptim -vtm80 --strip-all `find #{DEST_DIR}/**/* -name '*.jpg'`")
system("optipng -o5 `find #{DEST_DIR}/**/* -name '*.png'`")