2019年暑假培训总结

IMG相关

PIL学习

  1. 将数组转换成图片
	 img = np.random.randint(0,255,90000*3)
	 img = img.reshape(300,300,3)
	 #强转一下,因为图片里面的矩阵类型都是int8
	 img = np.array(img,dtype=np.int8)
	 #将数组转成图片1
	 img = Image.fromarray(img,mode="RGB")
	 img.show()
  1. 将图片转为指定通道数
	 image = image.convert("L")
	 image.show()
  1. 得到图片的颜色的频率分布
	pr = img.histogram()
  1. 图片粘贴,抠图,缩小
	#进行粘贴 (500,100)表示的是坐标,img2粘贴到img1上去
	img1.paste(img2,(200,500))
	#抠图
	img1 = img1.crop((200,169,500,500))
	#缩小
	w,h = img1.size
	img1.thumbnail((w//2,h//2))
	 
  1. 梯度下降演示
import  random
xs = [x*0.1 for x in range(0,10)];
ys = [x*0.6+0.32 for x in xs];
w  = random.random()
b = random.random()

for _ in range(1000):
    for x,y in zip(xs,ys):
        o = w*x+b
        e =o-y;
        loss = e**2

        dw = -2*e*x
        db = -2*e

        w = w+0.1*dw
        b = w+ 0.1*db;
        print(w,b)


  1. 利用plt画出动画图像效果,如下图是sigmod随着w不同,而动态变化的代码
import  numpy as np
import matplotlib.pyplot as plt
x =  np.linspace(-3,3,500)


def  sigmod(x):
    return 1/(1+np.exp(-x));
plt.ion()#开启会话,动态画图之前,调用该方法,则不用show了
for i in range(100):
    w+=0.1
    y = sigmod(w*x);
    plt.clf();#清空
    plt.plot(x, y)
    #锁定范围
    plt.xlim(-3,3)
    plt.ylim(0,1)
    plt.pause(0.1)#休眠0.1秒
plt.ioff() #关闭会话
y = sigmod(x)
plt.plot(x,y)
plt.show()
  1. 动态播放图片,PIL与plt的转换
import  numpy as np
import matplotlib.pyplot as plt
from PIL import  Image


plt.ion()#开启会话,动态画图之前,调用该方法,则不用show了
for i in range(1,9):
    st  = str(i)+".jpg";
    img = Image.open(st)
    plt.clf();#清空
    plt.imshow(img)
    plt.pause(1)#休眠0.1秒
plt.ioff() #关闭会话
  1. 利用PIL画出验证码图像
from PIL import Image,ImageDraw,ImageFont,ImageFilter
import  random

#验证码生成
#随机字母
def randChar():
    return chr(random.randint(65,90));
def randChar2():
    return str(random.randint(0,9))
#随机颜色生成
#随机颜色1
def randColor1():
    return (random.randint(64,255),
            random.randint(64, 255),
            random.randint(64, 255))

#随机颜色2
def randColor2():
    return (random.randint(32,127),
            random.randint(32, 127),
            random.randint(32, 127))
for _ in range(600):
    #创建画板
    wid = 240;
    h=60;
    img = Image.new("RGB",(wid,h),(255,255,255));
    #创建字体对象
    font = ImageFont.truetype("1.ttf",50)
    #创建Draw对象
    draw = ImageDraw.Draw(img)
    for x in range(wid):
        for y in range(h):
            draw.point((x,y),fill=randColor1());
    nums="";
    for i  in range(4):
        num = randChar2();
        nums = nums+str(num)
        draw.text((10+60*i,10), num, fill=randColor2(), font=font)
    img.filter(ImageFilter.BLUR)
    img.save("pic/"+nums+".jpg");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值