def __init__(self, img, gaussian_kernel_1d, size=None): """Create an SSIMImage. Args: img: PIL Image object or file name. gaussian_kernel_1d: Gaussian kernel from get_gaussian_kernel. size: New image size to resize image to. """ self.gaussian_kernel_1d = gaussian_kernel_1d self.img = img if isinstance(img, compat.basestring): self.img = compat.Image.open(img) if size and size != self.img.size: self.img = self.img.resize(size) self.size = self.img.size self.img_gray, self.img_alpha = to_grayscale(self.img) if self.img_alpha is not None: self.img_gray[self.img_alpha == 255] = 0 self.img_gray_squared = self.img_gray ** 2 self.img_gray_mu = convolve_gaussian_2d( self.img_gray, self.gaussian_kernel_1d) self.img_gray_mu_squared = self.img_gray_mu ** 2 self.img_gray_sigma_squared = convolve_gaussian_2d( self.img_gray_squared, self.gaussian_kernel_1d) self.img_gray_sigma_squared -= self.img_gray_mu_squared
def __init__(self, img, gaussian_kernel_1d, size=None): """Create an SSIMImage. Args: img: PIL Image object or file name. gaussian_kernel_1d: Gaussian kernel from get_gaussian_kernel. size: New image size to resize image to. """ self.gaussian_kernel_1d = gaussian_kernel_1d self.img = img if isinstance(img, compat.basestring): self.img = compat.Image.open(img) if size and size != self.img.size: self.img = self.img.resize(size) self.size = self.img.size self.img_gray, self.img_alpha = to_grayscale(self.img) if self.img_alpha is not None: self.img_gray[self.img_alpha == 255] = 0 self.img_gray_squared = self.img_gray**2 self.img_gray_mu = convolve_gaussian_2d(self.img_gray, self.gaussian_kernel_1d) self.img_gray_mu_squared = self.img_gray_mu**2 self.img_gray_sigma_squared = convolve_gaussian_2d( self.img_gray_squared, self.gaussian_kernel_1d) self.img_gray_sigma_squared -= self.img_gray_mu_squared
def __init__(self, img, gaussian_kernel_1d=None, size=None): """Create an SSIMImage. Args: img (str or PIL.Image): PIL Image object or file name. gaussian_kernel_1d (np.ndarray, optional): Gaussian kernel that was generated with utils.get_gaussian_kernel is used to precompute common objects for SSIM computation size (tuple, optional): New image size to resize image to. """ # Use existing or create a new PIL.Image try: self.img = img if not isinstance(img, compat.basestring) \ else compat.Image.open(img) except IOError as e: logging.debug("Unable to open %s" % img) raise IOError("Image probably malformed") # Resize image if size is defined and different # from original image if size and size != self.img.size: self.img = self.img.resize(size, Image.ANTIALIAS) # Set the size of the image self.size = self.img.size # If gaussian kernel is defined we create # common SSIM objects if gaussian_kernel_1d is not None: self.gaussian_kernel_1d = gaussian_kernel_1d # np.array of grayscale and alpha image self.img_gray, self.img_alpha = to_grayscale(self.img) if self.img_alpha is not None: self.img_gray[self.img_alpha == 255] = 0 # Squared grayscale self.img_gray_squared = self.img_gray**2 # Convolve grayscale image with gaussian self.img_gray_mu = convolve_gaussian_2d(self.img_gray, self.gaussian_kernel_1d) # Squared mu self.img_gray_mu_squared = self.img_gray_mu**2 # Convolve squared grayscale with gaussian self.img_gray_sigma_squared = convolve_gaussian_2d( self.img_gray_squared, self.gaussian_kernel_1d) # Substract squared mu self.img_gray_sigma_squared -= self.img_gray_mu_squared # If we don't define gaussian kernel, we create # common CW-SSIM objects else: # Grayscale PIL.Image self.img_gray = ImageOps.grayscale(self.img)
def __init__(self, img, gaussian_kernel_1d=None, size=None): """Create an SSIMImage. Args: img (str or PIL.Image): PIL Image object or file name. gaussian_kernel_1d (np.ndarray, optional): Gaussian kernel that was generated with utils.get_gaussian_kernel is used to precompute common objects for SSIM computation size (tuple, optional): New image size to resize image to. """ # Use existing or create a new PIL.Image try: self.img = img if not isinstance(img, compat.basestring) else compat.Image.open(img) except IOError as e: logging.debug("Unable to open %s" % img) raise IOError("Image probably malformed") # Resize image if size is defined and different # from original image if size and size != self.img.size: self.img = self.img.resize(size, Image.ANTIALIAS) # Set the size of the image self.size = self.img.size # If gaussian kernel is defined we create # common SSIM objects if gaussian_kernel_1d is not None: self.gaussian_kernel_1d = gaussian_kernel_1d # np.array of grayscale and alpha image self.img_gray, self.img_alpha = to_grayscale(self.img) if self.img_alpha is not None: self.img_gray[self.img_alpha == 255] = 0 # Squared grayscale self.img_gray_squared = self.img_gray ** 2 # Convolve grayscale image with gaussian self.img_gray_mu = convolve_gaussian_2d(self.img_gray, self.gaussian_kernel_1d) # Squared mu self.img_gray_mu_squared = self.img_gray_mu ** 2 # Convolve squared grayscale with gaussian self.img_gray_sigma_squared = convolve_gaussian_2d(self.img_gray_squared, self.gaussian_kernel_1d) # Substract squared mu self.img_gray_sigma_squared -= self.img_gray_mu_squared # If we don't define gaussian kernel, we create # common CW-SSIM objects else: # Grayscale PIL.Image self.img_gray = ImageOps.grayscale(self.img)