def test_validate_image_extension_good(self):
     with open(self.image_path, 'rb') as byte_file, PIL.Image.open(self.image_path) as image_file:
         file_memory = InMemoryUploadedFile(
             byte_file, image_file, self.name_image,
             'image', 1000, ''
         )
         file_memory.image = image_file
         try:
             validators.validate_image_extension(file_memory)
         except Exception:
             self.fail(
                 'validate_image_extension() raised Exception unexpectedly!'
             )
Example #2
0
File: models.py Project: g10f/sso
def transpose_image(picture):
    # copied from ImageOps.exif_transpose but avoiding to create a copy if not
    # transposed
    # argument is a UploadedFile Object instead of Image
    # exif is only in TIFF and JPEG available
    if picture.image.format not in ['JPEG', 'TIFF']:
        return picture

    exif = picture.image.getexif()
    orientation = exif.get(0x0112)
    method = {
        2: Image.FLIP_LEFT_RIGHT,
        3: Image.ROTATE_180,
        4: Image.FLIP_TOP_BOTTOM,
        5: Image.TRANSPOSE,
        6: Image.ROTATE_270,
        7: Image.TRANSVERSE,
        8: Image.ROTATE_90,
    }.get(orientation)
    if method is not None:
        image = Image.open(picture.file)
        transposed_image = image.transpose(method)
        del exif[0x0112]
        transposed_image.info["exif"] = exif.tobytes()
        # create a new UploadedFile
        f = BytesIO()
        transposed_image.save(f, image.format)
        picture = InMemoryUploadedFile(
            file=f,
            field_name=picture.field_name,
            name=picture.name,
            content_type=picture.content_type,
            size=f.tell(),
            charset=picture.charset,
            content_type_extra=picture.content_type_extra)
        picture.image = transposed_image
        return picture

    return picture