[英]Django Save Image on different model fields
I have a model called Picture . 我有一个名为Picture的模型。 When an image is uploaded into this model , it will automatically be re-size before saving. 将图像上传到此模型后,它会在保存之前自动重新调整大小。
My main goal is to re-size the uploaded image into 2 separate images . 我的主要目标是将上传的图像重新调整为2个单独的图像。 So I can use it for different purpose like small picture , and big pictures . 所以我可以将它用于不同的目的,如小图片和大图片。 So what I done to achieve this goal is create another field called small . 所以我为实现这个目标所做的就是创建另一个名为small的字段。 which represents small pictures . 代表小图片。
I have 2 functions underneath my model called save and small . 我的模型下面有两个函数叫做save和small。 These functions will automatically re-size the image. 这些功能将自动重新调整图像大小。
My plan is , when I upload an image to the model . 我的计划是,当我将图像上传到模型时。 My save function will automically resize the image and save it into images folder but how can I also get my small function to grab that image from image field so it can resize it and save it into my small field. 我的保存功能将自动调整图像大小并将其保存到图像文件夹中,但我怎样才能使我的小功能从图像字段中抓取该图像,以便它可以调整大小并将其保存到我的小字段中。
To sum is all up , it's just retrieveing an upload image and resizes the image on both field. 要求总结,它只是检索上传图像并调整两个字段上的图像大小。
class Picture(models.Model):
user = models.ForeignKey(User)
small = models.ImageField(upload_to="small/",blank=True,null=True)
image = models.ImageField(upload_to="images/",blank=True)
def save(self , force_insert=False,force_update=False):
super (Picture,self).save(force_insert,force_update)
pw = self.image.width
ph = self.image.height
mw = 500
mh = 500
if (pw > mw) or (ph > mh):
filename = str(self.image.path)
imageObj = img.open(filename)
ratio = 1
if ( pw > mw):
ratio = mw / float(pw)
pw = mw
ph = int(math.floor(float(ph)* ratio))
if ( ph > mh):
ratio = ratio * ( mh /float(ph))
ph = mh
pw = int(math.floor(float(ph)* ratio))
imageObj = imageObj.resize((pw,ph),img.ANTIALIAS)
imageObj.save(filename)
def save(self , force_insert=False,force_update=False):
super (Picture,self).save(force_insert,force_update)
pw = self.image.width
ph = self.image.height
mw = 300
mh = 300
if (pw > mw) or (ph > mh):
filename = str(self.image.path)
imageObj = img.open(filename)
ratio = 1
if ( pw > mw):
ratio = mw / float(pw)
pw = mw
ph = int(math.floor(float(ph)* ratio))
if ( ph > mh):
ratio = ratio * ( mh /float(ph))
ph = mh
pw = int(math.floor(float(ph)* ratio))
imageObj = imageObj.resize((pw,ph),img.ANTIALIAS)
imageObj.save(filename)
If this doesn't make sense , alert me so I can modify it 如果这没有意义,请提醒我,以便我可以修改它
You could create a custom field (inheriting ImageField) or create a pre_save signal to process the upload. 您可以创建自定义字段(继承ImageField)或创建pre_save信号来处理上载。
from django.db.models.signals import pre_save
from django.dispatch import receiver
from myapp.models import MyModel
class MyModel(models.Model):
# other fields
image = MyCustomImageField(sizes=(('small', '300x300'), ('large', '500x500')))
A signal 一个信号
@receiver(pre_save, sender=MyModel)
def process_picture(sender, **kwargs):
# do resizing and storage stuff
More on signals and custom fields . 更多关于信号和自定义字段 。
A working example of a custom ImageField. 自定义ImageField的工作示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.