def test_green(self): # l= 50 (/100), a = -100 (-128 .. 128) b=0 in PS # == RGB: 0, 152, 117 i = Image.open('Tests/images/lab-green.tif') k = i.getpixel((0, 0)) self.assertEqual(k, (128, 28, 128))
def test_red(self): # l= 50 (/100), a = 100 (-128 .. 128) b=0 in PS # == RGB: 255, 0, 124 i = Image.open('Tests/images/lab-red.tif') k = i.getpixel((0, 0)) self.assertEqual(k, (128, 228, 128))
def test_cmyk(self): # Test CMYK handling. Thanks to Tim and Charlie for test data, # Michael for getting me to look one more time. f = "Tests/images/pil_sample_cmyk.jpg" im = Image.open(f) # the source image has red pixels in the upper left corner. c, m, y, k = [x / 255.0 for x in im.getpixel((0, 0))] self.assertEqual(c, 0.0) self.assertGreater(m, 0.8) self.assertGreater(y, 0.8) self.assertEqual(k, 0.0) # the opposite corner is black c, m, y, k = [ x / 255.0 for x in im.getpixel((im.size[0] - 1, im.size[1] - 1)) ] self.assertGreater(k, 0.9) # roundtrip, and check again im = self.roundtrip(im) c, m, y, k = [x / 255.0 for x in im.getpixel((0, 0))] self.assertEqual(c, 0.0) self.assertGreater(m, 0.8) self.assertGreater(y, 0.8) self.assertEqual(k, 0.0) c, m, y, k = [ x / 255.0 for x in im.getpixel((im.size[0] - 1, im.size[1] - 1)) ] self.assertGreater(k, 0.9)
def test_exception(self): # Set limit to trigger exception on the test file Image.MAX_IMAGE_PIXELS = 64 * 128 - 1 self.assertEqual(Image.MAX_IMAGE_PIXELS, 64 * 128 - 1) self.assertRaises(Image.DecompressionBombError, lambda: Image.open(TEST_FILE))
def test_exif_rollback(self): # rolling back exif support in 3.1 to pre-3.0 formatting. # expected from 2.9, with b/u qualifiers switched for 3.2 compatibility # this test passes on 2.9 and 3.1, but not 3.0 expected_exif = { 34867: 4294967295, 258: (24, 24, 24), 36867: '2099:09:29 10:10:10', 34853: { 0: b'\x00\x00\x00\x01', 2: (4294967295, 1), 5: b'\x01', 30: 65535, 29: '1999:99:99 99:99:99' }, 296: 65535, 34665: 185, 41994: 65535, 514: 4294967295, 271: 'Make', 272: 'XXX-XXX', 305: 'PIL2', 42034: ((1, 1), (1, 1), (1, 1), (1, 1)), 42035: 'LensMake', 34856: b'\xaa\xaa\xaa\xaa\xaa\xaa', 282: (4294967295, 1), 33434: (4294967295, 1) } im = Image.open('Tests/images/exif_gps.jpg') exif = im._getexif() for tag, value in expected_exif.items(): self.assertEqual(value, exif[tag])
def test_truncated_jpeg_should_read_all_the_data(self): filename = "Tests/images/truncated_jpeg.jpg" ImageFile.LOAD_TRUNCATED_IMAGES = True im = Image.open(filename) im.load() ImageFile.LOAD_TRUNCATED_IMAGES = False self.assertIsNotNone(im.getbbox())
def test_roundtrip_dpi(self): # Check dpi roundtripping im = Image.open(TEST_PNG_FILE) im = roundtrip(im, dpi=(100, 100)) self.assertEqual(im.info["dpi"], (100, 100))
def test_icc(self): # Test ICC support im1 = Image.open("Tests/images/rgb.jpg") icc_profile = im1.info["icc_profile"] self.assertEqual(len(icc_profile), 3144) # Roundtrip via physical file. f = self.tempfile("temp.jpg") im1.save(f, icc_profile=icc_profile) im2 = Image.open(f) self.assertEqual(im2.info.get("icc_profile"), icc_profile) # Roundtrip via memory buffer. im1 = self.roundtrip(hopper()) im2 = self.roundtrip(hopper(), icc_profile=icc_profile) self.assert_image_equal(im1, im2) self.assertFalse(im1.info.get("icc_profile")) self.assertTrue(im2.info.get("icc_profile"))
def test_contextmanager(self): fn = None with Image.open("Tests/images/hopper.gif") as im: fn = im.fp.fileno() os.fstat(fn) self.assertRaises(OSError, os.fstat, fn)
def test_resize(self): # Arrange image1 = Image.open(file1) image2 = Image.open(file2) image3 = Image.open("Tests/images/illu10_preview.eps") new_size = (100, 100) # Act image1 = image1.resize(new_size) image2 = image2.resize(new_size) image3 = image3.resize(new_size) # Assert self.assertEqual(image1.size, new_size) self.assertEqual(image2.size, new_size) self.assertEqual(image3.size, new_size)
def test_write_rgb(self): """ Can we write a RGB mode file to webp without error. Does it have the bits we expect? """ temp_file = self.tempfile("temp.webp") hopper(self.rgb_mode).save(temp_file) image = Image.open(temp_file) self.assertEqual(image.mode, self.rgb_mode) self.assertEqual(image.size, (128, 128)) self.assertEqual(image.format, "WEBP") image.load() image.getdata() # generated with: dwebp -ppm temp.webp -o hopper_webp_write.ppm self.assert_image_similar_tofile(image, 'Tests/images/hopper_webp_write.ppm', 12.0) # This test asserts that the images are similar. If the average pixel # difference between the two images is less than the epsilon value, # then we're going to accept that it's a reasonable lossy version of # the image. The old lena images for WebP are showing ~16 on # Ubuntu, the jpegs are showing ~18. target = hopper(self.rgb_mode) self.assert_image_similar(image, target, 12.0)
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_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_questionable(self): """ These shouldn't crash/dos, but it's not well defined that these are in spec """ supported = [ "pal8os2v2.bmp", "rgb24prof.bmp", "pal1p1.bmp", "pal8offs.bmp", "rgb24lprof.bmp", "rgb32fakealpha.bmp", "rgb24largepal.bmp", "pal8os2sp.bmp", "rgb32bf-xbgr.bmp", ] for f in self.get_files('q'): try: im = Image.open(f) im.load() if os.path.basename(f) not in supported: print( "Please add %s to the partially supported bmp specs." % f) except Exception: # as msg: if os.path.basename(f) in supported: raise
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_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_draw(self): tempname = self.save_font() font = ImageFont.load(tempname) im = Image.new("L", (130, 30), "white") draw = ImageDraw.Draw(im) draw.text((0, 0), message, 'black', font=font) with Image.open('Tests/images/test_draw_pbm_target.png') as target: self.assert_image_similar(im, target, 0)
def _test_high_characters(self, message): tempname = self.save_font() font = ImageFont.load(tempname) im = Image.new("L", (750, 30), "white") draw = ImageDraw.Draw(im) draw.text((0, 0), message, "black", font=font) with Image.open('Tests/images/high_ascii_chars.png') as target: self.assert_image_similar(im, target, 0)
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_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_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 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 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_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_background(self): out = self.tempfile('temp.gif') im = Image.new('L', (100, 100), '#000') im.info['background'] = 1 im.save(out) reread = Image.open(out) self.assertEqual(reread.info['background'], im.info['background'])
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_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
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)