Proper metadata support.

master
Jason A. Donenfeld 2011-05-07 02:56:54 -04:00
parent 781ef1ac92
commit 07369da4aa
2 changed files with 76 additions and 31 deletions

View File

@ -112,11 +112,11 @@ class Photo(object):
except:
self.is_valid = False
return
if attributes is not None and attributes["DateTimeFile"] >= mtime:
if attributes is not None and attributes["dateTimeFile"] >= mtime:
self._attributes = attributes
return
self._attributes = {}
self._attributes["DateTimeFile"] = mtime
self._attributes["dateTimeFile"] = mtime
try:
image = Image.open(path)
@ -127,30 +127,76 @@ class Photo(object):
self._thumbnails(image, thumb_path)
def _metadata(self, image):
self._attributes["size"] = image.size
self._orientation = 1
try:
info = image._getexif()
except:
return
if not info:
return
exif = {}
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
if not isinstance(decoded, int) and decoded not in ['JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'FileSource', 'MakerNote', 'UserComment', 'ImageDescription', 'ComponentsConfiguration']:
if isinstance(value, str):
value = value.strip()
if decoded.startswith("DateTime"):
try:
value = datetime.strptime(value, '%Y:%m:%d %H:%M:%S')
except:
pass
self._attributes[decoded] = value
if isinstance(value, str):
value = value.strip()
if decoded.startswith("DateTime"):
try:
value = datetime.strptime(value, '%Y:%m:%d %H:%M:%S')
except:
pass
exif[decoded] = value
if "Orientation" in exif:
self._orientation = exif["Orientation"];
if self._orientation in range(5, 9):
self._attributes["size"] = (self._attributes["size"][1], self._attributes["size"][0])
self._attributes["orientation"] = ["Horizontal (normal)", "Mirror horizontal", "Rotate 180", "Mirror vertical", "Mirror horizontal and rotate 270 CW", "Rotate 90 CW", "Mirror horizontal and rotate 90 CW", "Rotate 270 CW"][self._orientation - 1]
if "Make" in exif:
self._attributes["make"] = exif["Make"]
if "Model" in exif:
self._attributes["model"] = exif["Model"]
if "ApertureValue" in exif:
self._attributes["aperture"] = exif["ApertureValue"]
if "FNumber" in exif:
self._attributes["fStop"] = exif["FNumber"]
if "FocalLength" in exif:
self._attributes["focalLength"] = exif["FocalLength"]
if "ISOSpeedRatings" in exif:
self._attributes["iso"] = exif["ISOSpeedRatings"]
if "ISO" in exif:
self._attributes["iso"] = exif["ISO"]
if "PhotographicSensitivity" in exif:
self._attributes["iso"] = exif["PhotographicSensitivity"]
if "ExposureTime" in exif:
self._attributes["exposureTime"] = exif["ExposureTime"]
if "MeteringMode" in exif:
self._attributes["meteringMode"] = exif["MeteringMode"]
if "Flash" in exif:
self._attributes["flash"] = exif["Flash"] != 0
if "ExposureProgram" in exif:
self._attributes["exposureProgram"] = ["Not Defined", "Manual", "Program AE", "Aperture-priority AE", "Shutter speed priority AE", "Creative (Slow speed)", "Action (High speed)", "Portrait", "Landscape", "Bulb"][exif["ExposureProgram"]]
if "SpectralSensitivity" in exif:
self._attributes["spectralSensitivity"] = exif["SpectralSensitivity"]
if "MeteringMode" in exif:
if exif["MeteringMode"] == 255:
self._attributes["meteringMode"] = "Other"
else:
self._attributes["meteringMode"] = ["Unknown", "Average", "Center-weighted average", "Spot", "Multi-spot", "Multi-segment", "Partial"][exif["MeteringMode"]]
if "ExposureCompensation" in exif:
self._attributes["exposureCompensation"] = exif["ExposureCompensation"]
if "ExposureBiasValue" in exif:
self._attributes["exposureCompensation"] = exif["ExposureBiasValue"]
if "DateTimeOriginal" in exif:
self._attributes["dateTimeOriginal"] = exif["DateTimeOriginal"]
if "DateTime" in exif:
self._attributes["dateTime"] = exif["DateTime"]
if "Orientation" in self._attributes and self._attributes["Orientation"] in range(5, 9):
self._attributes["size"] = (self._attributes["size"][1], self._attributes["size"][0])
def _thumbnail(self, image, thumb_path, size, square=False):
thumb_path = os.path.join(thumb_path, image_cache(self._path, size, square))
print "Thumbing %s" % thumb_path
if os.path.exists(thumb_path) and file_mtime(thumb_path) >= self._attributes["DateTimeFile"]:
if os.path.exists(thumb_path) and file_mtime(thumb_path) >= self._attributes["dateTimeFile"]:
return
gc.collect()
image = image.copy()
@ -174,30 +220,26 @@ class Photo(object):
os.path.unlink(thumb_path)
def _thumbnails(self, image, thumb_path):
if "Orientation" in self._attributes:
orientation = self._attributes["Orientation"]
else:
orientation = 1
mirror = image
if orientation == 2:
if self._orientation == 2:
# Vertical Mirror
mirror = image.transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 3:
elif self._orientation == 3:
# Rotation 180
mirror = image.transpose(Image.ROTATE_180)
elif orientation == 4:
elif self._orientation == 4:
# Horizontal Mirror
mirror = image.transpose(Image.FLIP_TOP_BOTTOM)
elif orientation == 5:
elif self._orientation == 5:
# Horizontal Mirror + Rotation 270
mirror = image.transpose(Image.FLIP_TOP_BOTTOM).transpose(Image.ROTATE_270)
elif orientation == 6:
elif self._orientation == 6:
# Rotation 270
mirror = image.transpose(Image.ROTATE_270)
elif orientation == 7:
elif self._orientation == 7:
# Vertical Mirror + Rotation 270
mirror = image.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_270)
elif orientation == 8:
elif self._orientation == 8:
# Rotation 90
mirror = image.transpose(Image.ROTATE_90)
for size in Photo.thumb_sizes:
@ -222,7 +264,7 @@ class Photo(object):
elif "DateTime" in self._attributes:
return self._attributes["DateTime"]
else:
return self._attributes["DateTimeFile"]
return self._attributes["dateTimeFile"]
def __cmp__(self, other):
date_compare = cmp(self.date, other.date)
if date_compare == 0:

View File

@ -156,13 +156,16 @@ $(document).ready(function() {
if (current_photo.model != undefined) text += "<tr><td>Camera Model</td><td>" + current_photo.model + "</td></tr>";
if (current_photo.date != undefined) text += "<tr><td>Time Taken</td><td>" + current_photo.date + "</td></tr>";
if (current_photo.size != undefined) text += "<tr><td>Resolution</td><td>" + current_photo.size[0] + " x " + current_photo.size[1] + "</td></tr>";
if (current_photo.shutterSpeed != undefined) text += "<tr><td>Shutter Speed</td><td>" + current_photo.shutterSpeed + "</td></tr>";
if (current_photo.aperture != undefined) text += "<tr><td>Aperture</td><td>" + current_photo.aperture + "</td></tr>";
if (current_photo.fStop != undefined) text += "<tr><td>F-Stop</td><td>" + current_photo.fStop + "</td></tr>";
if (current_photo.focalLength != undefined) text += "<tr><td>Focal Length</td><td>" + current_photo.focalLength + "</td></tr>";
if (current_photo.iso != undefined) text += "<tr><td>ISO Sensitivity</td><td>" + current_photo.iso + "</td></tr>";
if (current_photo.exposureCompenstaion != undefined) text += "<tr><td>Exposure Compenstation</td><td>" + current_photo.exposureCompenstaion + "</td></tr>";
if (current_photo.meteringMode != undefined) text += "<tr><td>Metering Mode</td><td>" + current_photo.meteringMode + "</td></tr>";
if (current_photo.flashFired != undefined) text += "<tr><td>Flash Fired</td><td>" + current_photo.flashFired + "</td></tr>";
if (current_photo.iso != undefined) text += "<tr><td>ISO</td><td>" + current_photo.iso + "</td></tr>";
if (current_photo.exposureTime != undefined) text += "<tr><td>Exposure Time</td><td>" + current_photo.exposureTime + "</td></tr>";
if (current_photo.exposureProgram != undefined) text += "<tr><td>Exposure Program</td><td>" + current_photo.exposureProgram + "</td></tr>";
if (current_photo.exposureCompensation != undefined) text += "<tr><td>Exposure Compensation</td><td>" + current_photo.exposureCompensation + "</td></tr>";
if (current_photo.exposureBiasValue != undefined) text += "<tr><td>Exposure Bias</td><td>" + current_photo.exposureBiasValue + "</td></tr>";
if (current_photo.spectralSensitivity != undefined) text += "<tr><td>Spectra lSensitivity</td><td>" + current_photo.spectralSensitivity + "</td></tr>";
if (current_photo.flash != undefined) text += "<tr><td>Flash</td><td>" + (current_photo.flash ? "Yes" : "No") + "</td></tr>";
if (current_photo.orientation != undefined) text += "<tr><td>Orientation</td><td>" + current_photo.orientation + "</td></tr>";
text += "</table>";
$("#metadata").html(text);