Python 2 / Pillow - issue calculating resized image height & width

时间:2022-12-24 00:23:57

I'm building a blog-style website using Django that allows people to upload images for their posts. I wrote some code to resize user uploaded images. It takes the uploaded image and saves two copies, 1 scaled down large image and 1 thumbnail.

我正在使用Django构建一个博客风格的网站,允许人们为他们的帖子上传图像。我写了一些代码来调整用户上传的图像。它需要上传的图像并保存两个副本,1个按比例缩小的大图像和1个缩略图。

The code works perfectly on my personal computer I'm using for development (Python 3, Django 1.8), but it doesn't work in production on my server (Python 2, Django 1.8). On the server, it appears the image resize math is giving a value of 0 for either the width or height. I've tried various methods of rounding, etc but nothing seems to fix the issue.

代码在我用于开发的个人计算机上完美运行(Python 3,Django 1.8),但它在我的服务器(Python 2,Django 1.8)上的生产中不起作用。在服务器上,显示图像调整大小数学为宽度或高度赋予值0。我尝试了各种舍入等方法,但似乎没有解决问题。

Here's the section of my views.py that handles the image:

这是我处理图像的views.py部分:

    (card_image_resize, card_image_thumb_resize, card_image_orientation) = image_resize(card_image.userimg.path)
    (w, h) = card_image_resize.size
    card_image_resize.save(card_image.userimg.path)
    card_image_thumb_resize.save(card_image.userimg_thumb.path)
    card_image.orientation = card_image_orientation
    card_image.save()

And here's the image resize code:

这是图像大小调整代码:

def image_resize(path):
    image = Image.open(path)

    (w, h) = image.size
    if w > h:
        orientation = 'l'
    elif w < h:
        orientation = 'p'
    else:
        orientation = 's'

#calculate new large image dimensions
    if w >= 1000 or h >= 1000:
        if w > h:
            w_new = 1000
            h_new = (h/w) * 1000
        elif h > w:
            h_new = 1000
            w_new = (w/h) * 1000
        elif h == w:
            h_new = 1000
            w_new = 1000
    else:
        if w > h:
            w_new = 400
            h_new = (h/w) * 400
        elif h > w:
            h_new = 400
            w_new = (w/h) * 400
        elif h == w:
            h_new = 400
            w_new = 400

#calculate thumbnail dimensions
    if w >= 1000 or h >= 1000:
        if w > h:
            wthumb_new = 400
            hthumb_new = (h/w) * 400
        elif h > w:
            hthumb_new = 400
            wthumb_new = (w/h) * 400
        elif h == w:
            hthumb_new = 400
            wthumb_new = 400

    w_new = int(w_new)
    h_new = int(h_new)
    try:
        wthumb_new = int(wthumb_new)
        hthumb_new = int(hthumb_new)
        image_thumb = image.resize((wthumb_new, hthumb_new), Image.ANTIALIAS)
        image = image.resize((w_new, h_new), Image.ANTIALIAS)
    except:
        image_thumb = image.resize((w, h), Image.ANTIALIAS)

    image = image.resize((w_new, h_new), Image.ANTIALIAS)

    return image, image_thumb, orientation

The part that's causing the issue (I assume) is the section the calculates the ratio calculation for height or width: w_new = (w/h) * 1000. When I run this in development, I'm getting the error Exception Value: tile cannot extend outside image. Looking at the image size values, it's clear that the w_new / h_new calculation is returning zero:

引起问题的部分(我假设)是计算高度或宽度的比率计算的部分:w_new =(w / h)* 1000.当我在开发中运行它时,我得到错误异常值:tile不能延伸到外面的图像。查看图像大小值,很明显w_new / h_new计算返回零:

card_image_resize: PIL.Image.Image image mode=RGB size=1000x0 at 0x7FADB1A0E320

card_image_resize:PIL.Image.Image图像模式= RGB尺寸= 1000x0 at 0x7FADB1A0E320

The error occurs upon saving

保存时会发生错误

card_image.save()

My question is why and how do I get around this? It seems like a pretty straightforward formula to maintain the image size ratio. Even stranger that it works with Python 3 but not Python 2.

我的问题是为什么以及如何解决这个问题?这似乎是一个非常简单的公式来保持图像大小比例。更奇怪的是,它适用于Python 3但不适用于Python 2。

I'm admittedly not an expert at this, so I'm open to a more efficient way of resizing the image. However, I'd still be interested to learn why that formula returns a value of zero.

我当然不是这方面的专家,所以我愿意采用更有效的方法来调整图像大小。但是,我仍然有兴趣了解为什么该公式返回零值。

1 个解决方案

#1


1  

In Python 3, / uses floating-point arithmetic when necessary. In Python 2, you have to specify floats. Change:

在Python 3中,/在必要时使用浮点运算。在Python 2中,您必须指定浮点数。更改:

(w/h) * 400

to

(float(w)/h) * 400

and similar, wherever necessary.

和类似的,在必要的地方。

#1


1  

In Python 3, / uses floating-point arithmetic when necessary. In Python 2, you have to specify floats. Change:

在Python 3中,/在必要时使用浮点运算。在Python 2中,您必须指定浮点数。更改:

(w/h) * 400

to

(float(w)/h) * 400

and similar, wherever necessary.

和类似的,在必要的地方。