import sys
import time
import random
import pymysql
import threading
import VerificationCode
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
filename = str
database = pymysql.connect(host="182.254.179.35", user="root", password="guxinyi", database="mysql")
cursor = database.cursor()
def Rotate(image, angle, position):
transform = QTransform()
transform.rotate(angle)
image = image.transformed(transform)
if position == 'left':
image = image.copy((image.width() - ui.labelLeft.width()) / 2, (image.height() - ui.labelLeft.height()) / 2,
ui.labelLeft.width(), ui.labelLeft.height())
image.scaled(ui.labelLeft.width(), ui.labelLeft.height())
else:
image = image.copy((image.width() - ui.labelRight.width()) / 2, (image.height() - ui.labelRight.height()) / 2,
ui.labelRight.width(), ui.labelRight.height())
image.scaled(ui.labelRight.width(), ui.labelRight.height())
return image
def Caculate(image):
image = image.scaled(10, 10)
image = image.toImage()
grayValues = []
for i in range(0, 10):
for j in range(0, 10):
color = QColor(image.pixel(i, j))
grayValues.append((color.red() * 30 + color.green() * 59 + color.blue() * 11) / 100)
sum = 0
for i in range(0, 100):
sum = sum + grayValues[i]
averageValue = sum / len(grayValues)
result = ""
for i in range(0, 100):
if grayValues[i] < averageValue:
result = result + '0'
else:
result = result + '1'
return result
def Correct(angle):
for i in range(0, 360 - angle):
ui.horizontalSliderRight.setValue(i)
time.sleep(0.01)
def pushButtonAddClick():
image = ui.labelLeft.pixmap()
for i in range(0, 360):
tempImage = Rotate(image, i, "left")
code = Caculate(tempImage)
sql = "insert into verificationcode values(\'" + code + "\'," + str(i) + ")"
cursor.execute(sql)
database.commit()
print("success")
def pushButtonSelectClick():
global filename
filename, filentype = QFileDialog.getOpenFileName(None, "Select Verification Picture", "",
"*.png;;*.jpg;;All Files(*)")
if filename != "":
ui.labelLeft.setPixmap(QPixmap(filename).scaled(ui.labelLeft.width(), ui.labelLeft.height()))
def pushButtonCreatetClick():
ui.labelRight.setPixmap(ui.labelLeft.pixmap())
ui.horizontalSliderRight.setValue(0)
def pushButtonDecodeClick():
image = ui.labelRight.pixmap()
code = Caculate(image)
sql = "select * from verificationcode where code=\'" + code + "\'"
cursor.execute(sql)
result = cursor.fetchall()
angle = -1
for row in result:
angle = row[1]
if angle != -1:
ui.textEdit.append("The verification code picture has been rotate " + str(
angle) + ",it still needs to be rotated " + str(
360 - angle) + " degrees to be corrected\n")
result = QMessageBox.information(None, "Information", "The verification code picture has been rotate " + str(
angle) + ",it still needs to be rotated " + str(
360 - angle) + " degrees to be corrected,do you need it to be rotated automatically", QMessageBox.Ok,
QMessageBox.Cancel)
if result == QMessageBox.Ok:
ui.textEdit.append("Start to rotate automatically\n")
thread = threading.Thread(target=Correct, args=(angle,))
thread.start()
else:
ui.textEdit.append("Cancel auto rotation\n")
else:
ui.textEdit.append("There is no match\n")
QMessageBox.warning(None, "warning", "There is no match")
def pushButtonCheckClick():
if abs(360 - ui.horizontalSliderLeft.value() - ui.horizontalSliderRight.value()) <= 10:
ui.textEdit.append("Authentication is successful\n")
QMessageBox.information(None, "Correct", "Correct!")
else:
ui.textEdit.append("Validation fails\n")
QMessageBox.warning(None, "Wrong", "Wrong!")
def pushButtonRandomClick():
ui.horizontalSliderLeft.setValue(random.randint(0, 360))
def horizontalSliderLeftValueChanged():
image = Rotate(QPixmap(filename), ui.horizontalSliderLeft.value(), "left")
ui.labelLeft.setPixmap(image)
def horizontalSliderRightValueChanged():
image = Rotate(ui.labelLeft.pixmap(), ui.horizontalSliderRight.value(), "right")
ui.labelRight.setPixmap(image)
def horizontalSliderRightsliderReleased():
ui.textEdit.append("The rotation Angle is " + str(ui.horizontalSliderRight.value()) + "\n")
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = VerificationCode.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
ui.pushButtonAdd.clicked.connect(pushButtonAddClick)
ui.pushButtonSelect.clicked.connect(pushButtonSelectClick)
ui.pushButtonCreate.clicked.connect(pushButtonCreatetClick)
ui.pushButtonDecode.clicked.connect(pushButtonDecodeClick)
ui.pushButtonRandom.clicked.connect(pushButtonRandomClick)
ui.pushButtonCheck.clicked.connect(pushButtonCheckClick)
ui.horizontalSliderLeft.valueChanged.connect(horizontalSliderLeftValueChanged)
ui.horizontalSliderRight.valueChanged.connect(horizontalSliderRightValueChanged)
ui.horizontalSliderRight.sliderReleased.connect(horizontalSliderRightsliderReleased)
sys.exit(app.exec_())
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
旋转验证码生成与识别(Python版).rar (14个子文件)
旋转验证码生成与识别(Python版)
VerificationCode.ui 4KB
VerificationCode.py 6KB
.idea
.gitignore 50B
misc.xml 204B
workspace.xml 3KB
inspectionProfiles
profiles_settings.xml 174B
modules.xml 349B
旋转验证码生成与识别(Python版).iml 291B
__pycache__
VerificationCode.cpython-36.pyc 3KB
VerificationCode.cpython-38.pyc 3KB
验证码2(正确).png 154KB
验证码1(正确).png 178KB
main.py 6KB
验证码3(正确).png 120KB
共 14 条
- 1
不会敲代码的小鱼
- 粉丝: 6
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页