Example #1
0
def clear_metadata(fileobj: FileStorage, mime_type: str):
    resultIO = FileStorage()
    fileobj.seek(0)
    if mime_type in ("image/jpeg", "image/png"):
        image = Image.open(fileobj)
        if not image.info.get("exif"):
            return fileobj
        exifdata = Exif()
        exifdata.load(image.info["exif"])
        # XXX: We want to remove all EXIF data except orientation (tag 274) or people will start seeing
        # rotated images...
        # Also, Pillow can't encode EXIF data, so we have to do it manually
        if exifdata.endian == "<":
            head = b"II\x2A\x00\x08\x00\x00\x00"
        else:
            head = b"MM\x00\x2A\x00\x00\x00\x08"
        ifd = TiffImagePlugin.ImageFileDirectory_v2(ifh=head)
        for tag, value in exifdata.items():
            if tag == 274:
                ifd[tag] = value
        newExif = b"Exif\x00\x00" + head + ifd.tobytes(8)
        image.save(resultIO,
                   format=Image.EXTENSION[EXTENSIONS[mime_type]],
                   exif=newExif)
        return resultIO
    elif mime_type == "video/mp4":
        video = MP4(fileobj)
        video.clear()
        video.save(resultIO)
        return resultIO
    elif mime_type == "video/webm":
        # XXX: Mutagen doesn't seem to support webm files
        return fileobj
Example #2
0
def clear_metadata(path: str, mime_type: str):
    if mime_type in ('image/jpeg', 'image/png'):
        image = Image.open(path)
        if not image.info.get('exif'):
            return image.save(path)
        exifdata = Exif()
        exifdata.load(image.info['exif'])
        # XXX: We want to remove all EXIF data except orientation (tag 274) or people will start seeing
        # rotated images...
        # Also, Pillow can't encode EXIF data, so we have to do it manually
        if exifdata.endian == "<":
            head = b"II\x2A\x00\x08\x00\x00\x00"
        else:
            head = b"MM\x00\x2A\x00\x00\x00\x08"
        ifd = TiffImagePlugin.ImageFileDirectory_v2(ifh=head)
        for tag, value in exifdata.items():
            if tag == 274:
                ifd[tag] = value
        newExif = b"Exif\x00\x00" + head + ifd.tobytes(8)
        image.save(path, exif=newExif)
    elif mime_type == 'video/mp4':
        video = MP4(path)
        video.clear()
        video.save()
    elif mime_type == 'video/webm':
        # XXX: Mutagen doesn't seem to support webm files
        pass