From: Vladimír Vondruš Date: Mon, 3 Jan 2022 00:39:35 +0000 (+0100) Subject: m.images: properly close images opened with PIL. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=d92004aac506dcca85795b1e50f349d22a1361a4;p=blog.git m.images: properly close images opened with PIL. Python 3.10 yells at me if I don't. Useful warning. (Ahah, C++ and your sane scoping rules, I miss you.) --- diff --git a/plugins/m/images.py b/plugins/m/images.py index a27e4c78..3e71ae13 100644 --- a/plugins/m/images.py +++ b/plugins/m/images.py @@ -109,8 +109,8 @@ class Image(Directive): if 'scale' in self.options: file = os.path.join(os.getcwd(), settings['INPUT']) absuri = os.path.join(file, reference.format(filename=file, static=file)) - im = PIL.Image.open(absuri) - width = "{}px".format(int(im.width*self.options['scale']/100.0)) + with PIL.Image.open(absuri) as im: + width = "{}px".format(int(im.width*self.options['scale']/100.0)) elif 'width' in self.options: width = self.options['width'] elif 'height' in self.options: @@ -226,30 +226,30 @@ class ImageGrid(rst.Directive): # also prepend the absolute path in case we're not Pelican file = os.path.join(os.getcwd(), settings['INPUT']) absuri = os.path.join(file, uri.format(filename=file, static=file)) - im = PIL.Image.open(absuri) # If no caption provided, get EXIF info, if it's there - if not caption and hasattr(im, '_getexif') and im._getexif() is not None: - exif = { - PIL.ExifTags.TAGS[k]: v - for k, v in im._getexif().items() - if k in PIL.ExifTags.TAGS and len(str(v)) < 256 - } - - # Not all info might be present - caption = [] - if 'FNumber' in exif: - numerator, denominator = _to_numerator_denominator_tuple(exif['FNumber']) - caption += ["F{}".format(float(numerator)/float(denominator))] - if 'ExposureTime' in exif: - numerator, denominator = _to_numerator_denominator_tuple(exif['ExposureTime']) - if int(numerator) > int(denominator): - caption += ["{} s".format(float(numerator)/float(denominator))] - else: - caption += ["{}/{} s".format(numerator, denominator)] - if 'ISOSpeedRatings' in exif: - caption += ["ISO {}".format(exif['ISOSpeedRatings'])] - caption = ', '.join(caption) + with PIL.Image.open(absuri) as im: + if not caption and hasattr(im, '_getexif') and im._getexif() is not None: + exif = { + PIL.ExifTags.TAGS[k]: v + for k, v in im._getexif().items() + if k in PIL.ExifTags.TAGS and len(str(v)) < 256 + } + + # Not all info might be present + caption = [] + if 'FNumber' in exif: + numerator, denominator = _to_numerator_denominator_tuple(exif['FNumber']) + caption += ["F{}".format(float(numerator)/float(denominator))] + if 'ExposureTime' in exif: + numerator, denominator = _to_numerator_denominator_tuple(exif['ExposureTime']) + if int(numerator) > int(denominator): + caption += ["{} s".format(float(numerator)/float(denominator))] + else: + caption += ["{}/{} s".format(numerator, denominator)] + if 'ISOSpeedRatings' in exif: + caption += ["ISO {}".format(exif['ISOSpeedRatings'])] + caption = ', '.join(caption) # If the caption is `..`, it's meant to be explicitly disabled if caption == '..': caption = ''