

# 方法1pip install Pillow# 方法2pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ Pillow
from PIL import Image
Image.open(fp, mode='r')
Image.new(mode, size, color=0)
参数:
mode:图片模式。
size:一个二元组,包含(宽度,高度)像素。
Image.copy()
Image.paste(im, box=None, mask=None)
Image.save(fp, format=None, **params)
# 创建新图片defcreate_new_image(image): width, height = image.sizeif width > height: new_image = Image.new(image.mode, (width, width), color='white') new_image.paste(image, (0, int((width - height) / 2)))else: new_image = Image.new(image.mode, (height, height), color='white') new_image.paste(image, (int((height - width) / 2), 0))return new_image
# 生成9个子图defget_9_images(image): width, height = image.size new_image_width = int(width / 3) boxs = []for i in range(0, 3):for j in range(0, 3): box = (j * new_image_width, i * new_image_width, (j + 1) * new_image_width, (i + 1) * new_image_width) boxs.append(box) images = [image.crop(box) for box in boxs]return images
# 保存图片defsave_images(images, file_path):ifnot os.path.exists(file_path): os.mkdir(file_path)for index,image in enumerate(images): new_image = os.path.join(file_path, str(index) + '.jpg') image.save(new_image)


以上就是本期为大家整理的全部内容了,喜欢的朋友可以点赞、点在看
也可以分享让更多人知道。
往期推荐
源码下载 | 【01-50】Pthon可视化系列文章资源(源码+数据)
53 | 基于Lasso回归和随机森林的上海链家二手房房价预测
52 | 基于KNN近邻和随机森林模型对用户转化进行分析与预测
Pandas+Pyecharts | 全国热门旅游景点数据分析可视化
可视化 | 分享一套Flask+Pyecharts可视化模板
用Python分析了3W+《独行月球》影评数据,看看观众们怎么说~

