Python图片处理_PIL学习记录
①:PIL获取,保存,更改图片
#调用模块
from PIL import Image
#打开图片
img1 :Image.Image = Image.open("./picture/001.jpg") #得到图片
img1.show() #显示得到的图片
img2 = img1.resize((1000,2000)) #更改图片大小
img2.save("./picture/002.jpg") #保存图片
②:更改图片格式
from PIL import Image
img1: Image.Image = Image.open("./picture/099.jfif")
img1.save("./picture/099.jpg")
③:添加文本信息
from PIL import Image,ImageDraw,ImageFont
img1: Image.Image = Image.open("./picture/099.jfif")
img1_draw = ImageDraw.Draw(img1)
# 选中字体,设置字体大小
img1_font = ImageFont.truetype("D:/iFontsClientFileCache/HYXieNTJ.ttf",240)
#(x,y):x左右,y上下
#输入的信息
#设置颜色
#选中字体
img1_draw.text((50,30),"学号:541913460XXX",fill='blue',font=img1_font)
img1_draw.text((50,250),"姓名:李XX",fill='red',font=img1_font)
img1.show()
④:获取图片信息
from PIL import Image
import numpy as np
img1: Image = Image.open("./picture/001.jpg")
img1 = img1.convert("RGB")
img_date = img1.getdata()
#list
img_list = list(img_date)
print(img_list)
#numpy
img_np =np.asarray(img_date)
print(img_np)
⑤:获取的数据变成图片
from PIL import Image
import numpy as np
k =0
lis =[]
for i in range(0,8):
cur =[]
lis.append(cur)
for j in range(0,8):
cur.append(k)
k = k+2
print(lis[0])
print(lis[1])
print(lis[2])
print(lis[3])
print(lis[4])
print(lis[5])
print(lis[6])
print(lis[7])
img =Image.fromarray(np.asarray(lis)).convert("L")
img.save("./099.jpg")
运行结果:
[0, 2, 4, 6, 8, 10, 12, 14]
[16, 18, 20, 22, 24, 26, 28, 30]
[32, 34, 36, 38, 40, 42, 44, 46]
[48, 50, 52, 54, 56, 58, 60, 62]
[64, 66, 68, 70, 72, 74, 76, 78]
[80, 82, 84, 86, 88, 90, 92, 94]
[96, 98, 100, 102, 104, 106, 108, 110]
[112, 114, 116, 118, 120, 122, 124, 126]
0-126 :逐渐变白
生成的图片:
⑥:提取红色通道
from PIL import Image
import numpy as np
img1: Image.Image = Image.open("./picture/000.png")
img1 = img1.convert("RGB")
np_array = np.array(img1)
for i in np_array: #遍历行
for j in i: #遍历列
j[1]=0
j[2]=0
img = Image.fromarray(np_array).convert("RGB")
img.save("./picture/test.jpg")
原图:
生成图片:
⑦:提取绿色通道,提取蓝色通道
代码改进:
from PIL import Image
import numpy as np
img1: Image.Image = Image.open("./picture/000.png")
img1 = img1.convert("RGB")
def get_R_Picture(img):
np_array = np.array(img)
for i in np_array: #遍历行
for j in i: #遍历列
j[1]=0
j[2]=0
img = Image.fromarray(np_array).convert("RGB")
img.save("./picture/test_R.jpg")
def get_G_Picture(img):
np_array = np.array(img)
for i in np_array: # 遍历行
for j in i: # 遍历列
j[0] = 0
j[2] = 0
img = Image.fromarray(np_array).convert("RGB")
img.save("./picture/test_G.jpg")
def get_B_Picture(img):
np_array = np.array(img)
for i in np_array: # 遍历行
for j in i: # 遍历列
j[0] = 0
j[1] = 0
img = Image.fromarray(np_array).convert("RGB")
img.save("./picture/test_B.jpg")
get_R_Picture(img1)
get_G_Picture(img1)
get_B_Picture(img1)
生成图:
⑧:图像的模式
from PIL import Image
img1: Image = Image.open("./picture/001.jpg")
img2 =img1.convert("CMYK")
img2.save("./picture/002.jpg")
img3 =img1.convert("RGB")
img3.save("./picture/003.jpg")
img4 =img1.convert("YCbCr")
img4.save("./picture/004.jpg")
print(img1.mode) //查看图片的格式
print(img2.mode)
print(img3.mode)
print(img4.mode)
运行结果:
RGB
CMYK
RGB
YCbCr
常见的图片格式,推荐使用RGB。
⑨:镜像
from PIL import Image,ImageDraw,ImageFont
img1 = Image.open("./picture/000.png")
#左右镜像
img1_lr = img1.transpose(Image.FLIP_LEFT_RIGHT)
img1_lr.save("./picture/000_lr.png")
#上下镜像
img1_tb = img1.transpose(Image.FLIP_TOP_BOTTOM)
img1_tb.save("./picture/000_tb.png")
#上下左右镜像
img1_lr_tb = img1.transpose(Image.FLIP_TOP_BOTTOM)
img2_lr_tb =img1_lr_tb.transpose(Image.FLIP_LEFT_RIGHT)
img2_lr_tb.save("./picture/000_lr_tb.png")
运行结果:
⑩:拼接图片
from PIL import Image,ImageDraw,ImageFont
#准备拼接的两个图片
img1 = Image.open("./picture/000.png")
img2 = Image.open("./picture/000_lr_tb.png")
print(img1.size) #显示(235, 182),表明图片长235,宽182
new_img = Image.new('RGB',(470,182),(255,255,255))
#图片格式,图片大小,底色
#拼接图片
new_img.paste(img1,(0,0))
new_img.paste(img2,(235,0))
new_img.save("./picture/000_pj.png")
生成图片:
⑪:切割图片
from PIL import Image,ImageDraw,ImageFont
img1 = Image.open("./picture/000_pj.png")
print(img1.size) # (470, 182)
img1_cut = img1.crop((100,20,370,100))
img1_cut.save("./picture/000_pj_cut.png")
生成图片:
⑫:图片缩放
from PIL import Image,ImageDraw,ImageFont
import numpy as np
img1 = Image.open("./picture/000_pj.png")
print(img1.size) #(470, 182)
img1.thumbnail((100,100)) #按缩放比例最大的算
img1.save("./picture/000_pj_jianxiao.png")
生成图片: