1、ImageColor.getcolor()
Pillow模块提供ImageColor.getcolor(),该函数接受一个颜色名称字符串作为第一个参数,字符串’RGBA’作为第二个参数,返回一个 RGBA 元组 (红,绿,蓝,alpha透明度)
>>> from PIL
import ImageColor
>>> ImageColor
.getcolor
('red', 'RGBA')
(255, 0, 0, 255)
>>> ImageColor
.getcolor
('RED', 'RGBA')
(255, 0, 0, 255)
>>> ImageColor
.getcolor
('Black', 'RGBA')
(0, 0, 0, 255)
2、坐标与Box元组
矩形包括左、顶坐标,但不包括右、底坐标,如图。
3、处理Image数据类型(打开图片,新建图片)
打开图片
from PIL
import Image
cat_image
= Image
.open('zophie.png')
width
, height
= cat_image
.size
新建图片
from PIL
import Image
test_image1
= Image
.new
('RGBA', (100, 200), 'purple')
test_image1
.save
('image1.png')
test_image2
= Image
.new
('RGBA', (20, 20))
test_image2
.save
('image2.png')
4、裁剪图片
Image 对象的 crop()方法接受一个矩形元组(因此crop()传递参数时有双重括弧),返回一个 Image 对象,表示裁剪后的图像。返回的是新Image对象,也就是说会生成裁剪后的图片,而不是在原图基础上裁剪再保存。
from PIL
import Image
cat_image
= Image
.open('zophie.png')
cropped_image
= cat_image
.crop
((335, 345, 565, 560))
cropped_image
.save
('cropped_image.png')
5、复制粘贴图像到另一图像上
from PIL
import Image
cat_image
= Image
.open('zophie.png')
cat_copyimage
= cat_image
.copy
()
cropped_image
= cat_image
.crop
((335, 345, 565, 560))
face_im_width
, face_im_height
= face_image
.size
cat_im_width
, cat_im_height
= cat_image
.size
for left
in range(0, cat_im_width
, face_im_width
):
for top
in range(0, cat_im_height
, face_im_height
):
print("paste at the point of (%s, %s) now..."%(left
, top
))
cat_copyimage
.paste
(face_image
, (left
, top
))
cat_copyimage
.save
('tiled.png')
<image对象>.copy()用于复制;<被粘贴的image对象>.paste(<要粘贴到别处的image对象>,(坐标值,以被粘贴的image图像为坐标系,要粘贴到别处的image的左上角来定位))。如cat_copyimage.paste(face_image, (100, 200))
6、调整图像大小
resized_image
= cat_copyimage
.resize
((cat_im_width
, int(cat_im_height
/2)))
resized_image
.save
('resize.png')
resize()方法在 Image 对象上调用,返回指定宽度和高度的一个新 Image 对象。 它接受两个整数的元组作为参数(因此resize传递参数时有双重括弧),表示返回图像的新高度和宽度。这个是使得图像变胖变瘦,与crop()区别。
7、旋转图像或镜像图像
>>> catIm
.rotate
(6).save
('rotated6.png')
>>> catIm
.rotate
(6, expand
=True).save
('rotated6_expanded.png')
>>>catIm
.transpose
(Image
.FLIP_LEFT_RIGHT
).save
('horizontal.png')
>>>catIm
.transpose
(Image
.FLIP_TOP_BOTTOM
).save
('vertical.png')
8、更改单个像素点
单个像素的颜色可以通过 getpixel()和putpixel()方法取得和设置。它们都接受一个元组,表示像素的 x 和 y 坐标。putpixel()方法还接受一个元组,作为该像素的颜色。这个顔色参数是四整数 RGBA 元组或三整数 RGB 元组。
from PIL
import Image
from PIL
import ImageColor
block_image
= Image
.new
('RGBA', (100, 100))
for i
in range(0, 50):
for j
in range(0, 50):
block_image
.putpixel
((i
, j
), ImageColor
.getcolor
('purple', 'RGBA'))
for i
in range(50, 100):
for j
in range(0, 50):
block_image
.putpixel
((i
, j
), ImageColor
.getcolor
('red', 'RGBA'))
for i
in range(0, 50):
for j
in range(50, 100):
block_image
.putpixel
((i
, j
), ImageColor
.getcolor
('pink', 'RGBA'))
for i
in range(50, 100):
for j
in range(50, 100):
block_image
.putpixel
((i
, j
), ImageColor
.getcolor
('blue', 'RGBA'))
block_image
.save
('block_image.png')
9、用ImageDraw.Draw()画图
from PIL
import Image
, ImageDraw
im
= Image
.new
('RGBA', (200, 200), 'white')
draw
= ImageDraw
.Draw
(im
)
draw
.line
([(0, 0), (199, 0), (199, 199), (0, 199), (0, 0)], fill
='black')
draw
.rectangle
((20, 30, 60, 60), fill
='blue')
draw
.ellipse
((120, 30, 160, 60), fill
='red')
draw
.polygon
(((57, 87), (79, 62), (94, 85), (120, 90), (103, 113)), fill
='brown')
for i
in range(100, 200, 10):
draw
.line
([(i
, 0), (200, i
- 100)], fill
='green')
im
.save
('drawing.png')