Dec
26

[pil-handbook翻译]使用指南-2

Author laihj    Category 善其事     Tags , , ,

剪切,粘贴与合并图像
    Image类所包含的方法可以让你对图像的某个区域进行操作。取到图像的一个子区,可以使用crop方法。
    Example: 从图像中复制一小框

box = (100, 100, 400, 400)
region = im.crop(box)

    这个区域以一个四元组定义,其坐标表示为(左,上,右,下).PIL使用一个以(0,0)代表左上角的坐标系统。同时也要注意坐标指向的位置是以像素标识,所以在上面这个例子中的区域刚好是300×300像素。
    现在,这个图区可以做一些处理然后粘贴回去。
    Example: 处理一个子区,再贴回去

region = region.transpose(Image.ROTATE_180)
im.paste(region, box)

    当粘贴一个区域时,该区域的大小必须与给定的大小吻合,另外,粘贴区也不能延展到图像外面。但是,原始图像和粘贴区域的格式并不需要一致。如果它们不一致,粘贴域会自动进行转化。(在下方的颜色转换部分会有详细讨论)
    这里是另一个例子
    Example: Rolling an image

def roll(image, delta):

“Roll an image sideways”
xsize, ysize = image.size
delta = delta % xsize
if delta == 0: return image
part1 = image.crop((0, 0, delta, ysize))
part2 = image.crop((delta, 0, xsize, ysize))
image.paste(part2, (0, 0, xsize-delta, ysize))
image.paste(part1, (xsize-delta, 0, xsize, ysize))
return image

    更进一步,使用一个可选参数,paste方法也可以实现透明蒙版效果。在这个蒙版中,值255表示这个粘贴图像在该位置完全不透明,值0意味着粘贴图像是完全透明的,而这之间的值则表示不同的透明度。
    PIL 也使你可以操作一个多道道图像的单个颜色通道。比如一个RGB图像。split方法创建一个新的图像集,其中每一个图像包含量原始图像的一个颜色通道。 merge方法使用两个参数,一个是模式,一个是图像组,将图像组重新组成合一个新的图像。下面这个例子将三个颜色通道合成一个RGB图像:
Example: 分割和合并颜色通道

r, g, b = im.split()
im = Image.merge(”RGB”, (b, g, r))

几何变换
    图像库提供方法来缩放和旋转图像。前者需要以二元组的方式提供新的尺寸参数,后者则需要一个逆时针方向的角度。
    Example: 简单几何变换。

out = im.resize((128, 128))
out = im.rotate(45) # degrees counter-clockwise
    将一枚图像旋转90度,既可以使用rotate方法,也可以使用transpose方法,后者同时也可以用于将一枚图像沿着它作重直或水平旋转。
    Example: 变换一坨图像

out = im.transpose(Image.FLIP_LEFT_RIGHT)
out = im.transpose(Image.FLIP_TOP_BOTTOM)
out = im.transpose(Image.ROTATE_90)
out = im.transpose(Image.ROTATE_180)
out = im.transpose(Image.ROTATE_270)

    使用transpose(ROTATE),或选用相应的rotate操作,在结果和显示上并无不同。
    更通用的图像变换方式可以使用transform方法。在后面的reference可以找到相关细节。

上一页  PIL目录  下一页

3 Comments to “[pil-handbook翻译]使用指南-2”

  • [pil-handbook翻译]指用指南-3 | 右舷 12/27/2008 at 11:38 am

    [...] 上一页 PIL目录 下一页 [...]

  • shirz 09/02/2011 at 10:26 am

    在windows Vsista系统下PIL im.show()命令不工作,这个网址有一个解决办法,我试了一下,的确管用。
    编辑你的pil下的ImageShow.py文件(比如C:\Python26\lib\site-packages\PIL\ImageShow.py),将第99行return语句用
    return “start /wait %s && PING 127.0.0.1 -n 5 > NUL && del /f %s” % (file, file)

    替换即可。
    ——————–
    网址 http://www.velocityreviews.com/forums/t707158-python-pil-and-vista-windows-7-show-not-working.html
    相关解释:
    What actually happens is that the Windows code relies on the fact that the default image viewer on Windows XP was able to work at blocking mode – this means that the command will wait until the image window will be closed. Because of that behaviour, they constructed a command line which deletes a file right after the image has been shown.

    Windows Vista doesn’t work the same way, it immediately returns once the command has been executed, so what happens now is that the temporary file gets immediately deleted by the command line, and the image viewer doesn’t have enough time to load the image before it is being deleted.

    Here is a quick workaround:

    Edit C:\Python26\lib\site-packages\PIL\ImageShow.py, and around line 99, replace with the following line:

    return “start /wait %s && PING 127.0.0.1 -n 5 > NUL && del /f %s” % (file, file)

  • shirz 09/02/2011 at 11:03 am

    PIL RGB图像合并不工作。需要加一个语句:
    im.load()

    Example: 分割和合并颜色通道

    r, g, b = im.split()

    im.load()

    im = Image.merge(”RGB”, (b, g, r))

Post comment