def test_landscape_crop(self): def mean_pixel(x, y): values = im.getpixel((x, y)) if not isinstance(values, (tuple, list)): values = [values] return sum(values) / len(values) for crop in ('center', '50% 200%', '50px 700px'): th = self.BACKEND.get_thumbnail(self.landscape, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) self.assertEqual(mean_pixel(0, 50), 255) self.assertEqual(mean_pixel(45, 50), 255) self.assertEqual(250 < mean_pixel(49, 50) <= 255, True) self.assertEqual(mean_pixel(55, 50), 0) self.assertEqual(mean_pixel(99, 50), 0) for crop in ('left', '0%', '0px'): th = self.BACKEND.get_thumbnail(self.landscape, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) for x in range(0, 99, 10): for y in range(0, 99, 10): self.assertEqual(250 < mean_pixel(x, y) <= 255, True) for crop in ('right', '100%', '100px'): th = self.BACKEND.get_thumbnail(self.landscape, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) coords = ((x, y) for y in range(0, 99, 10) for x in range(0, 99, 10)) for x, y in coords: self.assertEqual(0 <= mean_pixel(x, y) < 5, True)
def test_portrait_crop(self): def mean_pixel(x, y): values = im.getpixel((x, y)) if not isinstance(values, (tuple, list)): values = [values] return sum(values) / len(values) for crop in ('center', '88% 50%', '50px'): th = self.BACKEND.get_thumbnail(self.portrait, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) self.assertEqual(mean_pixel(50, 0), 255) self.assertEqual(mean_pixel(50, 45), 255) self.assertEqual(250 <= mean_pixel(50, 49) <= 255, True, mean_pixel(50, 49)) self.assertEqual(mean_pixel(50, 55), 0) self.assertEqual(mean_pixel(50, 99), 0) for crop in ('top', '0%', '0px'): th = self.BACKEND.get_thumbnail(self.portrait, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) for x in range(0, 99, 10): for y in range(0, 99, 10): self.assertEqual(250 < mean_pixel(x, y) <= 255, True) for crop in ('bottom', '100%', '100px'): th = self.BACKEND.get_thumbnail(self.portrait, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) for x in range(0, 99, 10): for y in range(0, 99, 10): self.assertEqual(0 <= mean_pixel(x, y) < 5, True)
def testLandscapeCrop(self): def mean_pixel(x, y): values = im.getpixel((x, y)) if not isinstance(values, (tuple, list)): values = [values] return reduce(operator.add, values) / len(values) for crop in ('center', '50% 200%', '50px 700px'): th = self.backend.get_thumbnail(self.landscape, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) self.assertEqual(mean_pixel(0, 50), 255) self.assertEqual(mean_pixel(45, 50), 255) self.assertEqual(250 < mean_pixel(49, 50) <= 255, True) self.assertEqual(mean_pixel(55, 50), 0) self.assertEqual(mean_pixel(99, 50), 0) for crop in ('left', '0%', '0px'): th = self.backend.get_thumbnail(self.landscape, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) for x in xrange(0, 99, 10): for y in xrange(0, 99, 10): self.assertEqual(250 < mean_pixel(x, y) <= 255, True) for crop in ('right', '100%', '100px'): th = self.backend.get_thumbnail(self.landscape, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) for x in xrange(0, 99, 10): for y in xrange(0, 99, 10): self.assertEqual(0 <= mean_pixel(x, y) < 5, True)
def test_portrait_crop(self): def mean_pixel(x, y): values = im.getpixel((x, y)) if not isinstance(values, (tuple, list)): values = [values] return sum(values) / len(values) for crop in ('center', '88% 50%', '50px'): th = self.backend.get_thumbnail(self.portrait, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) self.assertEqual(mean_pixel(50, 0), 255) self.assertEqual(mean_pixel(50, 45), 255) self.assertEqual(250 <= mean_pixel(50, 49) <= 255, True, mean_pixel(50, 49)) self.assertEqual(mean_pixel(50, 55), 0) self.assertEqual(mean_pixel(50, 99), 0) for crop in ('top', '0%', '0px'): th = self.backend.get_thumbnail(self.portrait, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) for x in range(0, 99, 10): for y in range(0, 99, 10): self.assertEqual(250 < mean_pixel(x, y) <= 255, True) for crop in ('bottom', '100%', '100px'): th = self.backend.get_thumbnail(self.portrait, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) for x in range(0, 99, 10): for y in range(0, 99, 10): self.assertEqual(0 <= mean_pixel(x, y) < 5, True)
def PIL_test_landscape_crop(self): def mean_pixel(x, y): values = im.getpixel((x, y)) if not isinstance(values, (tuple, list)): values = [values] return sum(values) / len(values) # Center th = self.BACKEND.get_thumbnail(self.landscape, '100x100', cropbox="50,0,150,100") engine = PILEngine() im = engine.get_image(th) self.assertEqual(mean_pixel(0, 50), 255) self.assertEqual(mean_pixel(45, 50), 255) self.assertEqual(250 < mean_pixel(49, 50) <= 255, True) self.assertEqual(mean_pixel(55, 50), 0) self.assertEqual(mean_pixel(99, 50), 0) # Left th = self.BACKEND.get_thumbnail(self.landscape, '100x100', cropbox="0,0,100,100") engine = PILEngine() im = engine.get_image(th) for x in range(0, 99, 10): for y in range(0, 99, 10): self.assertEqual(250 < mean_pixel(x, y) <= 255, True) # Right th = self.BACKEND.get_thumbnail(self.landscape, '100x100', cropbox="100,0,200,100") engine = PILEngine() im = engine.get_image(th) coords = ((x, y) for y in range(0, 99, 10) for x in range(0, 99, 10)) for x, y in coords: self.assertEqual(0 <= mean_pixel(x, y) < 5, True)
def PIL_test_portrait_crop(self): def mean_pixel(x, y): values = im.getpixel((x, y)) if not isinstance(values, (tuple, list)): values = [values] return sum(values) / len(values) # Center Crop th = self.BACKEND.get_thumbnail(self.portrait, '100x100', cropbox="0,50,100,150") engine = PILEngine() im = engine.get_image(th) # Top half should be color, bottom not self.assertEqual(mean_pixel(0, 0), 255) self.assertEqual(mean_pixel(50, 0), 255) self.assertEqual(mean_pixel(50, 45), 255) self.assertEqual(mean_pixel(50, 55), 0) self.assertEqual(mean_pixel(50, 99), 0) # Top Crop th = self.BACKEND.get_thumbnail(self.portrait, '100x100', cropbox="0,0,100,100") engine = PILEngine() im = engine.get_image(th) for x in range(0, 99, 10): for y in range(0, 99, 10): self.assertEqual(250 < mean_pixel(x, y) <= 255, True) # Bottom Crop th = self.BACKEND.get_thumbnail(self.portrait, '100x100', cropbox="0,100,100,200") engine = PILEngine() im = engine.get_image(th) for x in range(0, 99, 10): for y in range(0, 99, 10): self.assertEqual(0 <= mean_pixel(x, y) < 5, True)
def test_landscape_crop(self): def mean_pixel(x, y): values = im.getpixel((x, y)) if not isinstance(values, (tuple, list)): values = [values] return sum(values) / len(values) for crop in ('center', '50% 200%', '50px 700px'): th = self.backend.get_thumbnail(self.landscape, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) self.assertEqual(mean_pixel(0, 50), 255) self.assertEqual(mean_pixel(45, 50), 255) self.assertEqual(250 < mean_pixel(49, 50) <= 255, True) self.assertEqual(mean_pixel(55, 50), 0) self.assertEqual(mean_pixel(99, 50), 0) for crop in ('left', '0%', '0px'): th = self.backend.get_thumbnail(self.landscape, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) for x in range(0, 99, 10): for y in range(0, 99, 10): self.assertEqual(250 < mean_pixel(x, y) <= 255, True) for crop in ('right', '100%', '100px'): th = self.backend.get_thumbnail(self.landscape, '100x100', crop=crop) engine = PILEngine() im = engine.get_image(th) for x in range(0, 99, 10): for y in range(0, 99, 10): self.assertEqual(0 <= mean_pixel(x, y) < 5, True)
def testLandscapeCrop(self): def mean_pixel(x, y): values = im.getpixel((x, y)) if not isinstance(values, (tuple, list)): values = [values] return reduce(operator.add, values) / len(values) for crop in ("center", "50% 200%", "50px 700px"): th = self.backend.get_thumbnail(self.landscape, "100x100", crop=crop) engine = PILEngine() im = engine.get_image(th) self.assertEqual(mean_pixel(0, 50), 255) self.assertEqual(mean_pixel(45, 50), 255) self.assertEqual(250 < mean_pixel(49, 50) <= 255, True) self.assertEqual(mean_pixel(55, 50), 0) self.assertEqual(mean_pixel(99, 50), 0) for crop in ("left", "0%", "0px"): th = self.backend.get_thumbnail(self.landscape, "100x100", crop=crop) engine = PILEngine() im = engine.get_image(th) for x in xrange(0, 99, 10): for y in xrange(0, 99, 10): self.assertEqual(250 < mean_pixel(x, y) <= 255, True) for crop in ("right", "100%", "100px"): th = self.backend.get_thumbnail(self.landscape, "100x100", crop=crop) engine = PILEngine() im = engine.get_image(th) for x in xrange(0, 99, 10): for y in xrange(0, 99, 10): self.assertEqual(0 <= mean_pixel(x, y) < 5, True)
def testPortraitCrop(self): def mean_pixel(x, y): values = im.getpixel((x, y)) if not isinstance(values, (tuple, list)): values = [values] return reduce(operator.add, values) / len(values) for crop in ("center", "88% 50%", "50px"): th = self.backend.get_thumbnail(self.portrait, "100x100", crop=crop) engine = PILEngine() im = engine.get_image(th) self.assertEqual(mean_pixel(50, 0), 255) self.assertEqual(mean_pixel(50, 45), 255) self.assertEqual(250 < mean_pixel(50, 49) <= 255, True) self.assertEqual(mean_pixel(50, 55), 0) self.assertEqual(mean_pixel(50, 99), 0) for crop in ("top", "0%", "0px"): th = self.backend.get_thumbnail(self.portrait, "100x100", crop=crop) engine = PILEngine() im = engine.get_image(th) for x in xrange(0, 99, 10): for y in xrange(0, 99, 10): self.assertEqual(250 < mean_pixel(x, y) <= 255, True) for crop in ("bottom", "100%", "100px"): th = self.backend.get_thumbnail(self.portrait, "100x100", crop=crop) engine = PILEngine() im = engine.get_image(th) for x in xrange(0, 99, 10): for y in xrange(0, 99, 10): self.assertEqual(0 <= mean_pixel(x, y) < 5, True)
def test_orientation(self): ref = Image.open(pjoin(DATA_DIR, '1_topleft.jpg')) top = ref.getpixel((14, 7)) left = ref.getpixel((7, 14)) engine = PILEngine() def epsilon(x, y): if isinstance(x, (tuple, list)): x = sum(x) / len(x) if isinstance(y, (tuple, list)): y = sum(y) / len(y) return abs(x - y) data_images = ( '1_topleft.jpg', '2_topright.jpg', '3_bottomright.jpg', '4_bottomleft.jpg', '5_lefttop.jpg', '6_righttop.jpg', '7_rightbottom.jpg', '8_leftbottom.jpg' ) for name in data_images: th = self.backend.get_thumbnail('data/%s' % name, '30x30') im = engine.get_image(th) self.assertLess(epsilon(top, im.getpixel((14, 7))), 10) self.assertLess(epsilon(left, im.getpixel((7, 14))), 10) exif = im._getexif() if exif: self.assertEqual(exif.get(0x0112), 1)
def test_retina(self): get_thumbnail(self.image, '50x50') actions = [ 'exists: test/cache/91/bb/91bb06cf9169e4c52132bb113f2d4c0d.jpg', # save regular resolution, same as in StorageTestCase 'open: retina.jpg', 'save: test/cache/91/bb/91bb06cf9169e4c52132bb113f2d4c0d.jpg', 'get_available_name: test/cache/91/bb/91bb06cf9169e4c52132bb113f2d4c0d.jpg', 'exists: test/cache/91/bb/91bb06cf9169e4c52132bb113f2d4c0d.jpg', # save the 1.5x resolution version 'save: test/cache/91/bb/[email protected]', 'get_available_name: test/cache/91/bb/[email protected]', 'exists: test/cache/91/bb/[email protected]', # save the 2x resolution version 'save: test/cache/91/bb/[email protected]', 'get_available_name: test/cache/91/bb/[email protected]', 'exists: test/cache/91/bb/[email protected]' ] self.assertEqual(self.log, actions) path = os.path.join(settings.MEDIA_ROOT, 'test/cache/91/bb/[email protected]') with open(path) as fp: engine = PILEngine() self.assertEqual(engine.get_image_size(engine.get_image(ImageFile(file_=fp))), (75, 75))
def test_retina(self): get_thumbnail(self.image, '50x50') actions = [ 'exists: test/cache/91/bb/91bb06cf9169e4c52132bb113f2d4c0d.jpg', # save regular resolution, same as in StorageTestCase 'open: retina.jpg', 'save: test/cache/91/bb/91bb06cf9169e4c52132bb113f2d4c0d.jpg', 'get_available_name: test/cache/91/bb/91bb06cf9169e4c52132bb113f2d4c0d.jpg', 'exists: test/cache/91/bb/91bb06cf9169e4c52132bb113f2d4c0d.jpg', # save the 1.5x resolution version 'save: test/cache/91/bb/[email protected]', 'get_available_name: test/cache/91/bb/[email protected]', 'exists: test/cache/91/bb/[email protected]', # save the 2x resolution version 'save: test/cache/91/bb/[email protected]', 'get_available_name: test/cache/91/bb/[email protected]', 'exists: test/cache/91/bb/[email protected]' ] self.assertEqual(self.log, actions) path = os.path.join( settings.MEDIA_ROOT, 'test/cache/91/bb/[email protected]') with open(path) as fp: engine = PILEngine() self.assertEqual( engine.get_image_size(engine.get_image(ImageFile(file_=fp))), (75, 75))
def test_retina(self): get_thumbnail(self.im, '50x50') actions = [ 'exists: test/cache/19/10/1910dc350bbe9ee55fd9d8d3d5e38e19.jpg', # save regular resolution, same as in StorageTestCase 'open: retina.jpg', 'save: test/cache/19/10/1910dc350bbe9ee55fd9d8d3d5e38e19.jpg', 'get_available_name: test/cache/19/10/1910dc350bbe9ee55fd9d8d3d5e38e19.jpg', 'exists: test/cache/19/10/1910dc350bbe9ee55fd9d8d3d5e38e19.jpg', # save the 1.5x resolution version 'save: test/cache/19/10/[email protected]', 'get_available_name: test/cache/19/10/[email protected]', 'exists: test/cache/19/10/[email protected]', # save the 2x resolution version 'save: test/cache/19/10/[email protected]', 'get_available_name: test/cache/19/10/[email protected]', 'exists: test/cache/19/10/[email protected]' ] self.assertEqual(self.log, actions) p = pjoin( settings.MEDIA_ROOT, 'test/cache/19/10/[email protected]') with open(p) as fp: engine = PILEngine() self.assertEqual( engine.get_image_size(engine.get_image(ImageFile(file_=fp))), (75, 75))
def test_orientation(self): ref = Image.open(os.path.join(DATA_DIR, '1_topleft.jpg')) top = ref.getpixel((14, 7)) left = ref.getpixel((7, 14)) engine = PILEngine() def epsilon(x, y): if isinstance(x, (tuple, list)): x = sum(x) / len(x) if isinstance(y, (tuple, list)): y = sum(y) / len(y) return abs(x - y) data_images = ('1_topleft.jpg', '2_topright.jpg', '3_bottomright.jpg', '4_bottomleft.jpg', '5_lefttop.jpg', '6_righttop.jpg', '7_rightbottom.jpg', '8_leftbottom.jpg') for name in data_images: th = self.BACKEND.get_thumbnail('data/%s' % name, '30x30') im = engine.get_image(th) self.assertLess(epsilon(top, im.getpixel((14, 7))), 10) self.assertLess(epsilon(left, im.getpixel((7, 14))), 10) exif = im._getexif() # no exif editor in GraphicsMagick if exif and not (settings.THUMBNAIL_CONVERT.endswith('gm convert') or 'pgmagick_engine' in settings.THUMBNAIL_ENGINE): self.assertEqual(exif.get(0x0112), 1)
def test_orientation(self): ref = Image.open(pjoin(DATA_DIR, '1_topleft.jpg')) top = ref.getpixel((14, 7)) left = ref.getpixel((7, 14)) engine = PILEngine() def epsilon(x, y): if isinstance(x, (tuple, list)): x = sum(x) / len(x) if isinstance(y, (tuple, list)): y = sum(y) / len(y) return abs(x - y) data_images = ('1_topleft.jpg', '2_topright.jpg', '3_bottomright.jpg', '4_bottomleft.jpg', '5_lefttop.jpg', '6_righttop.jpg', '7_rightbottom.jpg', '8_leftbottom.jpg') for name in data_images: th = self.backend.get_thumbnail('data/%s' % name, '30x30') im = engine.get_image(th) self.assertLess(epsilon(top, im.getpixel((14, 7))), 10) self.assertLess(epsilon(left, im.getpixel((7, 14))), 10) exif = im._getexif() if exif: self.assertEqual(exif.get(0x0112), 1)
def test_retina(self): get_thumbnail(self.im, '50x50') actions = [ # save regular resolution, same as in StorageTestCase 'open: retina.jpg', 'save: test/cache/19/10/1910dc350bbe9ee55fd9d8d3d5e38e19.jpg', 'get_available_name: test/cache/19/10/1910dc350bbe9ee55fd9d8d3d5e38e19.jpg', 'exists: test/cache/19/10/1910dc350bbe9ee55fd9d8d3d5e38e19.jpg', # save the 1.5x resolution version 'save: test/cache/19/10/[email protected]', 'get_available_name: test/cache/19/10/[email protected]', 'exists: test/cache/19/10/[email protected]', # save the 2x resolution version 'save: test/cache/19/10/[email protected]', 'get_available_name: test/cache/19/10/[email protected]', 'exists: test/cache/19/10/[email protected]' ] self.assertEqual(self.log, actions) with open(pjoin(settings.MEDIA_ROOT, 'test/cache/19/10/[email protected]')) as fp: engine = PILEngine() self.assertEqual(engine.get_image_size(engine.get_image(ImageFile(file_=fp))), (75, 75))
def test_smart_crop(self): th = self.BACKEND.get_thumbnail('data/white_border.jpg', '32x32', crop='smart') self.assertEqual(th.x, 32) self.assertEqual(th.y, 32) engine = PILEngine() im = engine.get_image(th) self.assertEqual(im.size[0], 32) self.assertEqual(im.size[1], 32)
def _clean_image(self): from sorl.thumbnail.engines.pil_engine import Engine from sorl.thumbnail.images import ImageFile e = Engine() f = ImageFile(self.image.file) tmp_image = e.get_image(f) tmp_image = e._orientation(tmp_image) new_file = StringIO() tmp_image.save(new_file, 'jpeg') file_content = ContentFile(new_file.getvalue()) self.image.save(self.image.name, file_content, save=False)
def test_crop_image_with_icc_profile(self): name = 'data/icc_profile_test.jpg' item, _ = Item.objects.get_or_create(image=name) im = ImageFile(item.image) th = self.BACKEND.get_thumbnail(im, '100x100') engine = PILEngine() self.assertEqual( engine.get_image(im).info.get('icc_profile'), engine.get_image(th).info.get('icc_profile'))
def test_crop_image_with_icc_profile(self): name = 'data/icc_profile_test.jpg' item, _ = Item.objects.get_or_create(image=name) im = ImageFile(item.image) th = self.BACKEND.get_thumbnail(im, '100x100') engine = PILEngine() self.assertEqual( engine.get_image(im).info.get('icc_profile'), engine.get_image(th).info.get('icc_profile') )
def _clean_image(self): from sorl.thumbnail.engines.pil_engine import Engine from sorl.thumbnail.images import ImageFile e = Engine() f = ImageFile(self.image.file) tmp_image = e.get_image(f) tmp_image = e._orientation(tmp_image) tmp_image = tmp_image.convert('RGB') new_file = BytesIO() tmp_image.save(new_file, 'jpeg') file_content = ContentFile(new_file.getvalue()) self.image.save(self.image.name, file_content, save=False)
def test_orientation(self): data_dir = pjoin(settings.MEDIA_ROOT, 'data') shutil.copytree(settings.DATA_ROOT, data_dir) ref = Image.open(pjoin(data_dir, '1_topleft.jpg')) top = ref.getpixel((14, 7)) left = ref.getpixel((7, 14)) engine = PILEngine() def epsilon(x, y): if isinstance(x, (tuple, list)): x = sum(x) / len(x) if isinstance(y, (tuple, list)): y = sum(y) / len(y) return abs(x - y) for name in sorted(os.listdir(data_dir)): th = self.backend.get_thumbnail('data/%s' % name, '30x30') im = engine.get_image(th) self.assertLess(epsilon(top, im.getpixel((14, 7))), 10) self.assertLess(epsilon(left, im.getpixel((7, 14))), 10) exif = im._getexif() if exif: self.assertEqual(exif.get(0x0112), 1)
def test_orientation(self): ref = Image.open(os.path.join(DATA_DIR, '1_topleft.jpg')) top = ref.getpixel((14, 7)) left = ref.getpixel((7, 14)) engine = PILEngine() def epsilon(x, y): if isinstance(x, (tuple, list)): x = sum(x) / len(x) if isinstance(y, (tuple, list)): y = sum(y) / len(y) return abs(x - y) data_images = ( '1_topleft.jpg', '2_topright.jpg', '3_bottomright.jpg', '4_bottomleft.jpg', '5_lefttop.jpg', '6_righttop.jpg', '7_rightbottom.jpg', '8_leftbottom.jpg' ) for name in data_images: th = self.BACKEND.get_thumbnail('data/%s' % name, '30x30') im = engine.get_image(th) self.assertLess(epsilon(top, im.getpixel((14, 7))), 10) self.assertLess(epsilon(left, im.getpixel((7, 14))), 10) exif = im._getexif() # no exif editor in GraphicsMagick if exif and not (settings.THUMBNAIL_CONVERT.endswith('gm convert') or 'pgmagick_engine' in settings.THUMBNAIL_ENGINE): self.assertEqual(exif.get(0x0112), 1)