Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PIL在Django临时上载文件上失败_Python_Python Imaging Library_Pillow - Fatal编程技术网

Python PIL在Django临时上载文件上失败

Python PIL在Django临时上载文件上失败,python,python-imaging-library,pillow,Python,Python Imaging Library,Pillow,我有一个成像工具,允许摄影师上传文件。上传后,我想检查上传的图像是否确实是一个有效的JPEG文件 因此,我编写了以下验证函数: def validate_JPG(self, image): ''' JPEG validation check. ''' # Since PIL offers a more rubust check to validate if the image # is an JPEG this is used instead of checkin

我有一个成像工具,允许摄影师上传文件。上传后,我想检查上传的图像是否确实是一个有效的JPEG文件

因此,我编写了以下验证函数:

    def validate_JPG(self, image):
    ''' JPEG validation check. '''
    # Since PIL offers a more rubust check to validate if the image
    # is an JPEG this is used instead of checking the file header.
    try:
        img = Image.open(image)
        if img.format != 'JPEG':
            raise JPEGValidationFailed()
        else:
            return True
    except IOError, e:
        self.logger.debug('Error in the JPG validation: {}'.format(e))
        return False
从拍摄图像的上载视图调用该函数:

    uploaded_file = self.request.FILES.get('image_file')

    image_checksum = sha256(uploaded_file.read()).hexdigest()

    if Photos.objects.filter(image_checksum=image_checksum).exists():
        return Response({
            'uploadError': 'Image already exists.'
            }, status=status.HTTP_409_CONFLICT,)
    try:
        self.logger.debug('Parsing: IPTC and EXIF')
        exif, iptc = self.data_parser.process_file(uploaded_file)
    except JPEGValidationFailed, e:
        raise serializers.ValidationError({
            'uploadError': str(e)
            })

    except Exception, e:
        self.logger.error('Error in parsing the IPTC and EXIF data: {}'.format(e))
        raise serializers.ValidationError({
            'uploadError': 'Something has gone wrong.'
            })
这段代码一直运行得很愉快,但不知怎么的,它现在失败了。。使用了Pillow库,
Pillow==3.0.0
,但更新到最新版本也不起作用

遇到以下错误:

无法识别图像文件

同时执行
image.seek(0)
也不起作用


有人能帮我吗?

好的。。。因此,在休息一下并再次查看代码后,我注意到一个文件在被传递到视图(备份保存)之前使用相同的上传文件写入:

因此,该文件已被读取一次。在将其传递给视图之前,我必须放置一个
图像文件.seek(0)
。。。这就是解决办法。希望它最终能帮助别人

  file.write(image_file.read())