欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用Python給二維碼圖片添加提示文字

 更新時(shí)間:2021年05月19日 11:43:56   作者:waketzheng  
今天教各位小伙伴怎么用Python給二維碼圖片添加提示文字,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴很有幫助,需要的朋友可以參考下

一、需求:

判斷當(dāng)前瀏覽器是否為微信,是的話挑起微信支付,不是的話,顯示二維碼圖片并提示用戶到微信中打開

二、效果圖:

三、代碼實(shí)現(xiàn):

1. 判斷是否微信

# toolbox.py
from typing import Any
 
 
class UserAgent:
    def __init__(self, user_agent: str = '', request: Any = None):
        if request is not None:
            try:
                user_agent = request.headers.get('user-agent', '')  # For Sanic
            except AttributeError:
                user_agent = request.META.get('HTTP_USER_AGENT', '')  # Django
        self.user_agent = user_agent
 
    @property
    def is_alipay(self) -> bool:
        return "AlipayClient/" in self.user_agent
 
    @property
    def is_wechat(self) -> bool:
        return "MicroMessenger/" in self.user_agent
 
    @property
    def is_qq(self) -> bool:
        return " QQ/" in self.user_agent
 
    @property
    def scan_type(self) -> str:
        if self.is_wechat or self.is_qq:
            return "wechat"
        if self.is_alipay:
            return "alipay"
        return "unknown"

2. 給圖片加文字 (參考了這篇文章并做了一些修改:http://www.dbjr.com.cn/article/175078.htm

# image_text.py
"""
給圖片(如二維碼)添上文字
Usage::
    >>> from xxx import deco_image
    >>> deco_image(image_path, text)  # 替換舊圖片
    >>> deco_image(image_path, text, new_path, color='red')  # 保留舊圖片并指定文字顏色
"""
from pathlib import Path
from typing import Optional, Tuple, Union
 
from PIL import Image, ImageDraw, ImageFont  # pip install pillow
 
TIP = "請(qǐng)用微信掃碼支付\n或分享到微信中打開"
 
 
# 獲取圖片寬度
def get_img_width(fname) -> int:
    return Image.open(fname).size[0]
 
 
# 獲取圖片高度
def get_img_height(fname) -> int:
    return Image.open(fname).size[1]
 
 
# 給圖片加文字
# 生成blank_img空白圖片,加上文字之后生成新圖片或覆蓋舊圖, 寬度為origin_img原始圖片的寬度
 
MARGIN_LEFT, MARGIN_TOP = 50, 15
FONT_SIZE = 22
FONT_COLOR = "red"
 
 
def gen_text_img(
    origin_img: Union[Path, str],
    text: str,
    img_path=None,
    color=FONT_COLOR,
    font_size: int = FONT_SIZE,
    margin_left: int = MARGIN_LEFT,
    margin_top: int = MARGIN_TOP,
    blank_img=None,
    font_path: Optional[str] = None,
    show_img: bool = False,
) -> Union[Path, str]:
    width = get_img_width(origin_img)
    if blank_img is None:
        blank_img = Path(f"/tmp/blank-{width}.png")
    elif isinstance(blank_img, str):
        blank_img = Path(blank_img)
    if not blank_img.exists():
        Image.new("RGB", (width, 70), (255, 255, 255)).save(blank_img)
    im = Image.open(blank_img)
    draw = ImageDraw.Draw(im)
    if font_path is None:
        # font_path = r"C:\Windows\Fonts\simsun.ttc"
        # font_path = "/System/Library/Fonts/Supplemental/Songti.ttc"
        font_path = "/usr/share/fonts/truetype/windows-font/Songti.ttc"
    fnt = ImageFont.truetype(font_path, font_size)
    draw.text((margin_left, margin_top), text, fill=color, font=fnt)
    if img_path is None:
        img_path = Path(origin_img)
        img_path = img_path.with_name(f"{img_path.stem}-{len(text)}{img_path.suffix}")
    im.save(img_path)
    if show_img:
        im.show()
    return img_path
 
 
# 拼接圖片,把上面生成的文字圖片拼接到原圖上面
# 生成一張寬度一致,高度為兩張圖片之和的空白長(zhǎng)圖
# 分別打開圖片進(jìn)行粘貼到空白長(zhǎng)圖里面
 
def join_imgs(text_img, origin_img, new_path=None) -> None:
    w = get_img_width(text_img)
    fh = get_img_height(text_img)
    oh = get_img_height(origin_img)
 
    blank_long_img = Image.new("RGBA", (w, fh + oh))  # 空白長(zhǎng)圖
 
    font_img = Image.open(text_img).resize((w, fh), Image.ANTIALIAS)
    blank_long_img.paste(font_img, (0, 0))
 
    img1 = Image.open(origin_img).resize((w, oh), Image.ANTIALIAS)
    blank_long_img.paste(img1, (0, fh))
    if new_path is None:
        new_path = origin_img
    blank_long_img.save(new_path)
    blank_long_img.show()
 
 
def deco_image(
    fpath: Union[Path, str],  # 圖片路徑
    text: str = TIP,  # 要添加的文字
    new_path: Union[Path, str, None] = None,  # 新圖片要保存的路徑(默認(rèn)覆蓋原圖)
    color: Union[str, Tuple[int, int, int]] = FONT_COLOR,  # 文字顏色
    font_size: int = FONT_SIZE,  # 文字高度
    margin_left: int = MARGIN_LEFT,
    margin_top: int = MARGIN_TOP,
) -> None:
    text_img = gen_text_img(
        fpath,
        text,
        color=color,
        font_size=font_size,
        margin_left=margin_left,
        margin_top=margin_top,
    )
    join_imgs(text_img, fpath)

3. 如果系統(tǒng)缺字體,那么需要去下載

sudo mkdir /usr/share/fonts/truetype/windows-font
sudo chmod 777 /usr/share/fonts/truetype/windows-font
cd /usr/share/fonts/truetype/windows-font
wget https://gitee.com/waketzheng/carstino/attach_files/703450/download/Songti.ttc  # 該文件比較大,有66.9MB

4. 調(diào)起支付或生成圖片

from pathlib import Path
from hashlib import md5
 
import qrcode  # pip install qrcode
from sanic import Blueprint
from sanic.log import logger
from sanic.request import Request
from sanic.response import json
 
from .models import Order
from .image_text import deco_image
from .toolbox import UserAgent
from .utils import async_http_post, get_host
from .consts import URL_PREFIX, WX_PAY_URL
 
 
bp = Blueprint("epay", url_prefix=URL_PREFIX)
 
async def get_qf_mch(community):
    pass
 
 
@bp.route("/pay-link", methods=["POST"])
async def pay_link(request: Request):
    requires, data = ["bills", "total", "next"], request.json
    logger.info(f"{request.url = } ;  {request.json = }")
    # 已經(jīng)1分鐘內(nèi)生成過對(duì)應(yīng)訂單的,直接去支付
    content = request.body + f"{datetime.now():%y%m%d%H%M%S}".encode()
    body = md5(content).hexdigest()
    if not (order := await Order.filter(body=body).first()):
        order = await new_pay_order(origin, data, request, body)
    mchid, mch_name = await get_qf_mch(order.community)
    if mchid:
        host = get_host(request.headers)
        if not UserAgent(request=request).is_wechat:
            # 故判斷當(dāng)前是否在微信里,如果不是就直接生成二維碼
            frontend_url = data["next"]
            fpath = "payit/" + md5(frontend_url.encode()).hexdigest() + ".png"
            if not (p := BASE_DIR / "media" / fpath).parent.exists():
                p.parent.mkdir(parents=True)
            qrcode.make(frontend_url).save(p)
            deco_image(p)
            img_url = host + URL_PREFIX + "/media/" + fpath
            return json({"payUrl": img_url})
        return json(qf_pay_it(mchid, mch_name, order, host=host))
    url = WX_PAY_URL
    if not (request_data := order.post_data).get("mch"):
        request_data.update(mch=1)  # 未配置支付的,先用1
    res = await async_http_post(url, request_data)
    try:
        res_json = res.json()
    except Exception as e:
        logger.error(f"{e = }; {url = }; {order.post_data=}; {res.content = }")
    return json(res_json)


到此這篇關(guān)于用Python給二維碼圖片添加提示文字的文章就介紹到這了,更多相關(guān)Python給二維碼添加文字內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python利用Matplotlib繪制柱狀圖(豎直柱狀圖和水平柱狀圖)、直方圖和餅狀圖

    Python利用Matplotlib繪制柱狀圖(豎直柱狀圖和水平柱狀圖)、直方圖和餅狀圖

    這篇文章主要給大家介紹了關(guān)于Python利用Matplotlib繪制柱狀圖(豎直柱狀圖和水平柱狀圖)、直方圖和餅狀圖的相關(guān)資料,Python使用matplotlib畫圖是非常方便的,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • Python:合并兩個(gè)numpy矩陣的實(shí)現(xiàn)

    Python:合并兩個(gè)numpy矩陣的實(shí)現(xiàn)

    今天小編就為大家分享一篇Python:合并兩個(gè)numpy矩陣的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 解決tf.keras.models.load_model加載模型報(bào)錯(cuò)問題

    解決tf.keras.models.load_model加載模型報(bào)錯(cuò)問題

    這篇文章主要介紹了解決tf.keras.models.load_model加載模型報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • django2 快速安裝指南分享

    django2 快速安裝指南分享

    下面小編就為大家分享一篇django2 快速安裝指南分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Python操作Word文件的流程步驟

    Python操作Word文件的流程步驟

    要操作Word文件,我們需要使用一個(gè)Python的第三方庫(kù)叫做?python-docx,它可以讓我們使用?Python對(duì)Word文件進(jìn)行讀取、修改以及創(chuàng)建等操作,文中有詳細(xì)的流程步驟介紹,需要的朋友可以參考下
    2023-06-06
  • int在python中的含義以及用法

    int在python中的含義以及用法

    在本篇文章中小編給大家整理了關(guān)于int在python中的含義以及用法,對(duì)此有興趣的朋友們可以跟著學(xué)習(xí)下。
    2019-06-06
  • python3?字符串str和bytes相互轉(zhuǎn)換

    python3?字符串str和bytes相互轉(zhuǎn)換

    這篇文章主要介紹了python3?字符串str和bytes相互轉(zhuǎn)換,在文件傳輸過程中,通常使用bytes格式的數(shù)據(jù)流,而代碼中通常用str類型,因此str和bytes的相互轉(zhuǎn)換就尤為重要,下文詳細(xì)介紹需要的小伙伴可以參考一下
    2022-03-03
  • python刪除列表內(nèi)容

    python刪除列表內(nèi)容

    這里給大家通過2個(gè)例子,分析了下如何使用python刪除列表內(nèi)容,也給出了思路,分別通過pop和remove方法來實(shí)現(xiàn),有相關(guān)需求的小伙伴可以參考下。
    2015-08-08
  • Python圖像處理之圖像的讀取、顯示與保存操作【測(cè)試可用】

    Python圖像處理之圖像的讀取、顯示與保存操作【測(cè)試可用】

    這篇文章主要介紹了Python圖像處理之圖像的讀取、顯示與保存操作,結(jié)合實(shí)例形式分析了Python使用PIL模塊、scipy和matplotlib模塊進(jìn)行圖像讀寫、顯示等相關(guān)操作技巧,需要的朋友可以參考下
    2019-01-01
  • Pandas中迭代DataFrame行的方法總結(jié)

    Pandas中迭代DataFrame行的方法總結(jié)

    Python是進(jìn)行數(shù)據(jù)分析的一種很好的語(yǔ)言,主要是因?yàn)橐詳?shù)據(jù)為中心的Python包的奇妙生態(tài)系統(tǒng),本文主要為大家介紹了如何在Pandas中迭代DataFrame中的行,有需要的可以參考下
    2023-09-09

最新評(píng)論