内容正在载入中,请稍后……
公告
搜索
统计
其他
3 Nov.2011

django-imagekit ImageKit 1.0.1中文文档翻译 夜晚

作者: 我就是个世界   分类:技术&学习 » Django   出处:本站原创            | |
django-imagekit介绍
Automates image processing for Django models. Resize, process and cache multiple versions of your image files. Access newly created files with a standard API. Supports alternate storage schemes such as Amazon S3.
自动化图像处理为Django模型。为你的图像文件调整大小,处理和缓存为多个版本。使用一个标准的API访问新创建的文件。支持云存储方案,如Amazon S3。

准备用这个东西,国内没搜索到相关的文档,所以自己翻译了一份。E文水平有限,有不到之处,请见凉!
-----------------------------
原英文文档 ImageKit 1.0.1 documentation

Getting Started  快速入门

ImageKit is a Django app that helps you to add variations of uploaded images to your models. These variations are called “specs” and can include things like different sizes (e.g. thumbnails) and black and white versions.
ImageKit是一个Django应用程序,可以帮助你添加上传图像变化到你的模型。这些变形称为“specs”,可以包括诸如不同尺寸(例如缩略图)和黑白的版本。

Installation 安装

    1. pip install django-imagekit (or clone the source and put the imagekit module on your path)
    2. Add 'imagekit' to your INSTALLED_APPS list in your project’s settings.py

Adding Specs to a Model 添加配置Specs 到模型中

Much like django.db.models.ImageField, Specs are defined as properties of a model class:
就像 django.db.models.ImageField一样, Specs 被定义为一个模型类的属性:


from django.db import models
from imagekit.models import ImageSpec

class Photo(models.Model):
    original_image = models.ImageField(upload_to='photos')
    formatted_image = ImageSpec(image_field='original_image', format='JPEG',
            quality=90)


Accessing the spec through a model instance will create the image and return an ImageFile-like object (just like with a normal django.db.models.ImageField):
通过一个模型实例访问这个spec ,将创建一个图像并返回一个ImageFile对象(就像使用默认的django.db.models.ImageField):


photo = Photo.objects.all()[0]
photo.original_image.url # > '/media/photos/birthday.tiff'
photo.formatted_image.url # > '/media/cache/photos/birthday_formatted_image.jpeg'


Check out imagekit.models.ImageSpec for more information.
更多信息请查看imagekit.models.ImageSpec

Processors 处理器

The real power of ImageKit comes from processors. Processors take an image, do something to it, and return the result. By providing a list of processors to your spec, you can expose different versions of the original image:
ImageKit真正的能力来自处理器。处理器对一个图像做一些处理,并返回一个结果。通过您的spec提供的处理器列表,你可以处理并展示不同版本的原始图像:


from django.db import models
from imagekit.models import ImageSpec
from imagekit.processors import resize, Adjust

class Photo(models.Model):
    original_image = models.ImageField(upload_to='photos')
    thumbnail = ImageSpec([Adjust(contrast=1.2, sharpness=1.1),
            resize.Crop(50, 50)], image_field='original_image',
            format='JPEG', quality=90)


The thumbnail property will now return a cropped image:
这个缩略图属性将返回一个裁剪图片:


photo = Photo.objects.all()[0]
photo.thumbnail.url # > '/media/cache/photos/birthday_thumbnail.jpeg'
photo.thumbnail.width # > 50
photo.original_image.width # > 1000


The original image is not modified; thumbnail is a new file that is the result of running the imagekit.processors.resize.Crop processor on the original.
原始图像不会被修改;缩略图是一个新的文件,是对原始图像运行imagekit.processors.resize.Crop处理器的结果。

The imagekit.processors module contains processors for many common image manipulations, like resizing, rotating, and color adjustments. However, if they aren’t up to the task, you can create your own. All you have to do is implement a process() method:
imagekit.processors模块包含许多处理器以供常见的图像处理,例如调整大小,旋转,色彩调整。但是,如果他们都没能达到的你的任务要求,你可以创建你自己的处理器。你所要做的就是实现一个process()的方法:


class Watermark(object):
    def process(self, image):
        # Code for adding the watermark goes here.
        return image

class Photo(models.Model):
    original_image = models.ImageField(upload_to='photos')
    watermarked_image = ImageSpec([Watermark()], image_field='original_image',
            format='JPEG', quality=90)


Admin 后台管理

ImageKit also contains a class named imagekit.admin.AdminThumbnail for displaying specs (or even regular ImageFields) in the Django admin change list. AdminThumbnail is used as a property on Django admin classes:
ImageKit还包含一个名为imagekit.admin.AdminThumbnail的类用来在 Django admin change list中显示specs(甚至常规的ImageFields)。 AdminThumbnail被用来作为Django admin类的属性:


from django.contrib import admin
from imagekit.admin import AdminThumbnail
from .models import Photo

class PhotoAdmin(admin.ModelAdmin):
    list_display = ('__str__', 'admin_thumbnail')
    admin_thumbnail = AdminThumbnail(image_field='thumbnail')

admin.site.register(Photo, PhotoAdmin)


AdminThumbnail can even use a custom template. For more information, see imagekit.admin.AdminThumbnail.
AdminThumbnail甚至可以使用一个自定义的模板。更多信息请查看imagekit.admin.AdminThumbnail

Commands 命令

Authors 作者

ImageKit was originally written by Justin Driscoll.
ImageKit 原作者 Justin Driscoll.

The field-based API was written by the bright minds at HZDG.


Maintainers 维护者

    Bryan Veloso
    Chris Drackett
    Greg Newman

Contributors 贡献者

    Josh Ourisman
    Jonathan Slenders
    Matthew Tretter
    Eric Eldredge
    Chris McKenzie
    Markus Kaiserswerth
    Ryan Bagwell

Digging Deeper 深入研究

    API Reference API参考
        models Module 模型模块
        processors Module 处理器模块
        admin Module 管理模块

------------------------------
本站原创,欢迎转载!  转载请注明出处,谢谢合作! 分享到QQ空间
最后编辑: 我就是个世界 编辑于November 4, 2011 00:01
发表评论
  昵称 [注册]
  密码 (游客无需密码)
  网址
  电邮
OpenID登入 权限选项 表情