操作图片中的二维码
本帖最后由 zjg121 于 2024-4-4 22:58 编辑吾爱破解网址(https://www.52pojie.com)生成二维码的代码:
https://imgsrc.baidu.com/forum/pic/item/48540923dd54564e0a051402f5de9c82d1584fad.jpg
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")
https://static.52pojie.cn/static/image/hrline/4.gif
用四个小片段,使用时选择一个即可。https://imgsrc.baidu.com/forum/pic/item/f9dcd100baa1cd11ccc2861bff12c8fcc3ce2d91.jpg
# 识别二维码内容,转化为文本,如果是链接,打开链接
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(, 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
# 获取二维码的边界框坐标
x, y, width, height = qr_code.rect
# 将OpenCV的坐标系统(原点在左上角,y轴向下增长)用于填充操作
img = (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)
下图已经用白色填充:
http://imgsrc.baidu.com/forum/pic/item/fcfaaf51f3deb48f005854e5b61f3a292df578dc.jpg lizhibobo521 发表于 2024-4-8 09:00
大哥我是求助你写股票小指标那个人 能不能再帮我写个小指标 MA5/MA250 MA是均线
MA5:=MA(CLOSE,5);
MA250:=MA(CLOSE,250);
RATIO:MA5/MA250*100;
通达信副图指标 大哥我是求助你写股票小指标那个人 能不能再帮我写个小指标 MA5/MA250 MA是均线 学习了 谢谢 设置颜色有没有什么好的办法 学习一下,感谢分享 多谢楼主 正好最近在接触这块 太有用了 zjg121 发表于 2024-4-8 12:57
MA5:=MA(CLOSE,5);
MA250:=MA(CLOSE,250);
RATIO:MA5/MA250*100;
我太崇拜你了大哥 对我来说太有用了 学习学习,谢谢。 学习一下,感谢分享 感谢分享
页:
[1]
2