def gradient_L(self): gradient = Image.new('L', (self.size, self.size)) px = gradient.load() for y in range(gradient.height): for x in range(gradient.width): px[y, x] = (x + y) % 255 return gradient
def gradient_RGBa(self): return Image.merge('RGBa', [ self.gradient_L, self.gradient_L.transpose(Image.ROTATE_90), self.gradient_L.transpose(Image.ROTATE_180), self.gradient_L.transpose(Image.ROTATE_270), ])
def mask_1(self): mask = Image.new('1', (self.size, self.size)) px = mask.load() for y in range(mask.height): for x in range(mask.width): px[y, x] = (x + y) % 2 return mask
def apply(self, im, imOut=None): im.load() if imOut is None: imOut = Image.new(self.output_mode, im.size, None) self.transform.apply(im.im.id, imOut.im.id) imOut.info['icc_profile'] = self.output_profile.tobytes() return imOut
def test_sanity(self): for mode in ('RGB', 'RGBA', 'L', 'P', '1'): src = hopper(mode) data = ImageQt.toqimage(src) self.assertIsInstance(data, QImage) self.assertFalse(data.isNull()) # reload directly from the qimage rt = ImageQt.fromqimage(data) if mode in ('L', 'P', '1'): self.assert_image_equal(rt, src.convert('RGB')) else: self.assert_image_equal(rt, src) if mode == '1': # BW appears to not save correctly on QT4 and QT5 # kicks out errors on console: # libpng warning: Invalid color type/bit depth combination # in IHDR # libpng error: Invalid IHDR data continue # Test saving the file tempfile = self.tempfile('temp_{}.png'.format(mode)) data.save(tempfile) # Check that it actually worked. reloaded = Image.open(tempfile) # Gray images appear to come back in palette mode. # They're roughly equivalent if QT_VERSION == 4 and mode == 'L': src = src.convert('P') self.assert_image_equal(reloaded, src)
def test_correct_mode(self): im = Image.new('RGBA', (10, 10), 0) im.im.color_lut_3d('RGBA', Image.LINEAR, *self.generate_identity_table(3, 3)) im = Image.new('RGBA', (10, 10), 0) im.im.color_lut_3d('RGBA', Image.LINEAR, *self.generate_identity_table(4, 3)) im = Image.new('RGB', (10, 10), 0) im.im.color_lut_3d('HSV', Image.LINEAR, *self.generate_identity_table(3, 3)) im = Image.new('RGB', (10, 10), 0) im.im.color_lut_3d('RGBA', Image.LINEAR, *self.generate_identity_table(4, 3))
def test_sanity(self): if "zip_decoder" in codecs: tar = TarIO.TarIO(TEST_TAR_FILE, 'hopper.png') im = Image.open(tar) im.load() self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "PNG") if "jpeg_decoder" in codecs: tar = TarIO.TarIO(TEST_TAR_FILE, 'hopper.jpg') im = Image.open(tar) im.load() self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "JPEG")
def test_exif(self): for test_file in test_files: im = Image.open(test_file) info = im._getexif() self.assertEqual(info[272], 'Nintendo 3DS') self.assertEqual(info[296], 2) self.assertEqual(info[34665], 188)
def test_sanity(self): for test_file in test_files: im = Image.open(test_file) im.load() self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (640, 480)) self.assertEqual(im.format, "MPO")
def test_similar(self): text = 'EmbeddedBitmap' font_outline = ImageFont.truetype(font='Tests/fonts/DejaVuSans.ttf', size=24) font_bitmap = ImageFont.truetype( font='Tests/fonts/DejaVuSans-bitmap.ttf', size=24) size_outline = font_outline.getsize(text) size_bitmap = font_bitmap.getsize(text) size_final = (max(size_outline[0], size_bitmap[0]), max(size_outline[1], size_bitmap[1])) im_bitmap = Image.new('RGB', size_final, (255, 255, 255)) im_outline = im_bitmap.copy() draw_bitmap = ImageDraw.Draw(im_bitmap) draw_outline = ImageDraw.Draw(im_outline) # Metrics are different on the bitmap and ttf fonts, # more so on some platforms and versions of freetype than others. # Mac has a 1px difference, linux doesn't. draw_bitmap.text((0, size_final[1] - size_bitmap[1]), text, fill=(0, 0, 0), font=font_bitmap) draw_outline.text((0, size_final[1] - size_outline[1]), text, fill=(0, 0, 0), font=font_outline) self.assert_image_similar(im_bitmap, im_outline, 20)
def basic(mode): imIn = self.original.convert(mode) self.verify(imIn) w, h = imIn.size imOut = imIn.copy() self.verify(imOut) # copy imOut = imIn.transform((w, h), Image.EXTENT, (0, 0, w, h)) self.verify(imOut) # transform filename = self.tempfile("temp.im") imIn.save(filename) imOut = Image.open(filename) self.verify(imIn) self.verify(imOut) imOut = imIn.crop((0, 0, w, h)) self.verify(imOut) imOut = Image.new(mode, (w, h), None) imOut.paste(imIn.crop((0, 0, w // 2, h)), (0, 0)) imOut.paste(imIn.crop((w // 2, 0, w, h)), (w // 2, 0)) self.verify(imIn) self.verify(imOut) imIn = Image.new(mode, (1, 1), 1) self.assertEqual(imIn.getpixel((0, 0)), 1) imIn.putpixel((0, 0), 2) self.assertEqual(imIn.getpixel((0, 0)), 2) if mode == "L": maximum = 255 else: maximum = 32767 imIn = Image.new(mode, (1, 1), 256) self.assertEqual(imIn.getpixel((0, 0)), min(256, maximum)) imIn.putpixel((0, 0), 512) self.assertEqual(imIn.getpixel((0, 0)), min(512, maximum))
def test_getbands(self): self.assertEqual(Image.new("1", (1, 1)).getbands(), ("1", )) self.assertEqual(Image.new("L", (1, 1)).getbands(), ("L", )) self.assertEqual(Image.new("I", (1, 1)).getbands(), ("I", )) self.assertEqual(Image.new("F", (1, 1)).getbands(), ("F", )) self.assertEqual(Image.new("P", (1, 1)).getbands(), ("P", )) self.assertEqual(Image.new("RGB", (1, 1)).getbands(), ("R", "G", "B")) self.assertEqual( Image.new("RGBA", (1, 1)).getbands(), ("R", "G", "B", "A")) self.assertEqual( Image.new("CMYK", (1, 1)).getbands(), ("C", "M", "Y", "K")) self.assertEqual( Image.new("YCbCr", (1, 1)).getbands(), ("Y", "Cb", "Cr"))
def test_width_height(self): im = Image.new("RGB", (1, 2)) self.assertEqual(im.width, 1) self.assertEqual(im.height, 2) im.size = (3, 4) self.assertEqual(im.width, 3) self.assertEqual(im.height, 4)
def test_sanity(self): # Loading this icon by default should result in the largest size # (512x512@2x) being loaded im = Image.open(TEST_FILE) im.load() self.assertEqual(im.mode, "RGBA") self.assertEqual(im.size, (1024, 1024)) self.assertEqual(im.format, "ICNS")
def test_break_one_at_end(self): im = Image.new('L', (256, 5)) px = im.load() for y in range(5): for x in range(256): px[x, y] = x % 128 px[0, 3] = 128 + 64 self._test_buffer_overflow(im)
def test_dispose_previous(self): img = Image.open("Tests/images/dispose_prev.gif") try: while True: img.seek(img.tell() + 1) self.assertEqual(img.disposal_method, 3) except EOFError: pass
def test_mode_F(self): src = hopper('L') data = list(src.getdata()) im = Image.new('F', src.size, 0) im.putdata(data, 2.0, 256.0) target = [2.0 * float(elt) + 256.0 for elt in data] self.assertEqual(list(im.getdata()), target)
def test_sampleformat(self): # https://github.com/python-pillow/Pillow2/issues/1466 im = Image.open("Tests/images/copyleft.tiff") self.assertEqual(im.mode, 'RGB') self.assert_image_equal_tofile(im, "Tests/images/copyleft.png", mode='RGB')
def test_g4_tiff(self): """Test the ordinary file path load path""" test_file = "Tests/images/hopper_g4_500.tif" im = Image.open(test_file) self.assertEqual(im.size, (500, 500)) self._assert_noerr(im)
def open_withImagemagick(self, f): if not imagemagick_available(): raise IOError() outfile = self.tempfile("temp.png") if command_succeeds([IMCONVERT, f, outfile]): return Image.open(outfile) raise IOError()
def test_open_windows_v1(self): # Arrange # Act im = Image.open(TEST_FILE) # Assert self.assert_image_equal(im, hopper("1")) self.assertIsInstance(im, MspImagePlugin.MspImageFile)
def test_valid_file(self): # Arrange # https://ghrc.nsstc.nasa.gov/hydro/details/cmx3g8 # https://ghrc.nsstc.nasa.gov/pub/fieldCampaigns/camex3/cmx3g8/browse/ test_file = "Tests/images/cmx3g8_wv_1998.260_0745_mcidas.ara" saved_file = "Tests/images/cmx3g8_wv_1998.260_0745_mcidas.png" # Act im = Image.open(test_file) im.load() # Assert self.assertEqual(im.format, "MCIDAS") self.assertEqual(im.mode, "I") self.assertEqual(im.size, (1800, 400)) im2 = Image.open(saved_file) self.assert_image_equal(im, im2)
def save_bytesio(compression=None): buffer_io = io.BytesIO() pilim.save(buffer_io, format="tiff", compression=compression) buffer_io.seek(0) pilim_load = Image.open(buffer_io) self.assert_image_similar(pilim, pilim_load, 0)
def test_multiline_width(self): ttf = self.get_font() im = Image.new(mode='RGB', size=(300, 100)) draw = ImageDraw.Draw(im) self.assertEqual( draw.textsize("longest line", font=ttf)[0], draw.multiline_textsize("longest line\nline", font=ttf)[0])
def test_lzw(self): im = Image.open("Tests/images/hopper_lzw.tif") self.assertEqual(im.mode, 'RGB') self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "TIFF") im2 = hopper() self.assert_image_similar(im, im2, 5)
def test_roundtrip(self): for mode in ["RGB", "P"]: out = self.tempfile('temp.im') im = hopper(mode) im.save(out) reread = Image.open(out) self.assert_image_equal(reread, im)
def test_mode_i(self): src = hopper('L') data = list(src.getdata()) im = Image.new('I', src.size, 0) im.putdata(data, 2, 256) target = [2 * elt + 256 for elt in data] self.assertEqual(list(im.getdata()), target)
def test_roundtrip_save_all(self): # Single frame image out = self.tempfile('temp.gif') im = hopper() im.save(out, save_all=True) reread = Image.open(out) self.assert_image_similar(reread.convert('RGB'), im, 50) # Multiframe image im = Image.open("Tests/images/dispose_bgnd.gif") out = self.tempfile('temp.gif') im.save(out, save_all=True) reread = Image.open(out) self.assertEqual(reread.n_frames, 5)
def test_seek(self): im = Image.open(TEST_FILE) im.seek(0) self.assertEqual(im.tell(), 0) self.assertRaises(EOFError, im.seek, 99) self.assertEqual(im.tell(), 0)
def test_dispose_background(self): img = Image.open("Tests/images/dispose_bgnd.gif") try: while True: img.seek(img.tell() + 1) self.assertEqual(img.disposal_method, 2) except EOFError: pass