温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Pillow模块怎么在python中使用

发布时间:2021-03-18 15:58:56 来源:亿速云 阅读:300 作者:Leah 栏目:开发技术

本篇文章为大家展示了Pillow模块怎么在python中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

0、使用pip安装

$ pip install pillow

1、使用easy_install

$ easy_install pillow

2、通过pycharm安装

1、学习并使用pillow库

#导入模块
from PIL import Image
#读取文件
img = Image.open('test.jpg')
#保存文件
#img.save(filename,format)
img.save(filename,"JPEG")
#获取图片大小
(width,height) = img.size
#获取图片的源格式
img_format = img.format
#获取图片模式,有三种模式:L(灰度图像),RGB(真彩色)和CMYK(pre-press图像)
img_mode = img.mode
#图片模式的转换
img = img.convert("L") #转化成灰度图像
#获取每个坐标的像素点的RGB值
r,g,b = img.getpixel((j,i))
#重设图片大小
img = img.resize(width,height)
#创建缩略图
img.thumbnail(size)

2、实战演练

其实应该很容易想到,如果要达到这种效果,应该能想得到就是获取图上每一点的RGB值,然后根据这三种值确定这一点采用什么字符,其实根据RGB来确定的交灰值,所以可以将图片转化成灰度图片,来直接获取每一点的灰度,或者通过灰度的转换公式来使得RGB三值转化成灰度

#coding:utf-8
from PIL import Image
#要索引的字符列表
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
length = len(ascii_char)
img = Image.open('03.jpg')  #读取图像文件
(width,height) = img.size
img = img.resize((int(width*0.9),int(height*0.5))) #对图像进行一定缩小
print(img.size)
def convert(img):
 img = img.convert("L") # 转为灰度图像
 txt = ""
 for i in range(img.size[1]):
  for j in range(img.size[0]):
   gray = img.getpixel((j, i))  # 获取每个坐标像素点的灰度
   unit = 256.0 / length
   txt += ascii_char[int(gray / unit)] #获取对应坐标的字符值
  txt += '\n'
 return txt

def convert1(img):
 txt = ""
 for i in range(img.size[1]):
  for j in range(img.size[0]):
   r,g,b = img.getpixel((j, i))   #获取每个坐标像素点的rgb值
   gray = int(r * 0.299 + g * 0.587 + b * 0.114) #通过灰度转换公式获取灰度
   unit = (256.0+1)/length
   txt += ascii_char[int(gray / unit)] # 获取对应坐标的字符值
  txt += '\n'
 return txt

txt = convert(img)
f = open("03_convert.txt","w")
f.write(txt)   #存储到文件中
f.close()

给图片加上文字(福利预警,前方有福利!!!!)

#coding:utf-8
from PIL import Image,ImageDraw,ImageFont

#http://font.chinaz.com/zhongwenziti.html 字体下载网站

img = Image.open('PDD01.jpg')
draw = ImageDraw.Draw(img)
myfont = ImageFont.truetype('HYLiuZiHeiJ.ttf',size=80)
fillcolor = 'pink'
(width, height) = img.size
#第一个参数是加入字体的坐标
#第二个参数是文字内容
#第三个参数是字体格式
#第四个参数是字体颜色
draw.text((40,100),u'萌萌哒',font=myfont,fill=fillcolor)
img.save('modfiy_pdd01.jpg','jpeg')

给图片加上数字

这个大家应该见过的,就是有些头像的左上角的那个小红圈加上白色的数字,其实方法和上面那个加文字的差不多 

讲道理,我还不如用ps,移坐标移到要死要死的

#coding:utf-8
from PIL import Image,ImageDraw,ImageFont
img = Image.open("03.jpg")
draw = ImageDraw.Draw(img)
myfont = ImageFont.truetype(u"时光体.ttf",50)
(width,height) = img.size
draw.ellipse((width-40,0,width,40),fill="red",outline="red") #在图上画一个圆
draw.text((width-30,-8),'1',font=myfont,fill='white')
img.save('03_modify.jpg')

生成4位随机验证码

#coding:utf-8
from PIL import Image,ImageDraw,ImageFont,ImageFilter
import random
"""
创建四位数的验证码
"""
#产生随机验证码内容
def rndTxt():
 txt = []
 txt.append(random.randint(97,123))  #大写字母
 txt.append(random.randint(65,90))  #小写字母
 txt.append(random.randint(48,57))  #数字
 return chr(txt[random.randint(0,2)])

#随机颜色(背景)
def rndColor1():
 return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))

#随机颜色(字体)
def rndColor2():
 return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))

#240x60:
width = 60*4
height = 60
img = Image.new('RGB',(width,height),(255,255,255))
font = ImageFont.truetype(u'时光体.ttf',36)
draw = ImageDraw.Draw(img)
#填充每个像素
for x in range(width):
 for y in range(height):
  draw.point((x,y),fill=rndColor1())

#输出文字
for txt in range(4):
 draw.text((60*txt+10,10),rndTxt(),font=font,fill=rndColor2())
#模糊化处理
#img = img.filter(ImageFilter.BLUR)
img.save("code.jpg")

上述内容就是Pillow模块怎么在python中使用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI