from PIL import ImageColor # pip install pillow# http://pillow-zh-cn.readthedocs.io/zh_CN/latest/installation.html
ImageColor.getcolor(‘red‘, ‘RGB‘)
(255, 0, 0)
ImageColor.getcolor(‘red‘, ‘RGBA‘) # A 透明度,png图片
(255, 0, 0, 255)
# 切换到工作目录,有图片文件的地方%cd D:\python全站\python处理图片%cd D:\python全站\新建文件夹\py2018-鏃堕棿API閭欢鐓х墖\py2018\02-auto\image_ctrl
D:\python全站\python处理图片D:\python全站\新建文件夹\py2018-鏃堕棿API閭欢鐓х墖\py2018\02-auto\image_ctrl
%pwd
‘D:\\python全站\\新建文件夹\\py2018-鏃堕棿API閭\ue1bb欢鐓х墖\\py2018\\02-auto\\image_ctrl‘
from PIL import Image# 创建一个缩略图# 打开一个jpg图像文件,注意是当前路径im = Image.open(‘lulu.jpg‘)print(im.format, im.size, im.mode)# 获取图像尺寸w, h = im.sizeprint(‘尺寸:%s%s‘ %(w,h))# 缩放到50%im.thumbnail((w//2, h//2)) # // 整除print(‘Resize image to %s%s:‘ %(w//2, h//2))# 吧缩放后的图像用jpeg格式保存im.save(‘thumbnail.jpg‘, ‘jpeg‘)
JPEG (960, 542) RGB尺寸:960542Resize image to 480271:
# 显示图像im.show()
# 调整大小im_sizec = im.resize((w//4, h//4))im_sizec.save(‘cc-1-4.jpg‘)
# 增强效果from PIL import ImageEnhanceenh = ImageEnhance.Contrast(im)enh.enhance(1.3).show(‘30%增强对比‘)
# 裁剪图像box = (100,100,400,400)region = im.crop(box)region.save(‘cc-300-300.jpg‘)region.show()
# 旋转图像im.rotate(90).save(‘cc-90.jpg‘)
# 镜像翻转im.transpose(Image.FLIP_LEFT_RIGHT).save(‘cc-水平.jpg‘)im.transpose(Image.FLIP_TOP_BOTTOM).save(‘cc-上下.jpg‘)
# 添加水印,复制图片,计算位置,粘贴合并图片# 打开图片文件logo_file = ‘cc.jpg‘im_logo = Image.open(logo_file)logo_width, logo_height = im_logo.size# 打开目标文件target = ‘py-banner.jpg‘im_target = Image.open(target)target_width, target_height = im_target.size# 粘贴im_copy = im_target.copy()im_copy.paste(im_logo, (target_width-logo_width, target_height-logo_height))im_copy.save(‘cc-logo.jpg‘)
Python-ps Pictures