From 27d7bd10c87c24a9babcc3ebec1b78b5a37c02b0 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sun, 3 Dec 2023 21:59:44 +0200 Subject: [PATCH] Use list comprehensions to create transformed lists --- Tests/test_file_apng.py | 4 +--- src/PIL/FpxImagePlugin.py | 9 ++++---- src/PIL/Image.py | 16 +++++--------- src/PIL/ImageCms.py | 7 ++---- src/PIL/ImageOps.py | 8 ++----- src/PIL/ImagePalette.py | 20 ++++++----------- src/PIL/ImageQt.py | 18 ++++++---------- src/PIL/ImageStat.py | 42 +++++++++++------------------------- src/PIL/JpegImagePlugin.py | 4 +--- src/PIL/MicImagePlugin.py | 9 ++++---- src/PIL/PcfFontFile.py | 16 +++++--------- src/PIL/SpiderImagePlugin.py | 4 +--- 12 files changed, 50 insertions(+), 107 deletions(-) diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index d0c81b5e9d7..1fb97a789a5 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -356,9 +356,7 @@ def test_apng_save(tmp_path): assert im.getpixel((64, 32)) == (0, 255, 0, 255) with Image.open("Tests/images/apng/single_frame_default.png") as im: - frames = [] - for frame_im in ImageSequence.Iterator(im): - frames.append(frame_im.copy()) + frames = [frame_im.copy() for frame_im in ImageSequence.Iterator(im)] frames[0].save( test_file, save_all=True, default_image=True, append_images=frames[1:] ) diff --git a/src/PIL/FpxImagePlugin.py b/src/PIL/FpxImagePlugin.py index 3027ef45b6e..a0999130ea5 100644 --- a/src/PIL/FpxImagePlugin.py +++ b/src/PIL/FpxImagePlugin.py @@ -97,16 +97,15 @@ def _open_index(self, index=1): s = prop[0x2000002 | id] - colors = [] bands = i32(s, 4) if bands > 4: msg = "Invalid number of bands" raise OSError(msg) - for i in range(bands): - # note: for now, we ignore the "uncalibrated" flag - colors.append(i32(s, 8 + i * 4) & 0x7FFFFFFF) - self._mode, self.rawmode = MODES[tuple(colors)] + # note: for now, we ignore the "uncalibrated" flag + colors = tuple(i32(s, 8 + i * 4) & 0x7FFFFFFF for i in range(bands)) + + self._mode, self.rawmode = MODES[colors] # load JPEG tables, if any self.jpeg = {} diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 2853bd5964a..8c17292a747 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1288,9 +1288,9 @@ def filter(self, filter): if self.im.bands == 1 or multiband: return self._new(filter.filter(self.im)) - ims = [] - for c in range(self.im.bands): - ims.append(self._new(filter.filter(self.im.getband(c)))) + ims = [ + self._new(filter.filter(self.im.getband(c))) for c in range(self.im.bands) + ] return merge(self.mode, ims) def getbands(self): @@ -1339,10 +1339,7 @@ def getcolors(self, maxcolors=256): self.load() if self.mode in ("1", "L", "P"): h = self.im.histogram() - out = [] - for i in range(256): - if h[i]: - out.append((h[i], i)) + out = [(h[i], i) for i in range(256) if h[i]] if len(out) > maxcolors: return None return out @@ -1383,10 +1380,7 @@ def getextrema(self): self.load() if self.im.bands > 1: - extrema = [] - for i in range(self.im.bands): - extrema.append(self.im.getband(i).getextrema()) - return tuple(extrema) + return tuple(self.im.getband(i).getextrema() for i in range(self.im.bands)) return self.im.getextrema() def _getxmp(self, xmp_tags): diff --git a/src/PIL/ImageCms.py b/src/PIL/ImageCms.py index 3a337f9f209..0df3a4c6ce5 100644 --- a/src/PIL/ImageCms.py +++ b/src/PIL/ImageCms.py @@ -787,11 +787,8 @@ def getProfileInfo(profile): # info was description \r\n\r\n copyright \r\n\r\n K007 tag \r\n\r\n whitepoint description = profile.profile.profile_description cpright = profile.profile.copyright - arr = [] - for elt in (description, cpright): - if elt: - arr.append(elt) - return "\r\n\r\n".join(arr) + "\r\n\r\n" + elements = [element for element in (description, cpright) if element] + return "\r\n\r\n".join(elements) + "\r\n\r\n" except (AttributeError, OSError, TypeError, ValueError) as v: raise PyCMSError(v) from v diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index 4f83a4edb69..f316a307c95 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -557,9 +557,7 @@ def invert(image): :param image: The image to invert. :return: An image. """ - lut = [] - for i in range(256): - lut.append(255 - i) + lut = [255 - i for i in range(256)] return image.point(lut) if image.mode == "1" else _lut(image, lut) @@ -581,10 +579,8 @@ def posterize(image, bits): :param bits: The number of bits to keep for each channel (1-8). :return: An image. """ - lut = [] mask = ~(2 ** (8 - bits) - 1) - for i in range(256): - lut.append(i & mask) + lut = [i & mask for i in range(256)] return _lut(image, lut) diff --git a/src/PIL/ImagePalette.py b/src/PIL/ImagePalette.py index cb4f1dba115..f33635a30f0 100644 --- a/src/PIL/ImagePalette.py +++ b/src/PIL/ImagePalette.py @@ -200,21 +200,15 @@ def raw(rawmode, data): def make_linear_lut(black, white): - lut = [] if black == 0: - for i in range(256): - lut.append(white * i // 255) - else: - msg = "unavailable when black is non-zero" - raise NotImplementedError(msg) # FIXME - return lut + return [white * i // 255 for i in range(256)] + + msg = "unavailable when black is non-zero" + raise NotImplementedError(msg) # FIXME def make_gamma_lut(exp): - lut = [] - for i in range(256): - lut.append(int(((i / 255.0) ** exp) * 255.0 + 0.5)) - return lut + return [int(((i / 255.0) ** exp) * 255.0 + 0.5) for i in range(256)] def negative(mode="RGB"): @@ -226,9 +220,7 @@ def negative(mode="RGB"): def random(mode="RGB"): from random import randint - palette = [] - for i in range(256 * len(mode)): - palette.append(randint(0, 255)) + palette = [randint(0, 255) for i in range(256 * len(mode))] return ImagePalette(mode, palette) diff --git a/src/PIL/ImageQt.py b/src/PIL/ImageQt.py index d017565a9e4..56c1aa52576 100644 --- a/src/PIL/ImageQt.py +++ b/src/PIL/ImageQt.py @@ -103,12 +103,10 @@ def align8to32(bytes, width, mode): if not extra_padding: return bytes - new_data = [] - for i in range(len(bytes) // bytes_per_line): - new_data.append( - bytes[i * bytes_per_line : (i + 1) * bytes_per_line] - + b"\x00" * extra_padding - ) + new_data = [ + bytes[i * bytes_per_line : (i + 1) * bytes_per_line] + b"\x00" * extra_padding + for i in range(len(bytes) // bytes_per_line) + ] return b"".join(new_data) @@ -131,15 +129,11 @@ def _toqclass_helper(im): format = qt_format.Format_Mono elif im.mode == "L": format = qt_format.Format_Indexed8 - colortable = [] - for i in range(256): - colortable.append(rgb(i, i, i)) + colortable = [rgb(i, i, i) for i in range(256)] elif im.mode == "P": format = qt_format.Format_Indexed8 - colortable = [] palette = im.getpalette() - for i in range(0, len(palette), 3): - colortable.append(rgb(*palette[i : i + 3])) + colortable = [rgb(*palette[i : i + 3]) for i in range(0, len(palette), 3)] elif im.mode == "RGB": # Populate the 4th channel with 255 im = im.convert("RGBA") diff --git a/src/PIL/ImageStat.py b/src/PIL/ImageStat.py index b7ebddf066a..12747d546c8 100644 --- a/src/PIL/ImageStat.py +++ b/src/PIL/ImageStat.py @@ -61,18 +61,14 @@ def minmax(histogram): x = max(x, i) return n, x # returns (255, 0) if there's no data in the histogram - v = [] - for i in range(0, len(self.h), 256): - v.append(minmax(self.h[i:])) - return v + return [minmax(self.h[i:]) for i in range(0, len(self.h), 256)] def _getcount(self): """Get total number of pixels in each layer""" - - v = [] - for i in range(0, len(self.h), 256): - v.append(functools.reduce(operator.add, self.h[i : i + 256])) - return v + return [ + functools.reduce(operator.add, self.h[i : i + 256]) + for i in range(0, len(self.h), 256) + ] def _getsum(self): """Get sum of all pixels in each layer""" @@ -98,11 +94,7 @@ def _getsum2(self): def _getmean(self): """Get average pixel level for each layer""" - - v = [] - for i in self.bands: - v.append(self.sum[i] / self.count[i]) - return v + return [self.sum[i] / self.count[i] for i in self.bands] def _getmedian(self): """Get median pixel level for each layer""" @@ -121,28 +113,18 @@ def _getmedian(self): def _getrms(self): """Get RMS for each layer""" - - v = [] - for i in self.bands: - v.append(math.sqrt(self.sum2[i] / self.count[i])) - return v + return [math.sqrt(self.sum2[i] / self.count[i]) for i in self.bands] def _getvar(self): """Get variance for each layer""" - - v = [] - for i in self.bands: - n = self.count[i] - v.append((self.sum2[i] - (self.sum[i] ** 2.0) / n) / n) - return v + return [ + (self.sum2[i] - (self.sum[i] ** 2.0) / self.count[i]) / self.count[i] + for i in self.bands + ] def _getstddev(self): """Get standard deviation for each layer""" - - v = [] - for i in self.bands: - v.append(math.sqrt(self.var[i])) - return v + return [math.sqrt(self.var[i]) for i in self.bands] Global = Stat # compatibility diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index b8a5e7a5993..5add65f4542 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -233,9 +233,7 @@ def SOF(self, marker): # fixup icc profile self.icclist.sort() # sort by sequence number if self.icclist[0][13] == len(self.icclist): - profile = [] - for p in self.icclist: - profile.append(p[14:]) + profile = [p[14:] for p in self.icclist] icc_profile = b"".join(profile) else: icc_profile = None # wrong number of fragments diff --git a/src/PIL/MicImagePlugin.py b/src/PIL/MicImagePlugin.py index e4154902f1c..9300d354544 100644 --- a/src/PIL/MicImagePlugin.py +++ b/src/PIL/MicImagePlugin.py @@ -51,10 +51,11 @@ def _open(self): # find ACI subfiles with Image members (maybe not the # best way to identify MIC files, but what the... ;-) - self.images = [] - for path in self.ole.listdir(): - if path[1:] and path[0][-4:] == ".ACI" and path[1] == "Image": - self.images.append(path) + self.images = [ + path + for path in self.ole.listdir() + if path[1:] and path[0][-4:] == ".ACI" and path[1] == "Image" + ] # if we didn't find any images, this is probably not # an MIC file. diff --git a/src/PIL/PcfFontFile.py b/src/PIL/PcfFontFile.py index 8db5822fe7d..1f5727ecaed 100644 --- a/src/PIL/PcfFontFile.py +++ b/src/PIL/PcfFontFile.py @@ -129,9 +129,8 @@ def _load_properties(self): nprops = i32(fp.read(4)) # read property description - p = [] - for i in range(nprops): - p.append((i32(fp.read(4)), i8(fp.read(1)), i32(fp.read(4)))) + p = [(i32(fp.read(4)), i8(fp.read(1)), i32(fp.read(4))) for i in range(nprops)] + if nprops & 3: fp.seek(4 - (nprops & 3), io.SEEK_CUR) # pad @@ -186,8 +185,6 @@ def _load_bitmaps(self, metrics): # # bitmap data - bitmaps = [] - fp, format, i16, i32 = self._getformat(PCF_BITMAPS) nbitmaps = i32(fp.read(4)) @@ -196,13 +193,9 @@ def _load_bitmaps(self, metrics): msg = "Wrong number of bitmaps" raise OSError(msg) - offsets = [] - for i in range(nbitmaps): - offsets.append(i32(fp.read(4))) + offsets = [i32(fp.read(4)) for _ in range(nbitmaps)] - bitmap_sizes = [] - for i in range(4): - bitmap_sizes.append(i32(fp.read(4))) + bitmap_sizes = [i32(fp.read(4)) for _ in range(4)] # byteorder = format & 4 # non-zero => MSB bitorder = format & 8 # non-zero => MSB @@ -218,6 +211,7 @@ def _load_bitmaps(self, metrics): if bitorder: mode = "1" + bitmaps = [] for i in range(nbitmaps): xsize, ysize = metrics[i][:2] b, e = offsets[i : i + 2] diff --git a/src/PIL/SpiderImagePlugin.py b/src/PIL/SpiderImagePlugin.py index 408b982b515..9c06766ff4b 100644 --- a/src/PIL/SpiderImagePlugin.py +++ b/src/PIL/SpiderImagePlugin.py @@ -238,9 +238,7 @@ def makeSpiderHeader(im): if nvalues < 23: return [] - hdr = [] - for i in range(nvalues): - hdr.append(0.0) + hdr = [0.0 for _ in range(nvalues)] # NB these are Fortran indices hdr[1] = 1.0 # nslice (=1 for an image)