Guest User

Untitled

a guest
Feb 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. # Author: Vitalie Lazu
  2. # Date: Mon, 01 Dec 2008 14:28:28 +0200
  3.  
  4. # Rake tasks to help you with family photos:
  5. # * Download photos from camera
  6. # * Arrange photos in folders by date like digikam,
  7. # * Rotate them according exif information
  8. #
  9. # Setup to use this tasks:
  10. # apt-get install rubygems imagemagick gphoto2 rake
  11. # gem install exifr
  12.  
  13. namespace :ph do
  14. desc "Get photos from camera to current folder"
  15. task :get do
  16. sh "gphoto2 -R -P"
  17. end
  18.  
  19. desc "Delete all photos from camera"
  20. task :delete do
  21. sh "gphoto2 -R -D"
  22. end
  23.  
  24. desc "Rotate photos from exif info"
  25. task :rotate => :init do
  26. for_each_image do |fname, info|
  27. p info.orientation
  28.  
  29. if info.orientation
  30. x = FakeImg.new
  31. info.orientation.transform_rmagick(x)
  32. op = x.to_s
  33.  
  34. dest_file = "r-" << fname
  35.  
  36. if op.length > 0
  37. mv fname, dest_file
  38. cmd = "convert '#{dest_file}' #{op} '#{fname}'"
  39. puts cmd
  40. sh cmd
  41. end
  42. end
  43. end
  44. end
  45.  
  46. desc "Arrange photos in folders by date"
  47. task :arrange => :init do
  48. for_each_image do |fname, info|
  49. dir = info.date_time.strftime("%Y-%m-%d")
  50. op = info.orientation.transform_rmagick(FakeImg.new).to_s
  51. print "%s %s %s\n" % [fname, dir, op]
  52.  
  53. dest_file = File.join(dir, fname)
  54. mkdir_p dir
  55.  
  56. if op.length > 0
  57. cmd = "convert '#{fname}' #{op} '#{dest_file}'"
  58. puts cmd
  59. sh cmd
  60. else
  61. if test ?f, dest_file
  62. puts "File exits: #{dest_file}"
  63. else
  64. mv fname, dest_file
  65. end
  66. end
  67. end
  68. end
  69.  
  70. desc "Show photos info"
  71. task :list => :init do
  72. for_each_image do |fname, info|
  73. print "%s %s %s\n" % [fname, info.date_time, info.orientation.transform_rmagick(FakeImg.new).to_s]
  74. end
  75. end
  76.  
  77. task :init do
  78. require 'rubygems'
  79. require 'exifr'
  80. end
  81.  
  82. def for_each_image(&block)
  83. for fname in Dir["*"]
  84. next unless test(?f, fname) && fname =~ /\.jpe?g$/i
  85. yield fname, EXIFR::JPEG.new(fname)
  86. end
  87. end
  88.  
  89. class FakeImg
  90. def initialize
  91. @cmd = []
  92. end
  93.  
  94. def flip
  95. @cmd << "-flip"
  96. self
  97. end
  98.  
  99. def flop
  100. @cmd << "-flop"
  101. self
  102. end
  103.  
  104. def rotate(degrees)
  105. @cmd << "-rotate #{degrees.to_i}" if degrees.to_i != 0
  106. self
  107. end
  108.  
  109. def to_s
  110. @cmd * ' '
  111. end
  112. end
  113. end
Add Comment
Please, Sign In to add comment