1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| import qrcode from PIL import Image from matplotlib import pyplot as plt
whiteRGBA = (255,255,255,255) blackRGBA = (0,0,0,255)
class GenerateQRCode(object): def __init__(self, background_map, version=5, error_correction=qrcode.ERROR_CORRECT_H, box_size=8, border=2): self.version = version self.error_correction = error_correction self.box_size = box_size self.border = border self.background_map = background_map def __transparent_back(self,image): """白色背景透明设置""" img = image.convert('RGBA') W, H = img.size color_0 = whiteRGBA for h in range(H): for w in range(W): dot = (w,h) color_1 = img.getpixel(dot) if color_1 == color_0: color_1 = color_1[:-1] + (0,) img.putpixel(dot,color_1) return img def __position(self,coordinate): """计算二维码黑点白点位置""" Tlist = list() Flist = list() rnum = int(self.box_size/(self.box_size/2)) clen = len(coordinate) numy = 0 for y in coordinate: if self.border - 1 < numy < self.box_size + self.border: numx = 0 for x in y: if self.box_size + self.border <= numx < clen - (self.box_size + self.border): if x == True: wx = int(numx*self.box_size) + rnum hy = int(numy*self.box_size) + rnum Tlist.append((wx,hy)) elif x == False: wx = int(numx*self.box_size) + rnum hy = int(numy*self.box_size) + rnum Flist.append((wx,hy)) numx += 1 elif self.box_size + self.border <= numy < clen - (self.box_size + self.border): numx = 0 for x in y: if self.border - 1 < numx < clen - self.border: if x == True: wx = int(numx*self.box_size) + rnum hy = int(numy*self.box_size) + rnum Tlist.append((wx,hy)) else: wx = int(numx*self.box_size) + rnum hy = int(numy*self.box_size) + rnum Flist.append((wx,hy)) numx += 1 elif clen - (self.box_size + self.border) <= numy < clen - self.border: numx = 0 for x in y: if self.box_size + self.border <= numx < clen - self.border : if x == True: wx = int(numx*self.box_size) + rnum hy = int(numy*self.box_size) + rnum Tlist.append((wx,hy)) elif x == False: wx = int(numx*self.box_size) + rnum hy = int(numy*self.box_size) + rnum Flist.append((wx,hy)) numx += 1 numy += 1 return Tlist,Flist def Qrcode(self, txt): """生成二维码""" qr = qrcode.QRCode( version = self.version, error_correction = self.error_correction, box_size = self.box_size, border = self.border, ) qr.add_data(txt) qr.make(fit=True) coordinate = qr.get_matrix() qrimg = qr.make_image() qrimg = self.__transparent_back(qrimg) w, h = qrimg.size b_map = Image.open(self.background_map) b_map_image = b_map.resize((w, h), Image.ANTIALIAS) (Tlist, Flist) = self.__position(coordinate) ''' 1、把需要缩小的黑色块变成白色 2、画出黑色的小块 3、把图片白色透明 4、二维码图和背景图合并 5、画出白色的小方块 '''
rnum = int(self.box_size/(self.box_size/2)) for n in Tlist: for m in range(self.box_size + 1): for m1 in range(self.box_size + 1): qrimg.putpixel((n[0]-rnum + m1, n[1]-rnum + m),(255,255,255,255)) for p in range(rnum + 2): for p1 in range(rnum + 2): qrimg.putpixel((n[0] + p1, n[1]+p),(0,0,0,255)) image = self.__transparent_back(qrimg) for n1 in Flist: for p in range(rnum + 2): for p1 in range(rnum + 2): image.putpixel((n1[0] + p1, n1[1]+p),(255,255,255,255)) b_map_image.paste(image, (0, 0), image) plt.imshow(b_map_image) plt.show()
if __name__ == "__main__": gqrc = GenerateQRCode(background_map='background.jpg') gqrc.Qrcode("http://wwww.baidu.com")
|