Django ImageKit和PIL [英] Django ImageKit and PIL

查看:132
本文介绍了Django ImageKit和PIL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django映像并创建一个自定义处理器。我想找出大小(KB)(或字节),但不能这样做。尺寸属性给出了尺寸,而不是文件的大小。我是一个新手,所以只能找到PIL的attr来获取更多关于图像的信息,但没有一个实际上给出了以字节为单位的文件大小。



我可以为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屋!

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