Django的图像字段
django-imagefield的Python项目详细描述
图像处理器
Django ImageField使用的图像处理管道 Django的中间件。
以下处理器是现成的:
- 自动旋转 :通过读取exif数据自动旋转图像。
- 处理JPEG:将非RGB图像转换为RGB,激活 渐进式编码,并将质量设置为90的更高值。
- 处理gif :在调整大小时保留透明度和调色板数据 图像:
- 缩略图 :调整图像大小,使其不超过边框。
- 裁剪 :将图像裁剪到给定的维度,同时获取ppoi (主要关注点)如果提供,请考虑信息。
- 默认值 :自动旋转 , 处理JPEG 的组合, 处理gif 和 保留icc配置文件 。附加违约 未来可能会增加处理器。建议使用 默认值 而不是逐个添加处理器。
处理器可以单独使用其名称指定,或者 获取参数,使用元组,其中第一个条目是处理器的 name和其余的是位置参数。
您可以轻松注册自己的处理器,甚至可以重写内置的 处理器(如果需要):
fromimagefield.processingimportregister# You could also write a class with a __call__ method, but I really# like the simplicity of functions.@registerdefmy_processor(get_image,...):defprocessor(image,context):# read some information from the image...# or maybe modify it, but it's mostly recommended to modify# the image after calling get_imageimage=get_image(image,context)# modify the image, and return it...modified_image=...# maybe modify the context...returnmodified_imagereturnprocessor
处理器的名称直接取自注册对象。
将图像转换为灰度的示例处理器如下所示 以下:
fromPILimportImageOpsfromimagefield.processingimportregister@registerdefgrayscale(get_image):defprocessor(image,context):image=get_image(image,context)returnImageOps.grayscale(image)returnprocessor
现在将 "灰度" 包含在图像的处理规范中,其中 您想使用它。