什么是PIL库??
PIL库是python语言中用来进行对图像操作的第三方库,主要实现图像归档和图像处理两方面功能需求
1、图像归档:对图像进行批处理、生成图像预览、图像格式转换等
2、图像处理:图像的基本处理、像素处理、颜色处理等
cmd命令行安装方式pip install pillow
PIL库常用的类
1.Image
2.ImageEnhance
3.ImageFilter
…
Image类的基本操作
Image.open(file_peth) #打开图像,建议使用绝对路径,图像的一切操作是基于想获取打开图像
Image.open(StringIO.StringIO(buffer)) #从字符串中获取图像
Image.new(mode,size,color) #根据参数创建新图像
Image.frombytes(mode,size,data) #根据像素点获取图像
Image.verify() #对图像文件完整性整合,返回异常
********常用属性*********
from PIL import Image
im = Image.open(file_peth)
im.size #获取图像大小,返回像素元组
im.format #获取图像格式
im.mode #获取图像的色彩模式,彩色返回“RGB”/灰色返回“L”
im.tell() #获取图片当前帧 ,用于gif类的序列类图片
im.seek(frame) #跳转并返回图像的指定帧
可根据需要,学习相应的类
简易化批量处理图片程序
******************************
#开发时间:2021.01.26
#开发人员:偷偷学python
#项目名称:基于python的图片操作
#功能:获取gif系列类图片每一个动作、批量更改图片格式、批量更改图片大小、证件照制作
******************************
from PIL import Image #图像处理库
from removebg import RemoveBg #抠图库
import os #对文件夹操作的库
class Gif: #获取gif类系列图片每一帧
def __init__(self): #文件获取路径
self.path = input("请输入文件路径(回车结束):")
def open_file(self): #打开文件
self.im = Image.open(self.path)
def mkdir(self): #创建存储文件夹
self.a = input("是否新建图像存储路径(是:Y/y;否:N/y):")
if self.a in ('Y','y'):
self.root = input("输入创建新建图像存储路径:")
while os.path.exists(self.root) != False:
self.root = input("该文件夹已存在,请重新输入:")
else:
os.mkdir(self.root)
print("创建成功,路径为:{}".format(self.root).center(30,'-'))
else:
self.root = os.path.dirname(self.path)
print("默认操作文件路径".center(30,"-"))
def start_get(self): #序列类图片获取每帧
try:
self.im.save(self.root + "/" + "{:02d}.png".format(self.im.tell()))
print("操作中".center(20,'-'))
while True:
self.im.seek(self.im.tell()+1)
self.im.save(self.root + "/" + "{:02d}.png".format(self.im.tell()))
except:
print("操作完成".center(20,'-'))
class Re_Format(Gif): #批量更改图片格式
def __init__(self):
self.path = input("请输入文件夹路径(回车结束):")
self.fr = input("请输入要转化的格式(png,jpg,jpeg...):")
def re_format(self):
try:
for i in os.listdir(self.path):
if '.' in i:
file_name = os.path.basename(i).split(".")[0] # 获取每个图片名字
file_format = os.path.basename(i).split(".")[1] # 格式
else:
continue
self.im1 = Image.open(self.path + "/" + i) # 打开文件
if file_format in ("jpg","png","jpeg","jfif"):
self.im1.save(self.root + "/" + "{}.".format(file_name) + self.fr)
print("{0}转换{1}格式成功".format(file_name,self.fr))
else:
continue
except:
print("{0}不能转换成{1}".format(file_format,self.fr).center(30,'*'))
class Re_Size(Gif): #批量更改图片大小
def __init__(self):
self.path = input("请输入文件夹路径(回车结束):")
def get_xiangsu(self):
print("提示:这是一般情况下的常用尺寸像素值".center(30, '-') + \
"\n一 寸:358像素 * 441像素\n" + "二 寸:413像素 * 626像素\n" \
+ "小二寸:390像素 * 567像素\n" + "其它自定义像素")
self.new_weight = eval(input("输入调整的宽度(像素):"))
self.new_height = eval(input("输入调整的高度(像素)"))
def re_size(self):
for i in os.listdir(self.path):
if '.' in i:
self.file_name = os.path.basename(i).split(".")[0] # 获取每个图片名字
self.file_format = os.path.basename(i).split(".")[1] # 格式
else:
continue
self.im2 = Image.open(self.path + "/" + i) # 打开文件
weight, height = self.im2.size
print("{0}原始像素大小(宽 * 高):{1}".format(self.file_name,(weight, height)))
self.im2.thumbnail((self.new_weight, self.new_height))
self.im2.save(self.root + "/" + "{}_new.".format(self.file_name) + self.file_format)
print("{0}转换完成:{1}".format(self.file_name,(self.new_weight,self.new_height))+"\n")
class Re_bg(Gif):
def __init__(self):
self.path = input("请输入要处理的图片路径(回车结束):")
def get_mode(self):
self.im = Image.open(self.path)
self.file_name = os.path.basename(self.path).split(".")[0] # 获取每个图片名字
self.file_format = os.path.basename(self.path).split(".")[1] # 格式
def koutu(self):
rmbg = RemoveBg("API_KEY", "error.log") #API_KEY是秘钥,需要私信我偶
rmbg.remove_background_from_img_file(self.path) # 抠图
self.foregound = Image.open(os.path.dirname(self.path)+ "/"+"{0}.{1}_no_bg.png".format(self.file_name,self.file_format)) # 前景图
def mk_bg(self): #制作背景
weight, height = self.foregound.size
i = eval(input("请挑选背景色数字(0:白底;1:红底;2:蓝底):"))
if i == 0:
self.background = Image.new("RGB", (weight, height), "white")
elif i == 1:
self.background = Image.new("RGB", (weight, height), "red")
elif i == 2:
self.background = Image.new("RGB", (weight, height), "blue")
else:
print("请按要求输入".center(20,'*'))
def paste_(self):
self.background.paste(self.foregound, mask=self.foregound)
self.background.show()
self.background.save(os.path.dirname(self.path)+"{}_证件照.png".format(self.file_name))
key = eval(input("按指令相应的功能".center(20,'-')+\
"\n获取gif系列类图片每一个动作扣:1\n"+\
"批量更改图片格式扣:2\n"+"批量更改图片大小扣:3\n"+\
"证件照制作扣:4\n"+\
"请输入你需要的功能的键:"))
while key != (1 or 2 or 3 or 4):
key = eval(input("输入错误,按指令相应的功能".center(30,'*')+\
"\n获取gif系列类图片每一个动作扣:1\n"+\
"批量更改图片格式扣:2\n"+"批量更改图片大小扣:3\n"+\
"证件照制作扣:4\n"+\
"请输入你需要的功能的键:"))
else:
if key == 1:
gif = Gif()
gif.open_file()
gif.mkdir()
gif.start_get()
elif key == 2:
re_format = Re_Format()
re_format.mkdir()
re_format.re_format()
elif key == 3:
re_size = Re_Size()
re_size.get_xiangsu()
re_size.mkdir()
re_size.re_size()
elif key == 4:
re_bg = Re_bg()
re_bg.get_mode()
re_bg.koutu()
re_bg.mk_bg()
re_bg.paste_()
自己之前没有项目经验,不知道怎么写出满足用户需求的程序,求打佬多多指点,反正是满足我的需求了了了了,,小伙伴们不懂的私信我呀,新手大家一块学习呀!!!
本文介绍了Python的PIL库,用于图像处理和归档。内容包括图像打开、创建、格式转换、尺寸调整等基本操作,以及批量处理图片的示例代码,如批量更改图片格式、大小和制作证件照。通过提供的简易程序,用户可以实现对GIF图片的帧提取、图片格式转换和尺寸调整等功能。

被折叠的 条评论
为什么被折叠?



