/
reorient
executable file
·54 lines (45 loc) · 1.4 KB
/
reorient
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
#!/usr/bin/env ruby
# frozen_string_literal: true
# reorient: extract and remove orientation tags from images while rotating
require "json"
require "open3"
ORIENTATION_MAP = {
2 => %w[-flip horizontal],
3 => %w[-rotate 180],
4 => %w[-flip vertical],
5 => %w[-transpose],
6 => %w[-rotate 90],
7 => %w[-transverse],
8 => %w[-rotate 270],
}.freeze
def run!(*args)
STDERR.puts "[+] #{args.join(" ")}" if ENV["VERBOSE"]
output, status = Open3.capture2(*args)
abort "[!] Fatal: Couldn't run #{args.first}" unless status.success?
output
end
abort "Usage: reorient <file> [file ...]" if ARGV.empty?
json = run! "exiftool", "-Orientation", "-n", "-json", *ARGV
JSON.parse(json).tap do |d|
d.each do |obj|
filename = obj["SourceFile"]
orientation = obj["Orientation"]
case orientation
when nil
STDERR.puts "[!] #{filename} has no orientation information, skipping"
next
when 1
next
else
orientation_flags = ORIENTATION_MAP[orientation]
unless orientation_flags
STDERR.puts "[!] Unknown orientation value (#{orientation}) for #{filename}"
next
end
# TODO(ww): Might be worthwhile to check whether we're operating on a JPEG
# here and use jpegtran instead.
run! "convert", filename, "-quality", "100", *orientation_flags, filename
run! "exiftool", "-Orientation=", "-overwrite_original", filename
end
end
end