Class: Photo

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Photo

Returns a new instance of Photo.



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

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

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#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

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#drehenObject

dreht Bild und schreibt in rot_… Datei



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

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



120
121
122
123
124
125
# File 'lib/multistockphoto/photo.rb', line 120

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

#file_titleObject



152
153
154
# File 'lib/multistockphoto/photo.rb', line 152

def file_title
  @title
end

#heightObject



147
148
149
# File 'lib/multistockphoto/photo.rb', line 147

def height
  42
end

#keywordsObject

Keywords aus Object-Attributen



115
116
117
# File 'lib/multistockphoto/photo.rb', line 115

def keywords
  tags
end

#portrait?Boolean

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

Returns:

  • (Boolean)


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

def portrait?
  if false # vorher
    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_keywords(from_lang = nil, to_lang = nil) ⇒ Object

setzt IPTC-Keywords, enthalten in Datei <imagefilename>.tags wenn from_lang und to_lang uebergeben, dann Uebersetzung der Keywords z.B. set_keywords(:de,:en)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/multistockphoto/photo.rb', line 63

def set_keywords(from_lang=nil,to_lang=nil)
  ext = File.extname(@filename)
  if ext.upcase != '.JPG' and ext.upcase != '.JPEG'
    warn "cannot set Exif-Header with non-JPEG file #{@filename}"
    return
  end
  fn = @filename.sub(ext,".tags")
  lines=[]
  begin
    File.open(fn) {|f|
      lines = f.read
    }
    tags = Photo.to_tags(lines)
    if from_lang and to_lang
      tags = translate_tags(tags,from_lang,to_lang)
    end
    self.tags = tags
    #print "writing tags to file ... "
    #$stdout.flush
    write_attributes
    #puts "done"
  rescue Errno::ENOENT
    #warn "WARNING: no tags file #{fn}"
  end
end

#set_keywords_without_write(from_lang = nil, to_lang = nil) ⇒ Object

setzt Keywords in Photo-Objekt, schreibt aber nicht in Datei. Aus Performanzgründen (z.B. bei –plist)



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/multistockphoto/photo.rb', line 91

def set_keywords_without_write(from_lang=nil,to_lang=nil)
  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)
#      tags.each_with_index {|tag,i|
#        p tag
#        p i
#      }
    if from_lang and to_lang
      tags = translate_tags(tags,from_lang,to_lang)
    end
    self.tags = tags
  rescue Errno::ENOENT
    #warn "WARNING: no tags file #{fn}"
  end
end

#widthObject



141
142
143
144
145
# File 'lib/multistockphoto/photo.rb', line 141

def width
  photo = MiniExiftool.new @filename
  
  42
end

#write_attributesObject

schreibt Keywords und Titel in Datei-Header



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/multistockphoto/photo.rb', line 128

def write_attributes
  photo = MiniExiftool.new @filename
  # lieber durch Komma trennen, da sonst das Formular z.B. bei fotolia nicht
  # richtig gefüllt wird
  # Die jetzige Lösung funktioniert bei: Fotolia, Zoonar (die restliche konnte
  # ich noch nicht überprüfen, da ich dort noch kein einziges Photo durchbekommen
  # habe :-(
  photo['keywords'] = tags.join(',')
  #TODO:
  photo['title'] = self.title
  photo.save
end