def test_create_thumb(self): """ create_thumb should generate a thumbnail of specified size and quality. * result should be, e.g., /path/to/thumb/thumb-245x136.jpg """ from garage.image_utils import ( create_thumb, get_image_size, get_img_ext, ) from garage.utils import delete_file self._msg('test', 'create_thumb', first=True) tempdir = tempfile.gettempdir() path, fname, w, h, ext = self._get_test_image('rodents') w, h = 245, 138 quality = 75 dst = tempdir fbase = 'rodents' thumb = create_thumb(path, w, h, quality, dst, fbase, ext) width, height = get_image_size(thumb) fext = get_img_ext(thumb) self.assertTrue(os.path.isfile(thumb)) self.assertEqual((width, height), (w, h)) self.assertEqual(fext, ext) self._msg('original', fname) self._msg('thumb', thumb) self._msg('width', width) self._msg('height', height) self._msg('ext', fext) self.assertTrue(delete_file(thumb))
def test_generate_thumb(self): """ generate_thumb is a wrapper function for create_thumb, so should bahave identically with the same parameters. * result should be, e.g., /path/to/thumb/thumb-245x136.jpg """ from garage.image_utils import ( generate_thumb, get_image_size, get_img_ext, ) from garage.utils import delete_file self._msg('test', 'generate_thumb', first=True) tempdir = tempfile.gettempdir() path, fname, w, h, ext = self._get_test_image('rodents') w, h = 245, 138 quality = 75 dst = tempdir thumb = generate_thumb(path, w, h, quality, dst) width, height = get_image_size(thumb) fext = get_img_ext(thumb) self.assertTrue(os.path.isfile(thumb)) self.assertEqual((width, height), (w, h)) self.assertEqual(fext, ext) self._msg('original', fname) self._msg('thumb', thumb) self._msg('width', width) self._msg('height', height) self._msg('ext', fext) self.assertTrue(delete_file(thumb))
def test_resize_image(self): """ resize_image should downsample an image and resize according to input dimensions. """ from garage.image_utils import ( resize_image, get_img_ext, get_file_basename, get_image_size, ) from garage.utils import delete_file self._msg('test', 'resize_image', first=True) tempdir = tempfile.gettempdir() path, fname, w, h, ext = self._get_test_image('rodents') fbase = 'rodents' w, h = 500, 280 dst = '%s-%dx%d.%s' % (os.path.join(tempdir, fbase), w, h, ext) img = Image.open(path) result = resize_image(img, (w, h), True) result.save(dst, quality=75) self.assertTrue(os.path.isfile(dst)) width, height = get_image_size(dst) fext = get_img_ext(dst) self.assertEqual((width, height), (w, h)) self.assertEqual(fext, ext) self._msg('original', dst) self._msg('resized', result) self._msg('width', width) self._msg('height', height) self._msg('ext', fext) self.assertTrue(delete_file(dst))
def test_get_image_size(self): """ get_image_size should return images's width x height as tuple. """ from garage.image_utils import get_image_size self._msg('test', 'get_image_size', first=True) path, fname, w, h, ext = self._get_test_image() width, height = get_image_size(path) self.assertEqual(width, w) self.assertEqual(height, h) self._msg('img', fname) self._msg('width', width) self._msg('height', height)
def resize_original_image(img_path, max_width=0, max_height=0, quality=DEFAULT_IMAGE_QUALITY, op=DEFAULT_IMG_OP, addwh=False, dup=False): """ resize image to below max width or max height. * NOTE: uses local file system for copies and storage. * set max_width to 0 or max_height to 0 to disable resizing width or height. * resizes to RGB color space. * set ``addwd`` to append image dimensions (width x height) filename. * set ``dup`` to True to make copy of image before resizing. :param img_path: local file system path to image :param max_width: set maximum width of image (for resizing) :param max_height: set maximum height of image (for resizing) :param quality: compression quality (0 to 100) :param op: operation to perform (IMG_OP.RESIZE or IMG_OP.CROP) :param addwh: add image dimension (width x height) to file name :param dup: if True, make copy of image before working on it. :returns: path of processed image """ transform = False if op == IMG_OP.RESIZE: crop = False w, h = get_image_size(img_path) if max_width > 0 and w > max_width: ratio = (1.0 * w) / h w = max_width h = int((1.0 * w) / ratio) transform = True elif max_height > 0 and h > max_height: ratio = (1.0 * w) / h h = max_height w = int(ratio * h) transform = True elif op == IMG_OP.CROP: w = max_width h = max_height crop = transform = True if transform: # resize image dst_dir = os.path.dirname(img_path) fbase = get_file_basename(img_path) fext = get_image_ext(img_path, use_path_ext=True) img = resize_image(Image.open(img_path), (w, h,), crop) if img.mode != 'RGB': img = img.convert('RGB') w, h = img.size filename = '%s-%dx%d.%s' % (fbase, w, h, fext) output = os.path.join(dst_dir, filename) img.save(output, quality=quality) if not dup: shutil.move(output, img_path) else: img_path = output elif addwh: # image does not need resizing but we need to append wxh to # file name and optionally (if dup is True) make a copy. dst_dir = os.path.dirname(img_path) fbase = get_file_basename(img_path) fext = get_image_ext(img_path, use_path_ext=True) w, h = get_image_size(img_path) filename = '%s-%dx%d.%s' % (fbase, w, h, fext) output = os.path.join(dst_dir, filename) if dup: shutil.copyfile(img_path, output) else: shutil.move(img_path, output) img_path = output return img_path