python之图像画图&动态验证码开发

图像绘画基础

导包

import PIL.Image as image  #图像
import PIL.ImageDraw as draw #画图
img1 = image.open(r"C:\Users\57616\Desktop\pic\1.jpg")
w,h = img1.size
print(w,h)  #宽,高

图像缩放

img2=img1.resize((100,100)) 
img2.show()

图像旋转

img3 =img2.rotate((45))
img3.show()

保存

img3.save(r"C:\Users\57616\Desktop\pic\3.jpg")

画点

img = draw.Draw(img1)
img.point((100,100),fill="red")
img1.show()

画矩形

img = draw.Draw(img1)
img.rectangle((30,30,100,100),fill="red")

划线

img.line((20,10,100,120),fill="blue")

写文字

img.text((50,50),text="abfd",fill="blue")
img.arc((30,50,100,200),100,200,fill="red")
img1.show()

颜色覆盖为灰色

img4 = img1.convert("L")
img4.show()

粘贴

img4.paste(img4,(20,20))
img1.show()

填充

imge = img1.filter(ifr.CONTOUR())
imge.show()

生成动态验证码

import PIL.Image as image
import PIL.ImageDraw as draw
import PIL.ImageFont as imgfont
import PIL.ImageFilter as ifr
import random #随机数
import numpy as np

font = imgfont.truetype("font.ttf",60)

w=240
h=120

def randchar():
    '''生成随机数'''
    return chr(random.randint(65,90)) #转化为字母
print(randchar())

def b_color():
    '''生成随机背景色'''
    return (random.randint(64,255),
            random.randint(64,255),
            random.randint(64,255))
    
def f_color():
    '''生成随机前景色'''
    return (random.randint(32,128),
            random.randint(32,128),
            random.randint(32,128))
    
def img():
    return image.new("RGB",(w,h),(255,255,255))

 
if __name__ == '__main__':
    img =img()
    image = draw.Draw(img)
    for x in range(w):
        for y in range(h):
            image.point((x,y),fill=b_color())
    
    for i in range(4):
        image.text((60*i+10,30),text=randchar(),fill = f_color(),font=font)
    #image.text(())
    img.show()
       

在图像画图

import cv2 #图像
img=cv2.imread(r"C:\Users\57616\Desktop\pic\1.jpg") #打开图像

color=(255,0,0)
cv2.line(img,(10,10),(100,100),color,3) #画线条 #图,坐标,大小,颜色,
cv2.rectangle(img,(50,50),(150,150),color) #化矩形
cv2.circle(img,(50,50),50,color) #画圆
cv2.imshow("image",img) #显示图片
cv2.waitKey(0) #等待键盘输入
cv2.destroyAllWindows() #关掉所有窗口
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值