1 Star 0 Fork 0

Jay.Xiang/python_demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
image_test.py 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
Jay.Xiang 提交于 4年前 . PIL库的使用学习
"""
@author: 码农飞哥
@file: image_test.py
@date: 2021/9/29 14:56
@desc:
"""
from PIL import Image
import os.path as op
base_path = "picture"
try:
# 打开图片,返回Image对象
img = Image.open(op.join(base_path, "img1.jpeg"))
# 获取图片的宽高
width, height = img.size
print('图片的宽{0},高{1},'.format(width, height))
# 展示图片
img.show()
# 旋转45度
img.rotate(45).show()
# 图片缩放
img.thumbnail((width / 2, height / 2))
# 保存图片
img.save(op.join(base_path, 'thumbnail.jpeg'))
# 创建新Images
newImg = Image.new('RGB', (500, 500), (255, 0, 0))
newImg.save(op.join(base_path, 'newImg.png'))
# 复制图片
copyImg = newImg.copy()
newImg.save(op.join(base_path, 'copyImg.png'))
finally:
# 这种打开方式需要手动关闭文件流
img.close()
with Image.open(op.join(base_path, 'img1.jpeg')) as img:
# 获取图片的宽高
width, height = img.size
print('图片的宽{0},高{1},'.format(width, height))
# 将两张图贴起来
img2 = Image.open(op.join(base_path, 'img2.jpeg'))
img3 = Image.open(op.join(base_path, 'img3.png'))
img2.paste(img3)
img2.save(op.join(base_path, 'beautiful_paste.jpeg'), 'jpeg')
# 透明底
img2 = Image.open(op.join(base_path, 'img2.jpeg')).convert('RGBA')
img3 = Image.open(op.join(base_path, 'img3.png')).convert('RGBA')
# 获取r,g,b,a的值
r, g, b, a = img3.split()
# 传入透明值
img2.paste(img3, box=(0, 0), mask=a)
img2.save(op.join(base_path, 'beautiful_paste2.png'))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/jayxiang31/python_demo.git
git@gitee.com:jayxiang31/python_demo.git
jayxiang31
python_demo
python_demo
master

搜索帮助