Exemple #1
0
    def copy_scaled(self, src, uri):
        """
        Rotate and scale image.
        Copy the processed image to scaled dest directory
        - src can be either a string or a file-like object
        """
        dest = os.path.join(self.abs_scaled_dest_dir, uri)
        if os.path.exists(dest):
            # dest already exists: failed silently
            return False

        loc = seek(src, 0)
        im = Image.open(src)
        try:
            orientation = get_exif(im._getexif())['Orientation']
        except:
            orientation = 0

        dirpath = os.path.dirname(dest)
        if not os.path.exists(dirpath):
            os.makedirs(dirpath, 0755)

        # auto rotate if needed
        if orientation == 6:
            im=im.rotate(270)
        if orientation == 8:
            im=im.rotate(90)

        width_src, height_src = im.size
        if width_src <= self.dimension and height_src <= self.dimension:
            log.info("Scale won't be processed: photo is to small.")
        else:
            if width_src > height_src:
                height_dest = self.dimension * height_src / width_src
                width_dest = self.dimension
            else:
                width_dest = self.dimension * width_src / height_src
                height_dest = self.dimension

            # Si on redimmensionne selon une taille paire, on force la
            # largeur et hauteur finales de l'image a etre egalement
            # paires.
            if self.dimension % 2 == 0:
                height_dest = height_dest - height_dest % 2
                width_dest = width_dest - width_dest % 2

            im=im.resize((width_dest, height_dest), Image.ANTIALIAS)
            log.info("Processed: %s" % dest)

        # save processed image
        im.save(dest, quality=self.quality)
        if loc is not None: seek(src, loc)
Exemple #2
0
def get_info(img, info=None):
    """
    Get infos about the given image
    """
    if info is None: info = {}
    loc = seek(img, 0)
    im = Image.open(img)
    if 'date' not in info:
        try:
            exif = get_exif(im._getexif())
            info['date'] = datetime.strptime(
                    exif['DateTimeOriginal'], '%Y:%m:%d %H:%M:%S')
        except:
            info['date'] = None
    if 'md5sum' not in info:
        info['md5sum'] = img_md5(im)
    if 'ext' not in info:
        info['ext'] = im.format.lower()
    if 'size' not in info:
        info['size'] = get_size(img)
    if loc is not None: seek(img, loc)

    return info
Exemple #3
0
    def copy_orig(self, src, uri):
        """
        Copy the original image to orig dest directory
        - src can be either a string or a file-like object
        """
        dest = os.path.join(self.abs_orig_dest_dir, uri)
        if os.path.exists(dest):
            # dest already exists: failed silently
            return False

        dirpath = os.path.dirname(dest)
        if not os.path.exists(dirpath):
            os.makedirs(dirpath, 0755)
        if isinstance(src, (StringType, UnicodeType)):
            shutil.copyfile(src, dest)
        else:
            with open(dest, 'wb') as f:
                loc = seek(src, 0)
                shutil.copyfileobj(src, f)
                src.seek(loc)
        log.info("Copied: %s" % dest)