Python Image.open()给出的对象没有属性'错误

时间:2016-11-29 21:06:55

标签: python amazon-web-services image-processing amazon-s3

我正在尝试使用requests.get()来打开/获取用于图像处理的文件,但是当我尝试使用我刚刚获得的文件Image.open()时,我的代码就会崩溃。

我的代码是:

conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
bucket = conn.get_bucket(settings.AWS_STORAGE_BUCKET_NAME)

for key in bucket.list(prefix='media/userphotos'):
    file_name=key.name
    full_path_filename = 'https://' + settings.AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/' + file_name
    fd_img = requests.get(full_path_filename);
    img = Image.open(fd_img)
    img = resizeimage.resize_width(img, 800)
    new_filename = file_name
    new_filename.replace('userphotos', 'webversion')
    full_path_filename =  'https://' + settings.AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/' + new_filename
    img.save(full_path_filename, img.format)
    fd_img.close()



The traceback is: 

Traceback:

File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/PIL/Image.py" in open
  2275.         fp.seek(0)

During handling of the above exception ('Response' object has no attribute 'seek'), another exception occurred:

File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/billnoble/Documents/YHistory-Server/items/views.py" in xformpics
  67.         img = Image.open(fd_img)

File "/Users/billnoble/Documents/YHistory-Server/yhistoryvenv/lib/python3.4/site-packages/PIL/Image.py" in open
  2277.         fp = io.BytesIO(fp.read())

Exception Type: AttributeError at /xformpics
Exception Value: 'Response' object has no attribute 'read'




一切正常,直至Image.open()

我花了很多时间在谷歌搜索这个问题,但无法找到解决方案。

2 个答案:

答案 0 :(得分:1)

经过大量的谷歌搜索和实验后,我找到了一个很好的解决方案。以下代码还可确保正确旋转图像。



def xformpics(self):
    conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
    bucket = conn.get_bucket(settings.AWS_STORAGE_BUCKET_NAME)

    for key in bucket.list(prefix='media/userphotos'):

        # Get the image to be resized from the S3 bucket
        file_name=key.name
        full_path_filename = 'https://' + settings.AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/' + file_name
        fd_img = urlopen(full_path_filename);
        img = Image.open(fd_img)

        # Check EXIF data to see if image needs to be rotated
        img = image_transpose_exif(img)

        # Resize the image to a width of 800 (for viewing on a web page)
        img = resizeimage.resize_width(img, 800)

        # Upload the resized image to the S3 bucket
        new_filename = full_path_filename.replace('userphotos', 'webversion')
        img.save('temp.jpg', img.format)
        with open('temp.jpg', 'rb') as data:
            r = requests.put(new_filename, data, auth=S3Auth(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY))

        fd_img.close()

def image_transpose_exif(im):
    exif_orientation_tag = 0x0112  # contains an integer, 1 through 8
    exif_transpose_sequences = [  # corresponding to the following
        [],
        [Image.FLIP_LEFT_RIGHT],
        [Image.ROTATE_180],
        [Image.FLIP_TOP_BOTTOM],
        [Image.FLIP_LEFT_RIGHT, Image.ROTATE_90],
        [Image.ROTATE_270],
        [Image.FLIP_TOP_BOTTOM, Image.ROTATE_90],
        [Image.ROTATE_90],
    ]

    try:
        seq = exif_transpose_sequences[im._getexif()[exif_orientation_tag] - 1]
    except Exception:
        return im
    else:
        return functools.reduce(lambda im, op: im.transpose(op), seq, im)




答案 1 :(得分:0)

Image.open()需要一个文件(或类文件)对象,requests.get()的结果不是。

您可能希望将图像内容压入缓冲区:

image_data = requests.get(full_path_filename).text

然后使用Image.fromstring()Image.frombuffer()来使用缓冲区而不是文件来创建图像。