基于django中的最大宽度调整上传图像的大小(PIL)

时间:2022-07-26 00:23:00

I'm working on a simple message board app in Django that lets people upload images to be displayed in their posts. Since the div containing the posts is only 700px, I want to resize the uploaded images to a max width of something like 680px to save space rather than using css to display an image that's too large within the space constraints.

我正在使用Django上的一个简单的消息板应用程序,它可以让人们上传图片显示在他们的帖子里。由于包含post的div只有700px,所以我希望将上传的图像调整为最大宽度,比如680px,以节省空间,而不是使用css来显示在空间约束下太大的图像。

I've been searching this site, google and the PIL docs for a while and haven't found any way of resizing an image on one dimension while maintaining the original aspect ratio. Given my inexperience, it's possible that I'm just not recognizing a solution that's plain to someone who knows what to look for.

我搜索这个站点谷歌和PIL文档已经有一段时间了,还没有找到任何方法在一个维度上调整图像的大小,同时保持原始的纵横比。考虑到我的经验不足,我可能只是没有认识到一个对那些知道该寻找什么的人来说显而易见的解决方案。

2 个解决方案

#1


2  

As an another option, you might like to resize the images using CSS. Thus the images may retain their original resolution (enabling user to save them) and at the same time also fit into divisions nicely.

作为另一个选项,您可能希望使用CSS调整图像的大小。因此,这些图像可能保留它们的原始分辨率(允许用户保存它们),同时也很好地适合于分割。

To prevent oversized images using CSS, you may use:

为避免使用CSS处理过大的图像,你可使用:

img {
max-width:100%;
height:auto;
}

Edit: Sorry Andrew, I don't know about PIL. However, if you know how to resize, and just need to work on maintaining ratio thing, then you can try something like:

对不起,Andrew,我不知道PIL。但是,如果你知道如何调整大小,只需要保持比例,你可以试试以下方法:

def scale_dimensions(width, height, max_width):
    if width > max_width:
        ratio = max_width*1./width
        return (int(width*ratio), int(height*ratio))
    return (width, height)

#2


0  

  • If what you want is to have the maximum dimension, either height or width, be a certain size, Image.thumbnail does precisely this.

    如果你想要最大的尺寸,无论是高度还是宽度,都要有一定的尺寸,图像。缩略图准确地完成这工作。

  • If what you want is for the width to be a particular size, and the height to scale appropriately, you can use (not yet tested!):

    如果你想要的宽度是特定的尺寸,并且高度是适当的比例,你可以使用(还没有测试!)

    # my_image contains the input image; target_width contains the width to resize to.  
    (width, height) = my_image.size   
    target_height = int(height * (1.0 * target_width / width))   
    im.resize((target_width, target_height))  
    

#1


2  

As an another option, you might like to resize the images using CSS. Thus the images may retain their original resolution (enabling user to save them) and at the same time also fit into divisions nicely.

作为另一个选项,您可能希望使用CSS调整图像的大小。因此,这些图像可能保留它们的原始分辨率(允许用户保存它们),同时也很好地适合于分割。

To prevent oversized images using CSS, you may use:

为避免使用CSS处理过大的图像,你可使用:

img {
max-width:100%;
height:auto;
}

Edit: Sorry Andrew, I don't know about PIL. However, if you know how to resize, and just need to work on maintaining ratio thing, then you can try something like:

对不起,Andrew,我不知道PIL。但是,如果你知道如何调整大小,只需要保持比例,你可以试试以下方法:

def scale_dimensions(width, height, max_width):
    if width > max_width:
        ratio = max_width*1./width
        return (int(width*ratio), int(height*ratio))
    return (width, height)

#2


0  

  • If what you want is to have the maximum dimension, either height or width, be a certain size, Image.thumbnail does precisely this.

    如果你想要最大的尺寸,无论是高度还是宽度,都要有一定的尺寸,图像。缩略图准确地完成这工作。

  • If what you want is for the width to be a particular size, and the height to scale appropriately, you can use (not yet tested!):

    如果你想要的宽度是特定的尺寸,并且高度是适当的比例,你可以使用(还没有测试!)

    # my_image contains the input image; target_width contains the width to resize to.  
    (width, height) = my_image.size   
    target_height = int(height * (1.0 * target_width / width))   
    im.resize((target_width, target_height))