Django ImageKit和PIL [英] Django ImageKit and 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
打印'image.readonly',image.readonly
打印'image.getpalette',image.getpalette
st = os.stat(image).st_size
print'get_size' ,st
返回图像
这是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 =照片
fields = ['old_image']
在文件的实际路径上使用os.stat来获取字节大小,然后除以1024获得KB:
import os
filesize = os.stat('/ path / to / somefile.jpg')。st_size
print filesize / float(1024)
I am using django image and creating a custom processor. I want to find out the size in KB (or bytes) but unable to do so. The size attribute give the dimensions and not the size of the file. I am a newbie so have only been able to find attr of PIL to get more information about the image but none of the them actually give the file size in bytes.
I have creating this processor for a ModelForm.
Can you please help with this?
I am adding the code written so far. It is more of a test code;
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
Here is the 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']
use os.stat on the actual path of the file to get the size in bytes, then divide by 1024 to get KB:
import os
filesize = os.stat('/path/to/somefile.jpg').st_size
print filesize/float(1024)
这篇关于Django ImageKit和PIL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!