本帖最后由 zjg121 于 2024-4-4 22:58 编辑
吾爱破解网址(https://www.52pojie.com)生成二维码的代码:
[Python] 纯文本查看 复制代码 import qrcode
# 要编码的数据
data = "https://www.52pojie.com"
# 创建一个QRCode对象
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=5,
)
# 添加数据
qr.add_data(data)
qr.make(fit=True)
# 创建图像并保存
img = qr.make_image(fill_color="black", back_color="white")
img.save("d:/a/qrcode.png")
用四个小片段,使用时选择一个即可。
[Python] 纯文本查看 复制代码
# 识别二维码内容,转化为文本,如果是链接,打开链接
from pyzbar.pyzbar import decode
from PIL import Image
from selenium import webdriver
from time import sleep
# 打开图像文件
img = Image.open('d:/a/1.png')
# 将图像转换为灰度
img = img.convert('L')
# 识别图像中的二维码
barcodes = decode(img)
# 输出识别结果
for barcode in barcodes:
barcodeData = barcode.data.decode("utf-8")
print(barcodeData)
try:
driver = webdriver.Edge()
driver.get(barcodeData)
sleep(10)
except:
pass
finally:
if driver:
driver.close()
driver.quit()
-----------------------------------------------------------------------------------------------------------------------
# 识别二维码,写出二维码的位置和大小
import cv2
from pyzbar.pyzbar import decode
def locate_qr_codes(image_path):
# 使用OpenCV读取图片并转换为灰度图
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# 使用pyzbar解码图像中的二维码
decoded = decode(img)
locations_and_sizes = []
for qr in decoded:
# 获取二维码的边界框坐标
x, y, width, height = qr.rect
# 将OpenCV的坐标系统(原点在左上角,y轴向下增长)转换为PIL坐标系统(原点在左上角,y轴向上增长)
top_left = (x, y)
bottom_right = (x + width, y + height)
# 存储位置(左上角和右下角坐标)和大小(宽度、高度)
location_and_size = {
'top_left': top_left,
'bottom_right': bottom_right,
'width': width,
'height': height,
}
locations_and_sizes.append(location_and_size)
return locations_and_sizes
image_path = 'd:/a/a.jpg' # 替换为你要检测的图片文件路径
qr_positions_and_sizes = locate_qr_codes(image_path)
for i, qr_info in enumerate(qr_positions_and_sizes, start=1):
print(f"QR Code {i}:")
print(f" Top-left corner: {qr_info['top_left']}")
print(f" Bottom-right corner: {qr_info['bottom_right']}")
print(f" Width: {qr_info['width']} pixels")
print(f" Height: {qr_info['height']} pixels")
-----------------------------------------------------------------------------------------------------------------------
# 用方框圈住二维码
import cv2
from pyzbar.pyzbar import decode
from PIL import Image, ImageDraw
def locate_and_draw_qr_codes(image_path, output_path):
# 使用OpenCV读取图片并转换为灰度图
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# 使用pyzbar解码图像中的二维码
decoded = decode(img)
pil_img = Image.open(image_path)
draw = ImageDraw.Draw(pil_img)
for qr in decoded:
# 获取二维码的边界框坐标
x, y, width, height = qr.rect
# 将OpenCV的坐标系统(原点在左上角,y轴向下增长)转换为PIL坐标系统(原点在左上角,y轴向上增长)
top_left = (x, y)
bottom_right = (x + width, y + height)
# 使用ImageDraw在原图上绘制红色方框
draw.rectangle([top_left, bottom_right], outline='red', width=2)
# 保存处理后的图片
pil_img.save(output_path)
input_image_path = 'd:/a/a.jpg'
output_image_path = 'd:/a/a1.jpg'
locate_and_draw_qr_codes(input_image_path, output_image_path)
-----------------------------------------------------------------------------------------------------------------------
# 确定图片中二维码的位置和大小,用白色填充二维码。
import cv2
from pyzbar.pyzbar import decode
def locate_and_fill_qr_code_with_white(input_image_path, output_image_path):
# 读取原始图像
img = cv2.imread(input_image_path)
# 确保图像读取成功
if img is None:
raise ValueError(f"Failed to read image from {input_image_path}")
# 使用pyzbar解码图像中的二维码
decoded = decode(img)
if decoded:
# 取第一个识别到的二维码
qr_code = decoded[0]
# 获取二维码的边界框坐标
x, y, width, height = qr_code.rect
# 将OpenCV的坐标系统(原点在左上角,y轴向下增长)用于填充操作
img[y:y + height, x:x + width] = (255, 255, 255) # 设置为白色
# 保存处理后的图像
cv2.imwrite(output_image_path, img)
input_image_path = 'd:/a/a.jpg'
output_image_path = 'd:/a/path_to_output_image_with_white_filled_qr_code.jpg'
locate_and_fill_qr_code_with_white(input_image_path, output_image_path)
下图已经用白色填充:
|