import qrcode
from PIL import Image
import os, sys
def gen_qrcode(string, path, logo=""):
"""
生成中间带logo的二维码
需要安装qrcode, PIL库
@参数 string: 二维码字符串
@参数 path: 生成的二维码保存路径
@参数 logo: logo文件路径
@return: None
"""
qr = qrcode.QRCode(
version=2,
error_correction=qrcode.constants.ERROR_CORRECT_H,
Box_size=8,
border=1
)
qr.add_data(string)
qr.make(fit=True)
img = qr.make_image()
img = img.convert("RGBA")
if logo and os.path.exists(logo):
try:
icon = Image.open(logo)
img_w, img_h = img.size
except Exception as e:
print(e)
sys.exit(1)
factor = 4
size_w = int(img_w / factor)
size_h = int(img_h / factor)
icon_w, icon_h = icon.size
if icon_w > size_w:
icon_w = size_w
if icon_h > size_h:
icon_h = size_h
icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
w = int((img_w - icon_w) / 2)
h = int((img_h - icon_h) / 2)
icon = icon.convert("RGBA")
img.paste(icon, (w, h), icon)
img.save(path)
# 调用系统命令打开图片
# xdg - open(opens a file or URL in the user's preferred application)
os.system('xdg-open %s' % path)
if __name__ == "__main__":
info = "http://www.fjut.edu.cn"
pic_path = "qr.png"
logo_path = "logo.png"
gen_qrcode(info, pic_path,logo_path )
验证码
import random, string, sys, math
from PIL import Image,ImageDraw, ImageFont,ImageFilter
font_path='C:\\Windows\\Fonts\\arial.ttf'
number=4
size=(80,90)
bgcolor=(255,255,255)
fontcolor=(0,0,255)
linecolor=(255,0,0)
draw_line=True
line_number=(1,5)
def gene_text():
source=list(string.ascii_letters)
for index in range(0,10):
source.append(str(index))
return "".join(random.sample(source,4))
def gene_line(draw,width,height):
begin=(random.randint(0,width),random.randint(0,height))
end=(random.randint(0,width),random.randint(0,height))
draw.line([begin,end],fill=linecolor)
def gene_code():
width,height=size
image=Image.new('RGBA',(width,height),bgcolor)
font=ImageFont.truetype(font_path,25)
draw=ImageDraw.Draw(image)
text=gene_text()
font_width, font_height=font.getsize(text)
draw.text(((width-font_width)/number, (height-font_height)/number),text,font=font,fill=fontcolor)
if draw_line:
gene_line(draw,width,height)
image=image.transform((width+20,height+10), Image.AFFINE,(1,-0.3,0,-0.1,1,0),Image.BILINEAR)
image=image.filter(ImageFilter.EDGE_ENHANCE_MORE)
image.save('1.png')
if __name__=="__main__":
gene_code()
聊天
#服务器
import socket
import threading
import time
def onLink(sock, addr, user):
user = 'user' + str(user)
print('来自' + str(addr) + ' as ' + user)
sock.send(b'welcome')
while True:
data = sock.recv(1024).decode('utf-8')
time.sleep(1)
if not data or data == 'exit':
break
print(user + ' : ' + data)
rtn = input('回复' + user + ' : ')
sock.send(rtn.encode('utf-8'))
sock.close()
print('close %s:%s' % addr)
print(user + ' 已断开')
s = socket.socket(socket.AF_INET, socket.soCK_STREAM)
s.bind(('127.0.0.1', 8888))
s.listen(5)
print('等待客户端连接.....')
user = 0
while True:
sock, addr = s.accept()
user += 1
t = threading.Thread(target=onLink, args=(sock, addr, user))
t.start()
#客户端
import socket
s = socket.socket(socket.AF_INET, socket.soCK_STREAM)
s.connect(('127.0.0.1', 8888))
data = s.recv(1024).decode('utf-8')
print('服务器:' + data)
while True:
data = input('请输入:')
if not data or data == 'exit':
break
s.send(data.encode('utf-8'))
print('服务器:' + s.recv(1024).decode('utf-8'))
print('与服务器断开连接')
s.close()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。