django-imagekit ImageKit 1.0.1中文文档翻译

作者:我就是个世界 发表于:2011-11-03
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文水平有限,有不到之处,请见凉!
-----------------------------
原英文文档 [url=http://readthedocs.org/docs/django-imagekit/en/latest/]ImageKit 1.0.1 documentation[/url]

[b] Getting Started  快速入门[url=http://readthedocs.org/docs/django-imagekit/en/latest/index.html#getting-started]¶[/url][/b]

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”,可以包括诸如不同尺寸(例如缩略图)和黑白的版本。[separator]

[b]Installation 安装[/b]

    1. [color=#4169E1]pip install django-imagekit[/color] (or clone the source and put the imagekit module on your path)
    2. Add [color=#4169E1]'imagekit'[/color] to your [color=#4169E1]INSTALLED_APPS[/color] list in your project’s settings.py

[b]Adding Specs to a Model 添加配置Specs 到模型中[/b]

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

[code]
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)
[/code]

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

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

Check out [color=#4169E1]imagekit.models.ImageSpec[/color] for more information.
更多信息请查看[color=#4169E1]imagekit.models.ImageSpec[/color]

[b]Processors 处理器[/b]

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提供的处理器列表,你可以处理并展示不同版本的原始图像:

[code]
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)
[/code]

The [color=#4169E1]thumbnail[/color] property will now return a cropped image:
这个缩略图属性将返回一个裁剪图片:

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

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

The [color=#4169E1]imagekit.processors[/color] 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 [color=#4169E1]process()[/color] method:
[color=#4169E1]imagekit.processors[/color]模块包含许多处理器以供常见的图像处理,例如调整大小,旋转,色彩调整。但是,如果他们都没能达到的你的任务要求,你可以创建你自己的处理器。你所要做的就是实现一个[color=#4169E1]process()[/color]的方法:

[code]
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)
[/code]

[b]Admin 后台管理[/b]

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

[code]
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)
[/code]

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

[b]Commands 命令[/b]

[b]Authors 作者[/b]

ImageKit was originally written by [url=http://github.com/jdriscoll]Justin Driscoll[/url].
ImageKit 原作者 [url=http://github.com/jdriscoll]Justin Driscoll[/url].

The field-based API was written by the bright minds at [url=http://hzdg.com/]HZDG[/url].


[b]Maintainers 维护者[/b]

    Bryan Veloso
    Chris Drackett
    Greg Newman

[b]Contributors 贡献者[/b]

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

[b]Digging Deeper 深入研究[/b]

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

------------------------------
[color=#FF0000]本站原创,欢迎转载!  转载请注明出处,谢谢合作![/color]

分享:

扫一扫在手机阅读、分享本文

请发表您的评论