找到图像分辨率的最佳方式 [英] Best way to find image resolution

查看:242
本文介绍了找到图像分辨率的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的图像模型。

  class ImageUpload(models.Model):
image = models.ImageField(upload_to ='images /%Y / %m /%d')
title = models.CharField(max_length = 20)

但是,当我创建模型时,我没有制作高度和宽度的字段。有没有反正我在视图中找到图像的高度和宽度,而不添加任何模型?如果没有,我可以如何将高度和宽度字段添加到模型中,并从已经存储在数据库中的图像中自动添加?

解决方案

或者你可以使用django get_image_dimensions

 从django.core.files.images导入get_image_dimensions 

w,h = get_image_dimensions(image)#返回图像的(宽度,高度),给定一个打开的文件或路径


I have a standard image model.

class ImageUpload(models.Model):
    image = models.ImageField(upload_to='images/%Y/%m/%d')
    title = models.CharField(max_length=20)    

However, when I created the model, I did not make fields for height and width. Is there anyway for me to find the height and width of the image in the view without adding anything to the model? If not, how could I go about adding the height and width fields to the model and have it auto add from the images already stored in the database?

解决方案

Or you could use django get_image_dimensions:

from django.core.files.images import get_image_dimensions

w, h = get_image_dimensions(image) # Returns the (width, height) of an image, given an open file or a path

这篇关于找到图像分辨率的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆