日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python图像处理库:PIL中Image,ImageDraw等基本模块介绍

發(fā)布時(shí)間:2025/3/21 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python图像处理库:PIL中Image,ImageDraw等基本模块介绍 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

常用操作


合成 Image.blend(i1,i2,a)/Image.composite(i1,i2,mask)

縮略圖 thumbnail(size,filter=None)??
Modifies in-place,Preserves aspect ratio
>>> myImage.thumbnail ((128, 128), Image.ANTIALIAS)
剪切 crop(bbox)

>>> bounds = (100, 100, 400, 400)
>>> cutoutIm = myImage.crop (bounds)
粘貼 paste(i2,where,mask=None)/paste(color,box=None,mask=None)

旋轉(zhuǎn) rotate(theta)
rotated around its center

翻轉(zhuǎn)旋轉(zhuǎn) transpose(method)
ROTATE_90/180/270(clockwise), FLIP_TOP_BOTTOM(horizontal), FLIP_RIGHT_LEFT(vertical)
>>> fixedIm = myImage.transpose (ROTATE_90)

?

The Image Module


The Image module provides

  • a class with the same name which is used to?represent a PIL image.
  • The module also provides a number of?factory functions(including functions to?load images from files, and to?create new images)

圖像對(duì)象 Image?– from file or newly created

所有的圖片操作必須有一個(gè)操作對(duì)象,例如Pil提供open(filename)進(jìn)行這個(gè)過(guò)程,此后,一切關(guān)于圖片的操作均基于這個(gè)對(duì)象。有以下幾種創(chuàng)建image對(duì)象的方式:

1 Image.open(f)

>>> import Image >>> >>> Im = Image.open("lena.jpg") >>> print Im.mode,Im.size,Im.format RGB (256, 256) JPEG >>> Im.show()

如果文件不能打開(kāi),會(huì)拋出IOError異常。

可以查看image對(duì)象的format,mode,size,palette,info幾個(gè)屬性。

調(diào)用im.show()會(huì)在圖片查看工具中顯示當(dāng)前操作的image對(duì)象。

標(biāo)準(zhǔn)版本的show方法的實(shí)現(xiàn)不太高效,因?yàn)樗劝裪mage保存到一個(gè)臨時(shí)文件,然后調(diào)用xy工具來(lái)顯示圖像。如果你沒(méi)有安裝xy,那么它就無(wú)法工作了。不過(guò)如果它可以工作,倒還是非常方便用來(lái)debug和測(cè)試。

2 Image.new(mode,size,color=None)

color的默認(rèn)值是黑色,這里我們新建一個(gè)紅色的圖像。

>>> newIm = Image.new (“RGBA”, (640, 480), (255, 0, 0)) #新建一個(gè)image對(duì)象creating images from scratch

3 Image.blend(i1,i2,a)? -- (p1 x (1 - a) + p2 x a)

選一張灰度圖(L)做背景,和雷娜圖(RGB)做blend操作

>>> Im2 = Image.open("background.jpg").convert(Im.mode) >>> Im2 = Im2.resize(Im.size) >>> Im2.show() >>> >>> img = Image.blend(Im,Im2,0.2) >>> img.show()

?

操作完畢后save(filename)用以保存這個(gè)臨時(shí)的image對(duì)象img到硬盤(pán)。

4 Image.composite(i1,i2,mask)?? --equal-sized images i1 ,i2 and mask("1", "L", or "RGBA") (p1 x (1 - m) + p2 x m)

5 Image.eval(f,i)? -- applying a function f to each pixel of image i

6 Image.merge(mode,bandList)? --Creates a multi-band image from a sequence of single-band images of equal size

以下是Image對(duì)象的全部方法:

save(f,format=None) 保存 如果f是一個(gè)file對(duì)象,必須指定format(format codes)
convert(mode) 轉(zhuǎn)換mode ?
copy() ? ?
crop(bbox) 剪切 原圖中bbox區(qū)域
filter(name) 濾鏡 the name of predefined image enhancement filters
濾鏡名字需要import ImageFilter
getbands() 通道的字符串序列 如RGB圖返回('R', 'G', 'B')
getbbox() 包含非零區(qū)域的最小bbox ?
getextrema() 最大最小像素點(diǎn)值 min&max pixel value
單通道圖:返回元組(min,max)
多通道圖:返回各個(gè)通道的元組組成的元組
getpixel(xy) 取像素點(diǎn)值 坐標(biāo)xy處的pixel value or a sequence of pixel values
histogram(mask=None)

統(tǒng)計(jì)直方圖

單通道圖:返回列表[c0, c1, ...],ci是值為i的像素?cái)?shù)

多通道圖:a single sequence that is the concatenation of the sequences for all bands

mask參數(shù):a same-sized mask image of mode "1" or "L"(include only those pixels correspond to nonzero pixels in the mask argument)

offset(dx,dy=None)

平移

Returns a new image the same size as the original, but with all pixels rotated dx in the +x direction,and dy in the +y direction.

If dy is omitted, it defaults to the same value as dx.

paste(i2,where,mask=None) 粘貼圖片 where參數(shù)可以是
1 (x,y)坐標(biāo)對(duì):i2的像素點(diǎn)(0,0)對(duì)齊原圖中的(x,y)粘貼,i2超過(guò)原圖邊界的部分被拋棄
2 bbox:i2必須和該bounding box大小一致
3 None:i2必須和原圖大小一致
如果i2的mode和原圖不一致,粘貼前會(huì)被轉(zhuǎn)換。
mask參數(shù):a same-sized mask image of mode "1","L" or “RGBA ”(control which pixels get replaced)
paste(color,box=None,mask=None) 填充顏色 如果box省略,整個(gè)圖被填充為color色;mask參數(shù)同上
point(function) 改變像素點(diǎn)(函數(shù)) Returns a new image with each pixel modified.
point(table) 改變像素點(diǎn)(查表) To translate pixels using a table(a sequence of 256n values, where n is the number of bands in the image) lookup
putalpha(band)

改變alpha通道

The pixels of the band image(same-sized,"L" or "1") replace the alpha band(A) of the original image(RGBA) in place.

putpixel(xy, color) 改變單個(gè)像素點(diǎn)顏色 Note that this method is relatively slow. For more extensive changes, use paste or theImageDraw?module instead.
resize(size,filter=None) 調(diào)整大小 ?
rotate(theta)

旋轉(zhuǎn)(圍繞圖片中心)

?

Any pixels that are not covered by rotation of the original image are set to black.

show()

顯示圖片

On Unix systems, this method runs the xv image viewer to display the image.?
On Windows boxes,the image is saved in BMP format and can be viewed using Paint.?
This can be useful for debugging.

split()

分離通道

返回各個(gè)通道的灰度圖組成的元組
Returns a tuple containing each band of the original image as an image of mode "L".?
For example, applying this method to an "RGB" image produces a tuple of three images, one each for the red, green, and blue bands.

thumbnail(size,filter=None) 縮略圖 Modifies in-place,Preserves aspect ratio
transform(xs, ys, Image.EXTENT, (x0,y0,x1,y1)) ?

Returns a transformed copy of the image. In the transformed image, the point originally at (x0,y0) will appear at (0,0), and point (x1,y1) will appear at (xs, ys).

transform(xs, ys, Image.AFFINE, (a,b,c,d,e,f)) affine變換

The values a through f are the first two rows of an affine transform matrix.
Each pixel at (x,y) in the resulting image comes from position (ax+by+c,dx+ey+f) in the input
image, rounded to the nearest pixel.

transpose(method) 翻轉(zhuǎn)旋轉(zhuǎn) ROTATE_90/180/270(clockwise), FLIP_TOP_BOTTOM(horizontal), FLIP_RIGHT_LEFT(vertical)

?

The ImageDraw Module


支持2D圖像?The ImageDraw module provide basic 2D graphics support for Image objects.
It can for example be used to

  • create new images,
  • annotate or retouch existing images, and to?generate graphics on the fly for web use.

For a more advanced drawing library for PIL, see?The aggdraw Module.

創(chuàng)建繪畫(huà)對(duì)象?ImageDraw module creates drawing surface for image

import Image, ImageDraw im = Image.open(“vacation.jpeg") drawSurface = ImageDraw.Draw(im)

基本繪畫(huà)操作?Basic methods of drawing surface

  • 弧/弦/扇形 chord arc pieslice (bbox, strtAng, endAng)
  • 橢圓 ellipse (bbox)
  • 線段/多段線 line (L)? draw.line(((60,60),(90,60), (90,90), (60,90), (60,60))) #draw a square
  • 點(diǎn) point (xy)? #單像素點(diǎn)很小看不清,實(shí)際中可用實(shí)心小圓代替
  • 多邊形 polygon (L) draw.polygon([(60,60), (90,60), (90,90), (60,90)]) #draw a square
  • 矩形 rectangle (bbox)?????? # first coord屬于矩形, second coord不屬于
  • 文字 text(xy,message,font=None) 繪制文字message,文本區(qū)域左上角坐標(biāo)為xy
    ????? drawable.text((10, 10), "Hello", fill=(255,0,0), font=None)
  • 文字大小 textsize(message,font=None)? 給定文字message,返回所占像素(width,height)

可選參數(shù)?Common optional args for these methods

  • fill=fillColor
  • outline=outlineColor

矢量字體支持?TrueType Font support

import ImageFont ttFont = ImageFont.truetype (“arial.ttf”, 16) drawable.text ((10, 10), “Hello”, fill=(255,0,0), font=ttFont)

例子:Draw a Grey Cross Over an Image

import Image, ImageDraw im = Image.open("lena.pgm") # Creates an object that can be used to draw in the given image. draw = ImageDraw.Draw(im) # draw.line(xy, options) => Draws a line between the coordinates in the xy list.# The coordinate list can be any sequence object containing either 2-tuples [ (x, y), ... ] # or numeric values [ x, y, ... ]. # The fill option gives the color to use for the line. draw.line((0, 0) + im.size, fill=128) draw.line((0, im.size[1], im.size[0], 0), fill=128) del draw # write to stdout im.save(sys.stdout, "PNG")

?

The ImageChops module


a number of arithmetical image operations, called?channel operations?("chops" 通道操作).

These can be used for various purposes, including special effects 特殊效果, image compositions 圖像合成, algorithmic painting 算法繪畫(huà), and more.

At this time, channel operations are?only implemented for 8-bit images?(e.g. "L" and "RGB").

例子:比較兩幅圖像

Exact Comparison:

The quickest way to determine if two images have exactly the same contents is to get the difference between the two images, and then calculate the bounding box of the non-zero regions in this image. If the images are identical, all pixels in the difference image are zero, and the bounding box function returns?None.

import ImageChops def equal(im1, im2): return ImageChops.difference(im1, im2).getbbox() is None

To get a measure of how similar two images are, you can calculate the root-mean-square (RMS) value of the difference between the images. If the images are exactly identical, this value is zero. The following function uses the?difference?function, and then calculates the RMS value from the histogram of the resulting image.

RMS Difference:

To get a measure of how similar two images are, you can calculate the root-mean-square (RMS) value of the difference between the images. If the images are exactly identical, this value is zero. The following function uses the?difference?function, and then calculates the RMS value from the histogram of the resulting image.

# Example: File: imagediff.pyimport ImageChops import math, operator def rmsdiff(im1, im2): "Calculate the root-mean-square difference between two images" h = ImageChops.difference(im1, im2).histogram() # calculate rmsreturn math.sqrt(reduce(operator.add, map(lambda h, i: h*(i**2), h, range(256)) ) / (float(im1.size[0]) * im1.size[1]))
from: http://www.cnblogs.com/wei-li/archive/2012/04/19/2456725.html

總結(jié)

以上是生活随笔為你收集整理的Python图像处理库:PIL中Image,ImageDraw等基本模块介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 波多野结衣在线播放 | 九一av| 日本色悠悠 | 国产原创视频在线 | 韩日产理伦片在线观看 | 超碰在线免费播放 | 波多野结衣中文字幕一区二区三区 | 精品人妻一区二区三区日产 | 久久久久亚洲精品系列色欲 | 中文字幕欧美激情 | 日韩一区二区三区三四区视频在线观看 | 午夜之声l性8电台lx8电台 | 亚洲永久视频 | 亚洲av无码一区二区三区网站 | 黄色一级网 | av激情影院 | 日韩av电影网 | 日韩成人高清视频在线观看 | 精品人妻一区二区三区换脸明星 | 亚洲av区无码字幕中文色 | 欧美黑人性受xxxx精品 | av电影免费在线播放 | 欧美成人a∨高清免费观看 国产精品999视频 | 少妇高潮一区二区三区在线 | 秋霞毛片少妇激情免费 | 亚洲精品中文字幕在线观看 | 日本久久精品 | 九九热九九爱 | 国产精美视频 | 波多野结衣福利 | 欧美一级片在线播放 | 日韩在线播放中文字幕 | 亚洲专区一区二区三区 | mm1313亚洲国产精品无码试看 | 亚洲AV无码一区二区三区性 | 久久香蕉精品视频 | 99热在线国产 | 日韩精品中文字幕在线播放 | 婷婷综合 | 91免费精品视频 | 亚洲视频欧美视频 | av黄色在线播放 | 91麻豆免费看 | 欧美一区二区久久久 | 国产精品入口麻豆 | 东凛在线观看 | 中文字幕一区二区三区5566 | 亚洲av无码一区二区三区在线观看 | 日本全黄裸体片 | 欧美精品在线观看视频 | 99久久精品国产毛片 | 欧美做爰全过程免费看 | 91丨国产| a天堂中文在线观看 | 国产精品亚洲AV色欲三区不卡 | 青娱乐在线视频观看 | 亚洲香蕉久久 | 日日日日日日bbbbbb | 五月婷婷亚洲综合 | 黄色一极视频 | 国产精品毛片一区 | 国产在线不卡视频 | 国产原创中文av | 精品一区亚洲 | 亚洲网站在线免费观看 | 午夜爱爱毛片xxxx视频免费看 | 伊人色综合网 | 国产v亚洲| 国产乱码精品一区二区三区精东 | ww黄色 | 午夜精品福利视频 | 男人天堂怡红院 | 亚洲成人1区 | 夜夜操天天爽 | 加勒比久久综合 | 国产一区二区视频在线播放 | 免费a大片 | 日韩欧美小视频 | 最近最新最好看的2019 | 日韩一卡二卡三卡四卡 | 黄a毛片| 外国电影免费观看高清完整版 | 一区二区三区四区人妻 | 成人免费午夜 | 久久精品综合网 | 中文字幕av解说 | 高级毛片| 蜜臀视频在线观看 | 亚欧洲精品在线视频 | 九热视频在线观看 | 在线成人中文字幕 | 欧美美女性视频 | 国产深喉视频一区二区 | 欧美 亚洲 激情 一区 | 欧美日韩久久久久久 | 少妇久久久久久 | 91久久国产综合久久 | 成人精品一区二区三区中文字幕 | 拔插拔插影库 |