@@ -149,52 +149,66 @@ def __init__(self, filename=""):
149
149
except FileNotFoundError :
150
150
_logger .debug ("File %s not found" , filename )
151
151
self ._metadata = None
152
+ except piexif .InvalidImageDataError :
153
+ log .warning (
154
+ "Piexif only supports the file types JPEG and TIFF.<br>\n "
155
+ "Please install pyexiv2 for better file type support.<br>\n "
156
+ "For more information see<br>\n "
157
+ "https://karlch.github.io/vimiv-qt/documentation/exif.html" ,
158
+ once = True ,
159
+ )
160
+ self ._metadata = None
152
161
153
162
def fetch_key (self , base_key : str ) -> Tuple [str , str , str ]:
154
163
key = base_key .rpartition ("." )[2 ]
155
164
156
- with contextlib .suppress (piexif .InvalidImageDataError ):
157
- for ifd in self ._metadata :
158
- if ifd == "thumbnail" :
165
+ if self ._metadata is None :
166
+ return {}
167
+
168
+ for ifd in self ._metadata :
169
+ if ifd == "thumbnail" :
170
+ continue
171
+
172
+ for tag in self ._metadata [ifd ]:
173
+ keyname = piexif .TAGS [ifd ][tag ]["name" ]
174
+ keytype = piexif .TAGS [ifd ][tag ]["type" ]
175
+ val = self ._metadata [ifd ][tag ]
176
+ _logger .debug (
177
+ f"name: { keyname } \
178
+ type: { keytype } \
179
+ value: { val } \
180
+ tag: { tag } "
181
+ )
182
+ if keyname != key :
159
183
continue
160
-
161
- for tag in self ._metadata [ifd ]:
162
- keyname = piexif .TAGS [ifd ][tag ]["name" ]
163
- keytype = piexif .TAGS [ifd ][tag ]["type" ]
164
- val = self ._metadata [ifd ][tag ]
165
- _logger .debug (
166
- f"name: { keyname } \
167
- type: { keytype } \
168
- value: { val } \
169
- tag: { tag } "
170
- )
171
- if keyname != key :
172
- continue
173
- if keytype in (
174
- piexif .TYPES .Byte ,
175
- piexif .TYPES .Short ,
176
- piexif .TYPES .Long ,
177
- piexif .TYPES .SByte ,
178
- piexif .TYPES .SShort ,
179
- piexif .TYPES .SLong ,
180
- piexif .TYPES .Float ,
181
- piexif .TYPES .DFloat ,
182
- ): # integer and float
183
- return (keyname , keyname , str (val ))
184
- if keytype in (
185
- piexif .TYPES .Ascii ,
186
- piexif .TYPES .Undefined ,
187
- ): # byte encoded
188
- return (keyname , keyname , val .decode ())
189
- if keytype in (
190
- piexif .TYPES .Rational ,
191
- piexif .TYPES .SRational ,
192
- ): # (int, int) <=> numerator, denominator
193
- return (keyname , keyname , f"{ val [0 ]} /{ val [1 ]} " )
184
+ if keytype in (
185
+ piexif .TYPES .Byte ,
186
+ piexif .TYPES .Short ,
187
+ piexif .TYPES .Long ,
188
+ piexif .TYPES .SByte ,
189
+ piexif .TYPES .SShort ,
190
+ piexif .TYPES .SLong ,
191
+ piexif .TYPES .Float ,
192
+ piexif .TYPES .DFloat ,
193
+ ): # integer and float
194
+ return (keyname , keyname , str (val ))
195
+ if keytype in (
196
+ piexif .TYPES .Ascii ,
197
+ piexif .TYPES .Undefined ,
198
+ ): # byte encoded
199
+ return (keyname , keyname , val .decode ())
200
+ if keytype in (
201
+ piexif .TYPES .Rational ,
202
+ piexif .TYPES .SRational ,
203
+ ): # (int, int) <=> numerator, denominator
204
+ return (keyname , keyname , f"{ val [0 ]} /{ val [1 ]} " )
194
205
195
206
raise KeyError (f"Key '{ base_key } ' not found" )
196
207
197
208
def get_keys (self ) -> Iterable [str ]:
209
+ if self ._metadata is None :
210
+ return iter ([])
211
+
198
212
return (
199
213
piexif .TAGS [ifd ][tag ]["name" ]
200
214
for ifd in self ._metadata
@@ -203,6 +217,9 @@ def get_keys(self) -> Iterable[str]:
203
217
)
204
218
205
219
def copy_metadata (self , dest : str , reset_orientation : bool = True ) -> None :
220
+ if self ._metadata is None :
221
+ return
222
+
206
223
try :
207
224
if reset_orientation :
208
225
with contextlib .suppress (KeyError ):
@@ -218,6 +235,9 @@ def copy_metadata(self, dest: str, reset_orientation: bool = True) -> None:
218
235
_logger .debug ("No metadata data in '%s'" , dest )
219
236
220
237
def get_date_time (self ) -> str :
238
+ if self ._metadata is None :
239
+ return ""
240
+
221
241
with contextlib .suppress (
222
242
piexif .InvalidImageDataError , FileNotFoundError , KeyError
223
243
):
0 commit comments