~mro/Photos2Atom

ebf3d0cf39f27934a560379ca0128a7cf6e46d0f — Marcus Rohrmoser 8 years ago 9c31e74
bake exif rotation into image and reset exif to normal. refs #1
3 files changed, 17 insertions(+), 12 deletions(-)

M Photos2Atom.rb
M README.md
M run.sh
M Photos2Atom.rb => Photos2Atom.rb +12 -9
@@ 224,10 224,7 @@ END_OF_SQL
            $stderr.write 'o'
            next
          end
          # $stderr.puts "sha('#{row[5]}') "
          sha = Digest::SHA1.hexdigest(row[5])   # (destination) file extension not part of sha.
          # $stderr.puts "  #{img['ThumbPath']}"
          # $stderr.puts "  #{img['OriginalPath']}"
          begin
            f.entries << Atom::Entry.new do |e|
              e.id = @BASE_URL + ('#_' + sha)


@@ 245,7 242,15 @@ END_OF_SQL
                # unless FileUtils.uptodate?(dst, [src])                            # wrong, compare timestamp to img[:created]
                if !File.exist?(dst) || File.mtime(dst) < create_date
                  $stderr.write '+'
                  # FileUtils.cp(src, dst)
                  FileUtils.cp(src, dst)
                  # bake exif rotation into image and reset exif to normal http://www.leancrew.com/all-this/2009/04/derotating-jpegs-with-exiftool/
                  rotation_exif = IO.popen("exiftool -Orientation -n '#{src}'") do |f|
                    m = /^Orientation\s*:\s*(\d+)\s*$/m.match(f.read) || [nil,nil]
                    m[1]
                  end
#                  rotation = row[11].nil? || '' == row[11] ? row[13].to_i : 0 # rotate only if unmodified
                  # fallback to Photos-DB rotation if exiftool fails (usually jpeg2000)
                  rotation = rotation_exif.nil? ? row[13].to_i : [0,90,180,270][(rotation_exif.to_i - 1) % 4]
                  unless h[:pxls].nil?
                    # @todo - keep original pngs in high-res
                    # https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/sips.1.html


@@ 257,16 262,14 @@ END_OF_SQL
                      :format         => dst_fmt,
                      :formatOptions  => h[:quality],
                    }
                    rotation = row[11].nil? ? row[13] : 0 # rotate only if unmodified
                    cmd = "sips --rotate #{rotation} #{properties.map{|k,v| "--setProperty #{k} #{sh_escape(v)}"}.join(' ')} -Z #{h[:pxls]} '#{src}' --out '#{dst}' > /dev/null 2> /dev/null"
                    # $stderr.puts cmd
                    cmd = "sips --rotate #{rotation} #{properties.map{|k,v| "--setProperty #{k} #{sh_escape(v)}"}.join(' ')} -Z #{h[:pxls]} '#{dst}' 1>/dev/null 2>&1"
                    unless system(cmd)
                      $stderr.puts "  failed: #{cmd}"
                      FileUtils.rm dst
                    end
                    cmd = "exiftool -Orientation=1 -n -overwrite_original_in_place '#{dst}' 1>/dev/null 2>&1"
                    $stderr.puts "  failed: #{cmd}" unless system(cmd)
                    # @todo optional run pngquant / optipng
                  else
                    FileUtils.cp(src, dst)
                  end
                  FileUtils.touch dst, :mtime => create_date+1
                else

M README.md => README.md +4 -3
@@ 6,6 6,7 @@ galleries.
# Usage

    $ gem install bundler && bundle install
    $ [brew](http://brew.sh/) install exiftool
    $ # optional:
    $  sh ./install-lightbox2.sh
    $ vim run.sh # enter iPhoto path, album names, dst path, dst base URL


@@ 13,15 14,15 @@ galleries.

## Prerequisites

all pre-installed on OS X:
- [exiftool](http://www.sno.phy.queensu.ca/~phil/exiftool/)

plus the already present (on OS X):

- [ruby](http://www.ruby-lang.org/de/)
- [xmllint](http://xmlsoft.org/xmllint.html)
- [xsltproc](http://xmlsoft.org/XSLT/xsltproc.html)
- [sips](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/sips.1.html)
- [rsync](http://rsync.samba.org/)
- [curl](http://curl.haxx.se/)
- [unzip](http://www.info-zip.org/pub/infozip/)

## Appendix


M run.sh => run.sh +1 -0
@@ 21,6 21,7 @@ bundle --version >/dev/null   || { echo "$ gem install bundler" && exit 1 ; }
xmllint --version 2>/dev/null || { echo "Install libxml2" && exit 1 ; }
xsltproc --version >/dev/null || { echo "Install xsltproc" && exit 1 ; }
rsync --version >/dev/null    || { echo "Install rsync" && exit 1 ; }
exiftool -ver >/dev/null      || { echo "Install exiftool, e.g. via $ brew install exiftool" && exit 1 ; }

cwd="$(pwd)"
cd "$(dirname "$0")"