django イメージを使用して、カスタム プロセッサを作成しています。サイズを KB (またはバイト) 単位で調べたいのですが、それができません。size 属性は、ファイルのサイズではなく寸法を示します。私は初心者なので、画像に関する詳細情報を取得するために PIL の属性のみを見つけることができましたが、実際にファイルサイズをバイト単位で提供するものはありません。
ModelForm 用にこのプロセッサを作成しました。
これを手伝ってもらえますか?
これまでに書いたコードを追加しています。これはどちらかというとテスト コードです。
import urllib
import os
class CustomCompress(object):
def process(self, image):
print 'image.width',image.width
print 'image.height',image.height
print 'image.size', image.size
print 'image.info', image.info
print 'image.tobytes', image.tobytes
print 'image.category', image.category
print 'image.readonly', image.readonly
print 'image.getpalette', image.getpalette
st = os.stat(image).st_size
print 'get_size ', st
return image
これがforms.pyです
class PhotoForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PhotoForm, self).__init__(*args, **kwargs)
self.fields['old_image'] = ProcessedImageField(spec_id='myapp:test_app:old_image',
processors=[CustomCompress()],
format='JPEG',
# options={'quality': 60}
)
class Meta:
model = Photo
fields = ['old_image']