示例#1
0
	def rescale(self, cropped_image, size, force_crop=False):
		""" Resizes and saves the image to other sizes of the same aspect ratio from a given cropped image """
		
		if force_crop or not size.create_on_request:
			auto_crop = (size.auto_size == AUTO_CROP)
			thumbnail = utils.rescale(cropped_image, size.width, size.height, auto_crop=auto_crop)
		
			# In case the thumbnail path hasn't been created yet
			if not os.path.exists(self.folder_path):
				try:
					os.makedirs(self.folder_path)
				except OSError:
					# Handles weird race conditions if the path wasn't created just yet
					if os.path.exists(self.folder_path):
						pass
					else: 
						os.makedirs(self.folder_path)
					
			thumbnail.save(self.thumbnail_path(size.slug), **IMAGE_SAVE_PARAMS)
			
			# Create retina image
			if size.retina:
				retina_size = size.retina_size
				
				# Only create retina image if cropped image is large enough
				# If retina size is required, make a separate size
				if retina_size.width <= cropped_image.size[0] and retina_size.height <= cropped_image.size[1]:
					retina_thumbnail = utils.rescale(cropped_image, retina_size.width, retina_size.height, crop=retina_size.auto_size)
					retina_thumbnail.save(self.thumbnail_path(retina_size.slug), **IMAGE_SAVE_PARAMS)
    def resize_image(self, image, sizes, force):
        """
        Resizes an image to the provided set sizes.

        @param image: Opened original image
        @type  image: PIL.Image
        
        @param sizes: Set of sizes to create.
        @type  sizes: [Size1, ...]
        
        @param force: Whether or not to recreate a thumbnail if it already exists.
        @type  force: bool

        @return: 
        @rtype: 
        """
        img_params = (self.IMG_TYPE_PARAMS.get(image.format) or {}).copy()
        for size in sizes:
            
            logging.debug('Converting image to size `%s` (%s x %s)' % (size.name,
                                                                       size.width,
                                                                       size.height))
            # Do we need to recreate the file?
            if not force and os.path.isfile(size.path) and os.stat(size.path).st_size > 0:
                logging.debug(' - Image `%s` exists, skipping...' % size.name)
                continue

            folder, _basename = os.path.split(size.path)
            if not os.path.isdir(folder):
                logging.debug(' - Directory %s does not exist.  Creating...' % folder)
                os.makedirs(folder)

            try:
                # In place scaling, so we need to use a copy of the image.
                thumbnail = rescale(image.copy(),
                                    size.width,
                                    size.height,
                                    crop=size.crop)

                tmp_path = size.path + '.tmp'

                thumbnail.save(tmp_path, image.format, **img_params)

            # No idea what this can throw, so catch them all
            except Exception, e:
                logging.exception('Error saving thumbnail to %s' % tmp_path)
                raise SystemExit('Exiting...')
                
            else:
                os.rename(tmp_path, size.path)
示例#3
0
    def resize_image(self, image, sizes, force):
        """
        Resizes an image to the provided set sizes.

        @param image: Opened original image
        @type  image: PIL.Image
        
        @param sizes: Set of sizes to create.
        @type  sizes: [Size1, ...]
        
        @param force: Whether or not to recreate a thumbnail if it already exists.
        @type  force: bool

        @return: 
        @rtype: 
        """
        img_params = (self.IMG_TYPE_PARAMS.get(image.format) or {}).copy()
        for size in sizes:
            
            logging.debug('Converting image to size `%s` (%s x %s)' % (size.name,
                                                                       size.width,
                                                                       size.height))
            # Do we need to recreate the file?
            if not force and os.path.isfile(size.path) and os.stat(size.path).st_size > 0:
                logging.debug(' - Image `%s` exists, skipping...' % size.name)
                continue

            folder, _basename = os.path.split(size.path)
            if not os.path.isdir(folder):
                logging.debug(' - Directory %s does not exist.  Creating...' % folder)
                os.makedirs(folder)

            try:
                # In place scaling, so we need to use a copy of the image.
                thumbnail = rescale(image.copy(),
                                    size.width,
                                    size.height,
                                    crop=size.crop)

                tmp_path = size.path + '.tmp'

                thumbnail.save(tmp_path, image.format, **img_params)

            # No idea what this can throw, so catch them all
            except Exception, e:
                logging.exception('Error saving thumbnail to %s' % tmp_path)
                raise SystemExit('Exiting...')
                
            else:
                os.rename(tmp_path, size.path)
示例#4
0
	def save(self, *args, **kwargs):

		super(Image, self).save(*args, **kwargs)

		for size in self.size_set.size_set.all().filter(auto_size=1):
			if self.image.width > size.width and self.image.height > size.height:
				thumbnail = utils.rescale(pil.open(self.image.path), size.width, size.height, crop=True)
				if not os.path.exists(self.folder_path):
					os.makedirs(self.folder_path)
						
				thumbnail.save(self.thumbnail_path(size), **IMAGE_SAVE_PARAMS)
			else:
				thumbnail = pil.open(self.image.path)
				if not os.path.exists(self.folder_path):
					os.makedirs(self.folder_path)
				thumbnail.save(self.thumbnail_path(size), **IMAGE_SAVE_PARAMS)
示例#5
0
	def save(self, *args, **kwargs):
		super(Crop, self).save(*args, **kwargs)

		if self.size:
			sizes = Size.objects.all().filter(aspect_ratio=self.size.aspect_ratio, size_set=self.size.size_set).exclude(auto_size=1).order_by("-width")
			if sizes:
				cropped_image = utils.create_cropped_image(self.image.image.path, self.crop_x, self.crop_y, self.crop_w, self.crop_h)
					
				for size in sizes:
					
					thumbnail = utils.rescale(cropped_image, size.width, size.height, crop=size.auto_size)
	
					if not os.path.exists(self.image.folder_path):
						os.makedirs(self.image.folder_path)
					
					thumbnail.save(self.image.thumbnail_path(size), **IMAGE_SAVE_PARAMS)