1. 二维码
<span class="code-snippet_outer"><span style="font-size: 15px;"> pip install MyQR</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;">if __name__ == '__main__':</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> version, level, qr_name = myqr.run(</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> words='https://xxx.com/ccc',</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> version=1,</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> level='H',</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> picture='./XKD.gif',</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> colorized=True,</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> contrast=1.0,</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> brightness=1.0,</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> save_name='test4.gif',</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> save_dir=os.getcwd()</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> )</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;">print(version)</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;">print(level)</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> print(qr_name)</span></span>
-
数字 0 到 9 -
大小写的英文字母 -
常用英文标点符号和空格
3. qrcode
<span class="code-snippet_outer"><span style="font-size: 15px;"> pip install qrcode</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;">def mergeImg(img1, img2, flag=True):</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;">"""</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> 合并图像</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> :param img1:</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> :param img2:</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> :param flag: True 表示将logo居中, False 表示将logo作为背景图</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> :return:</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> """</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> img1_w, img1_h = img1.size</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;">if flag:</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> img2_w = int(img2.size[0] / 3)</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> img2_h = int(img2.size[1] / 3)</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"># 缩放logo</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> img2 = img2.resize(size=(img2_w, img2_h), resample=Image.ANTIALIAS)</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"># 将logo居中显示</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> img1.paste(im=img2, box=(int((img1_w - img2_w) / 2), int((img1_h - img2_h) / 2)))</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> img1.save('./test8.png')</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;">else:</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> img2 = img2.resize(size=(img1_w, img1_h), resample=Image.ANTIALIAS)</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"># 两张图片的size和mode要一致</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"># 计算公式为out = image1 * (1.0 - alpha) + image2 * alpha</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"># 这里的背景色就是一种干扰了, 如果将二维码的透明度设置过高会识别不出来</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> out_img = Image.blend(img1, img2, alpha=0.3)</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> out_img.save('./test9.png')</span></span>
<span class="code-snippet_outer"><br />
</span>
<span class="code-snippet_outer"><br />
</span>
<span class="code-snippet_outer"><span style="font-size: 15px;">if __name__ == '__main__':</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> qr = qrcode.QRCode(</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> version=1,</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> error_correction=qrcode.constants.ERROR_CORRECT_H,</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> box_size=10,</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> border=4,</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> )</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> qr.add_data('https://xxx.com/ccc')</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> qr.make(fit=True)</span></span>
<span class="code-snippet_outer"><br />
</span>
<span class="code-snippet_outer"><span style="font-size: 15px;"># black green purple orange pink white blue</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"># make_image() 返回的是一个PilImage对象</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"># 可以使用pillow对它进行图像处理相关的操作</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"># img1 = qr.make_image(fill_color="#8A2BE2", back_color="white")</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> img1 = qr.make_image(fill_color="orange", back_color="white").convert('RGB')</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"># img1.show()</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"># img1.save('./test6.png')</span></span>
<span class="code-snippet_outer"><br />
</span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> img2 = Image.open('./cutcamera.png').convert('RGB')</span></span>
<span class="code-snippet_outer"><span style="font-size: 15px;"> mergeImg(img1, img2, flag=True)</span></span>
<span class="code-snippet_outer"><br />
</span>
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 如何在python中表示一个空数组10/20
- ♥ Python列表排序(方法、实例)11/20
- ♥ 如何使用python random的随机播放?12/22
- ♥ python变量是对象吗?11/12
- ♥ python中numpy数据类型转换的方法09/29
- ♥ 怎么在手机上写python01/03
内容反馈