Class: Photo

Inherits:
Object
  • Object
show all
Defined in:
lib/multistockphoto/photo.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Photo

Returns a new instance of Photo.



10
11
12
13
# File 'lib/multistockphoto/photo.rb', line 10

def initialize(filename)
  @filename = filename
  @tags = nil
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



7
8
9
# File 'lib/multistockphoto/photo.rb', line 7

def filename
  @filename
end

#rotated_filenameObject (readonly)

Returns the value of attribute rotated_filename.



7
8
9
# File 'lib/multistockphoto/photo.rb', line 7

def rotated_filename
  @rotated_filename
end

#tagsObject

Returns the value of attribute tags.



8
9
10
# File 'lib/multistockphoto/photo.rb', line 8

def tags
  @tags
end

Class Method Details

.to_tags(s) ⇒ Object

erstellt Key-Array aus String (getrennt durch newline,‘,’ oder ‘ ’)



86
87
88
89
90
91
92
# File 'lib/multistockphoto/photo.rb', line 86

def self.to_tags(s)
  keys = []
  s.split(/[, |\t\n]+/).each {|w|
    keys << w
  }
  keys
end

Instance Method Details

#drehenObject

dreht Bild und schreibt in rot_… Datei



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/multistockphoto/photo.rb', line 44

def drehen
  pic = ImageList.new(@filename)
  if pic.orientation == LeftBottomOrientation
    b = File.basename(@filename)
    tmpname = @filename.sub(b,'') +'rot_' + b 
    if not File.exist? tmpname
      pic.auto_orient!
      puts "rotating picture, writing to #{tmpname}"
      pic.write(tmpname)
      
    end
    @rotated_filename = tmpname
  end
end

#file_keywordsObject

ermittelt Keywords aus Datei



95
96
97
98
99
# File 'lib/multistockphoto/photo.rb', line 95

def file_keywords
  photo = MiniExiftool.new @filename
  self.tags = photo.tags
  photo.keywords
end

#keywordsObject

Keywords aus Object-Attributen



81
82
83
# File 'lib/multistockphoto/photo.rb', line 81

def keywords
  tags
end

#portrait?Boolean

ist Bild im Hochformat TODO: paßt dieser Test für alle gedrehten Bilder?

Returns:

  • (Boolean)


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
# File 'lib/multistockphoto/photo.rb', line 17

def portrait?
  if false
    pic = ImageList.new(@filename)
    if pic.orientation == LeftBottomOrientation
      return true
    else
      return false
    end
  end
  if true
    img = Magick::Image::read(@filename).first
    #   p img
    begin
      if img.orientation == LeftBottomOrientation
        return true
      else
        return false
      end
    rescue
      puts "error in picture #{@filename}"
      return false # TODO: etwas gefaehrlich, bei nicht richtiger JPG-Datei
      # wuerde das auch zuenden
    end
  end
end

#set_keywordsObject

setzt IPTC-Keywords, enthalten in Datei <imagefilename>.tags



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/multistockphoto/photo.rb', line 60

def set_keywords
  ext = File.extname(@filename)
  fn = @filename.sub(ext,".tags")
  lines=[]
  begin
    File.open(fn) {|f|
      lines = f.read
    }
    puts "tags file #{fn} found"
    tags = Photo.to_tags(lines)
    self.tags = tags
    print "writing tags to file ... "
    $stdout.flush
    write_keywords
    puts "done"
  rescue Errno::ENOENT
    warn "WARNING: no tags file #{fn}"
  end
end

#write_keywordsObject

schreibt Keywords in Datei-Header



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/multistockphoto/photo.rb', line 102

def write_keywords
  photo = MiniExiftool.new @filename
  s = ''
  tags.each {|tag|
    if s == ''
      s = s + tag
    else
#        s = s + ' ' + tag
# lieber durch Komma trennen, da sonst das Formular z.B. bei fotolia nicht
# richtig gefüllt wird
      s = s + ',' + tag
    end
  }
  photo['keywords'] = s
  
  #TODO:
  photo['title'] = 'Testtitel'
  
  photo.save
end