Ejemplo n.º 1
0
def generate_image(image, size):
    size = int(size)
    path = os.path.join(settings.MEDIA_ROOT, image)
    name = os.path.basename(image)
    mime = guess_type(path)
    if mime.startswith('image/'):
        img = Image.open(path)
    elif mime.startswith('video/'):
        thumbdir = os.path.join(
            settings.MEDIA_ROOT, 'thumb', os.path.dirname(image)
        )
        try:
            os.makedirs(thumbdir)
        except OSError:
            pass
        thumbpath = os.path.join(thumbdir, name) + ".jpg"
        if not os.path.exists(thumbpath):
            subprocess.call(['avconv', '-i', path, '-vframes', '1', thumbpath])
        img = Image.open(thumbpath)
    else:
        return None

    if hasattr(img, '_getexif'):
        exif = img._getexif()
    else:
        exif = None
    if exif:
        orientation = exif.get(0x0112, 1)
    else:
        orientation = 1

    rotate = {
        3: Image.ROTATE_180,
        6: Image.ROTATE_270,
        8: Image.ROTATE_90
    }
    if orientation in rotate:
        img = img.transpose(rotate[orientation])

    if size < 256:
        img = ImageOps.fit(img, (size, size), Image.ANTIALIAS)
    elif size < 600:
        img.thumbnail((size, size), Image.ANTIALIAS)
    else:
        width, height = img.size
        if width > size:
            ratio = float(size) / float(width)
            height = int(height * ratio)
            img = img.resize((size, height), Image.ANTIALIAS)

    tdir = os.path.join(settings.MEDIA_ROOT, str(size), os.path.dirname(image))
    try:
        os.makedirs(tdir)
    except OSError:
        pass
    img.save(os.path.join(tdir, name), 'JPEG')
    data = BytesIO()
    img.save(data, 'JPEG')
    data.seek(0)
    return data
Ejemplo n.º 2
0
    def mimetype(self):
        if self.type is not None:
            return self.type.mimetype

        if self.file.name not in (None, ""):
            mimetype = guess_type(self.file.name, self.file.read(1024))
            return mimetype
        else:
            return None
Ejemplo n.º 3
0
    def mimetype(self):
        if self.type is not None:
            return self.type.mimetype

        if self.file.name not in (None, ""):
            mimetype = guess_type(self.file.name, self.file.read(1024))
            return mimetype
        else:
            return None
Ejemplo n.º 4
0
    def mimetype(self):
        if self.type is not None:
            return self.type.mimetype

        if self.file.name not in (None, ""):
            from wq.io.util import guess_type
            mimetype = guess_type(self.file.name, self.file.read(1024))
            return mimetype
        else:
            return None
Ejemplo n.º 5
0
    def mimetype(self):
        if self.type is not None:
            return self.type.mimetype

        if self.file.name not in (None, ""):
            from wq.io.util import guess_type
            mimetype = guess_type(self.file.name, self.file.read(1024))
            return mimetype
        else:
            return None
Ejemplo n.º 6
0
def generate_image(image, size):
    size = int(size)
    path = os.path.join(settings.MEDIA_ROOT, image)
    name = os.path.basename(image)
    mime = guess_type(path)
    if mime.startswith('image/'):
        img = Image.open(path)
    elif mime.startswith('video/'):
        thumbdir = os.path.join(settings.MEDIA_ROOT, 'thumb',
                                os.path.dirname(image))
        try:
            os.makedirs(thumbdir)
        except OSError:
            pass
        thumbpath = os.path.join(thumbdir, name) + ".jpg"
        if not os.path.exists(thumbpath):
            subprocess.call(['avconv', '-i', path, '-vframes', '1', thumbpath])
        img = Image.open(thumbpath)
    else:
        return None

    if hasattr(img, '_getexif'):
        exif = img._getexif()
    else:
        exif = None
    if exif:
        orientation = exif.get(0x0112, 1)
    else:
        orientation = 1

    rotate = {3: Image.ROTATE_180, 6: Image.ROTATE_270, 8: Image.ROTATE_90}
    if orientation in rotate:
        img = img.transpose(rotate[orientation])

    if size < 256:
        img = ImageOps.fit(img, (size, size), Image.ANTIALIAS)
    elif size < 600:
        img.thumbnail((size, size), Image.ANTIALIAS)
    else:
        width, height = img.size
        if width > size:
            ratio = float(size) / float(width)
            height = int(height * ratio)
            img = img.resize((size, height), Image.ANTIALIAS)

    tdir = os.path.join(settings.MEDIA_ROOT, str(size), os.path.dirname(image))
    try:
        os.makedirs(tdir)
    except OSError:
        pass
    img.save(os.path.join(tdir, name), 'JPEG')
    data = BytesIO()
    img.save(data, 'JPEG')
    data.seek(0)
    return data
Ejemplo n.º 7
0
def export_image(source, dest, size):
    size = int(size)
    mime = guess_type(source)
    if not mime.startswith('image/'):
        raise Exception("Unknown file type %s for %s!" % (mime, source))

    img = Image.open(source)
    if hasattr(img, '_getexif'):
        exif = img._getexif()
    else:
        exif = None
    if exif:
        orientation = exif.get(0x0112, 1)
    else:
        orientation = 1

    rotate = {3: Image.ROTATE_180, 6: Image.ROTATE_270, 8: Image.ROTATE_90}
    if orientation in rotate:
        img = img.transpose(rotate[orientation])

    img = ImageOps.fit(img, (size, size), Image.ANTIALIAS)
    img.save(dest, 'JPEG')
Ejemplo n.º 8
0
 def mimetype(self):
     if self.file.name not in (None, ""):
         self.file.open()
         return guess_type(self.file.name, self.file.read(1024))
     else:
         return None