二维码
import qrcodefrom PIL import Imageimport os, sysdef 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, mathfrom PIL import Image,ImageDraw, ImageFont,ImageFilterFont_path='C:\windows\Fonts\arial.ttf'number=4size=(80,90)bgcolor=(255,255,255)Fontcolor=(0,0,255)linecolor=(255,0,0)draw_line=Trueline_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 socketimport threadingimport timedef 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 = 0while True: sock, addr = s.accept() user += 1 t = threading.Thread(target=onlink, args=(sock, addr, user))t.start()#客户端import sockets = 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()
总结 以上是内存溢出为你收集整理的python 11121211全部内容,希望文章能够帮你解决python 11121211所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)