1

我有一个名为 Picture 的模型,它有 2 个图像场。一个叫做 Image ,一个叫做 small 。我有 2 个图像字段的主要原因是,我模型下的函数将调整图像大小并根据它们的位置保存。

我的主要目标是最小化不需要的代码,因为我有 2 个重复的函数,它们执行相同的操作但针对不同的字段。我们可以创建一个执行相同操作但针对不同字段的函数,而不是拥有 2 个函数。

class Picture(models.Model):

    small = models.ImageField(upload_to="small/",blank=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 small(self , force_insert=False,force_update=False):
        super (Picture,self).save(force_insert,force_update)

        pw = self.small.width
        ph = self.small.height
        mw = 1500
        mh = 1500

        if (pw > mw) or (ph > mh):
            filename = str(self.small.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)

我试图做的是创建一个名为 Resizier 的类,其参数为 field 。因此,如果我在小字段中上传图像,我的模型将使用 If 语句确定是否小。转换为图像变量并调用 Resizier 类。

问题是我明白了

   error dict expect at most 1 argument , got 3

我的模型.py

from pet.fields import Resizier

class Picture(models.Model):

    small = models.ImageField(upload_to="small/",blank=True)
    image = models.ImageField(upload_to="images/",blank=True)

    if image:
        Resizier(image)
    else:
        small = image
        Resizier(image)

我的领域.py

from PIL import Image as img
import math

image = {}
class Resizier(image):


    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 = 400
        mh = 400

        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)

错误

    from pet.fields import Resizier
  File "C:\o\17\mysite\pet\fields.py", line 5, in <module>
    class Resizier(image):
TypeError: Error when calling the metaclass bases
    dict expected at most 1 arguments, got 3
4

1 回答 1

1

你的问题在这里:

from PIL import Image as img
import math

image = {}
class Resizier(image): # you are inheriting from `image`, which is a dict,
                       # you probably want `Picture` here.

但是,您还有其他一些问题。我是否可以建议不要尝试重新发明轮子,而是使用已经可用的东西。以下是提供此功能的软件包列表。

于 2013-04-21T05:24:21.520 回答