关于塔罗牌摆放的小程序------python入门案例
前言
- 本文主要分享基于python开发的小程序,用于实现随机抽取塔罗牌并按特定需求摆放位置进行展示,之后从数据库中获取每张的解释与分类推理含义。
- 通过本文你将收获基础的python语法规则、常用包**(opencv2、pIL、numpy、pymysql)**的调用、本地文件的操作、简单一个程序流程。作为一个简化的入门学习案例,可分解进行学习掌握。本文涉及的sql数据库构建、调用会在后续的博文中更新。
程序开发原因(待更新)
程序流程

结果展示
- 说明:由于商业保密(。。。),数据库调用过程的结果只有相关代码,不进行展示



函数从零讲解
- 说明: 完整代码在下文中展示。需要直接运行尝试的可以下个相关上传的文件/百度云盘/github。本节是分步讲解(慢慢更新)。运行环境python3.9、pycharm 2020.4
- 导入使用的包
- 图像相关包 2数学包 3本地文件操作 4数据库包
import cv2
import numpy as np
import PIL.Image as img
import os,sys
import pymysql
a1_1_name = 'name'
a1_1_sex = 1
a1_1_type = '工作'
a1_1_type_1 = 'a'
a1_1_num = 3
a1_1_weixie = '12345678'
a1_1_Divination_content = '我想知道我能当上逗逼吗?'
a14_2 = ["bake.jpg"]
name ='最终测试'
danhao = 1
mima ='1602480875'
file_photo_dr = 'F:/软件安装包/项目文件/不更新/占卜/简化版/tupian/'
if __name__ == '__main__':
pan_duan_sex_data(a1_1_sex,a1_1_num)
[a11_1,b11_1]=rand_num(a1_1_num)
- 随机生成一个2*3的随机矩阵,第一行为(1:156)塔罗牌数目,第2行为0或1的随机数(逆位或正位)。
def rand_num(num_card):
num_card=int(num_card)
cx=np.zeros(int(num_card))
cx_1=np.zeros(int(num_card))
num_card_1=int(num_card)
for i in range(num_card_1):
cx[int(i)]=np.random.randint(1,156)
while cx[i] in cx[1:i-1] and cx[i] in cx[1:i-1]-78:
cx[int(i)] = np.random.randint(1, 156)
if cx[int(i)]>78:
cx_1[int(i)]=1
else:
cx_1[int(i)] =0
print('产生的卡片序号分别是卡片=%s'%\
cx)
return cx_1,cx
- 读取本地卡图地址(数据库中调用、本地文件读取)
- 本地读取
ptos_flies, root_name = file_name(file_photo_dr)
num_x = ptos_flies.__len__()
ax_name = list(range(num_x))
ptos_flies.sort();
for j in range(num_x-1):
ax_name[j] = root_name + ptos_flies[j]
if (i<=78):
a12_1=ax_name[int(i)]
else:
a12_1 = ax_name[int(i/2)]
def file_name(file_dir):
for root,dirs,files in os.walk(file_dir):
return files, root
def data_Write_1(q_1, type_1, type_a, mode, mima):
db= pymysql.connect(host='localhost', user='root', password=mima, database='牌库数据')
cursor = db.cursor()
if mode == 3:
cc_1 = cursor.execute("INSERT INTO `牌库数据`.`数据原始问题集合`(`问题`, `分类`, `分类标签`) VALUES ('%s', '%s', '%s') " % \
(q_1,type_1,type_a))
if cc_1 == 1:
print("成功保存问题数据 %s条" % \
cc_1)
db.commit()
db.close()
def pan_duan_sex_data(a1_1_sex,a1_1_num):
data_kongzhi_1=[1,2]
data_kongzhi_2=[3,5,8]
if a1_1_sex in data_kongzhi_1 :
print("输入性别数据数据无错误")
else:
print("输入的性别数据存在问题,已经停止运行")
sys.exit(0)
if a1_1_num in data_kongzhi_2 :
print("输入牌阵数据数据无错误")
else:
print("输入的牌阵数据存在问题,已经停止运行")
sys.exit(0)
def pan_none(type,gg,a1_1_type):
mode=1
if type == None:
print('--------------------------------------------------')
print('是否需要补充此记录的数据')
print('--------------------------------------------------')
ax_1 = input("是否补充?1补充,2不补充=====")
if int(ax_1) == 1:
print('补充')
data_Write(gg,a1_1_type,mode)
else:
print('不补充')
def data_Write(gg,a1_1_type,mode,mima):
db= pymysql.connect(host='localhost',
user='root',
password=mima,
database='牌库数据')
cursor = db.cursor()
if mode==1:
data_1 = input("请输入补充数据==")
cc_1 = cursor.execute('UPDATE 牌分类牌意 SET %s = "%s" where 牌序号= %s' % \
(a1_1_type, data_1, gg))
print("修改的数据条数= %s" % \
cc_1)
db.commit()
if mode==2:
jj=cursor.execute('select * from 牌分类牌意 where 牌序号= %s;'%\
gg )
print("分类牌查询到的解释数据条数= %s"%\
jj)
db.commit()
results = cursor.fetchall()
try:
for row in results:
id= row[0]
type1=row[1]
type2 = row[2]
type3=row[3]
type4 = row[4]
ss_1 = ["学习"]
ss_2 = ["工作"]
ss_3 = ["爱情"]
ss_4 = ["未来发展"]
if a1_1_type in ss_1:
print("学习类别下的解释:——————%s" % \
type1)
pan_none(type1,gg,a1_1_type,)
if a1_1_type in ss_2:
print("工作类别下的解释:——————%s" % \
type2)
pan_none(type2,gg,a1_1_type)
if a1_1_type in ss_3:
print("爱情类别下的解释:——————%s" % \
type3)
pan_none(type3,gg,a1_1_type)
if a1_1_type in ss_4:
print("未来发展类别下的解释:——————%s" % \
type4)
pan_none(type4,gg,a1_1_type)
return id, type1, type2, type3, type4
except:
print("牌分类读取存在问题")
db.close()
def crade_buzhi(cc_no_1,cc_no_2,cc_no_3,cc_no_4,name):
if len(cc_no_1) != len(cc_no_2):
print("输入的数据数目有误")
os.system("pause")
if cc_no_3 == 3:
print("执行的是3张牌阵")
for i in range(3):
if i == 0:
img2 = img.open(cc_no_4)
img_2 = img2.resize((684, 589),img.ANTIALIAS)
img1 = img.open(cc_no_1[i])
[rows, cols]= img1.size
img_1 = img1.resize( (int(rows / 2), int(cols / 2)))
if cc_no_2[i] == 1:
img_1.transpose(img.ROTATE_180)
img_1_1 = img_1
img_2_1 = img_2
cc_no = i + 1
if cc_no == 1:
a1_where = (100, 200)
img_2_1.paste(img_1_1, a1_where)
elif cc_no == 2:
a2_where = (260, 200)
img_2_1.paste(img_1_1, a2_where)
elif cc_no == 3:
a3_where = (420, 200)
img_2_1.paste(img_1_1, a3_where)
img_2_1.save("ceshi2.jpg")
if cc_no_3==5:
print("执行的是5张牌阵")
for i in range(5):
if i==0:
img2 = cv2.imread(cc_no_4[i])
img_2 = cv2.resize(img2, (601, 733))
cv2.imwrite("ceshi2.jpg", img_2)
img1 = cv2.imread(cc_no_1[i])
rows, cols = img1.shape[0:2]
img_1 = cv2.resize(img1, (int(cols / 2), int(rows / 2)))
if cc_no_2[i]==1:
img_1 = cv2.flip(img_1, -1)
cv2.imwrite("ceshi.jpg",img_1)
img_1_1=img.open('ceshi.jpg')
img_2_1 = img.open('ceshi2.jpg')
cc_no=i+1
if cc_no==3:
a1_where=(120,50)
img_2_1.paste(img_1_1,a1_where)
elif cc_no == 5:
a2_where = (371, 50)
img_2_1.paste(img_1_1,a2_where)
elif cc_no==2:
a3_where=(177,280)
img_2_1.paste(img_1_1,a3_where)
elif cc_no==4:
a4_where=(330,280)
img_2_1.paste(img_1_1,a4_where)
elif cc_no==1:
a5_where=(248,506)
img_2_1.paste(img_1_1,a5_where)
img_2_1.save("ceshi2.jpg")
if cc_no_3==8:
print("执行的是8张牌阵")
for i in range(8):
if i==0:
img2 = cv2.imread(cc_no_4[i])
img_2 = cv2.resize(img2, (638, 878))
img1 = cv2.imread(cc_no_1[i])
rows, cols = img1.shape[0:2]
img_1 = cv2.resize(img1, (int(cols / 2), int(rows / 2)))
if cc_no_2[i]==1:
img_1 = cv2.flip(img_1, -1)
cv2.imwrite("ceshi.jpg",img_1)
img_1_1=img.open('ceshi.jpg')
img_2_1 = img.open('ceshi2.jpg')
cc_no=i+1
if cc_no==1:
a1_where=(160,140)
img_2_1.paste(img_1_1,a1_where)
elif cc_no==2:
a2_where=(400,140)
img_2_1.paste(img_1_1,a2_where)
elif cc_no==3:
a3_where=(279,20)
img_2_1.paste(img_1_1,a3_where)
elif cc_no==4:
a4_where=(279,234)
img_2_1.paste(img_1_1,a4_where)
elif cc_no==5:
a5_where=(160,448)
img_2_1.paste(img_1_1,a5_where)
elif cc_no == 6:
a3_where = (400, 448)
img_2_1.paste(img_1_1, a3_where)
elif cc_no == 7:
a4_where = (279, 448)
img_2_1.paste(img_1_1, a4_where)
elif cc_no == 8:
a5_where = (279, 665)
img_2_1.paste(img_1_1, a5_where)
img_2_1.save("ceshi2.jpg")
img._show(img_2_1)
img_2_1.save("输出结果结果%s.jpg"% \
name)
def data_Write(gg,a1_1_type,mode,mima):
db= pymysql.connect(host='localhost',
user='root',
password=mima,
database='牌库数据')
cursor = db.cursor()
if mode==1:
data_1 = input("请输入补充数据==")
cc_1 = cursor.execute('UPDATE 牌分类牌意 SET %s = "%s" where 牌序号= %s' % \
(a1_1_type, data_1, gg))
print("修改的数据条数= %s" % \
cc_1)
db.commit()
if mode==2:
jj=cursor.execute('select * from 牌分类牌意 where 牌序号= %s;'%\
gg )
print("分类牌查询到的解释数据条数= %s"%\
jj)
db.commit()
results = cursor.fetchall()
try:
for row in results:
id= row[0]
type1=row[1]
type2 = row[2]
type3=row[3]
type4 = row[4]
ss_1 = ["学习"]
ss_2 = ["工作"]
ss_3 = ["爱情"]
ss_4 = ["未来发展"]
if a1_1_type in ss_1:
print("学习类别下的解释:——————%s" % \
type1)
pan_none(type1,gg,a1_1_type,)
if a1_1_type in ss_2:
print("工作类别下的解释:——————%s" % \
type2)
pan_none(type2,gg,a1_1_type)
if a1_1_type in ss_3:
print("爱情类别下的解释:——————%s" % \
type3)
pan_none(type3,gg,a1_1_type)
if a1_1_type in ss_4:
print("未来发展类别下的解释:——————%s" % \
type4)
pan_none(type4,gg,a1_1_type)
return id, type1, type2, type3, type4
except:
print("牌分类读取存在问题")
db.close()
完整函数
import cv2
import numpy as np
import PIL.Image as img
import os,sys
import pymysql
a1_1_name = 'name'
a1_1_sex = 1
a1_1_type = '工作'
a1_1_type_1 = 'a'
a1_1_num = 3
a1_1_weixie = '12345678'
a1_1_Divination_content = '我想知道我能当上逗逼王吗?'
a14_2 = ["bake.jpg"]
name ='最终测试'
danhao = 1
mima ='1602480875'
file_photo_dr = 'F:/软件安装包/项目文件/不更新/占卜/简化版/tupian/'
def data_Write_1(q_1, type_1, type_a, mode, mima):
db= pymysql.connect(host='localhost', user='root', password=mima, database='牌库数据')
cursor = db.cursor()
if mode == 3:
cc_1 = cursor.execute("INSERT INTO `牌库数据`.`数据原始问题集合`(`问题`, `分类`, `分类标签`) VALUES ('%s', '%s', '%s') " % \
(q_1,type_1,type_a))
if cc_1 == 1:
print("成功保存问题数据 %s条" % \
cc_1)
db.commit()
db.close()
def pan_duan_sex_data(a1_1_sex,a1_1_num):
data_kongzhi_1=[1,2]
data_kongzhi_2=[3,5,8]
if a1_1_sex in data_kongzhi_1 :
print("输入性别数据数据无错误")
else:
print("输入的性别数据存在问题,已经停止运行")
sys.exit(0)
if a1_1_num in data_kongzhi_2 :
print("输入牌阵数据数据无错误")
else:
print("输入的牌阵数据存在问题,已经停止运行")
sys.exit(0)
def pan_none(type,gg,a1_1_type):
mode=1
if type == None:
print('--------------------------------------------------')
print('是否需要补充此记录的数据')
print('--------------------------------------------------')
ax_1 = input("是否补充?1补充,2不补充=====")
if int(ax_1) == 1:
print('补充')
data_Write(gg,a1_1_type,mode)
else:
print('不补充')
def data_Write(gg,a1_1_type,mode,mima):
db= pymysql.connect(host='localhost',
user='root',
password=mima,
database='牌库数据')
cursor = db.cursor()
if mode==1:
data_1 = input("请输入补充数据==")
cc_1 = cursor.execute('UPDATE 牌分类牌意 SET %s = "%s" where 牌序号= %s' % \
(a1_1_type, data_1, gg))
print("修改的数据条数= %s" % \
cc_1)
db.commit()
if mode==2:
jj=cursor.execute('select * from 牌分类牌意 where 牌序号= %s;'%\
gg )
print("分类牌查询到的解释数据条数= %s"%\
jj)
db.commit()
results = cursor.fetchall()
try:
for row in results:
id= row[0]
type1=row[1]
type2 = row[2]
type3=row[3]
type4 = row[4]
ss_1 = ["学习"]
ss_2 = ["工作"]
ss_3 = ["爱情"]
ss_4 = ["未来发展"]
if a1_1_type in ss_1:
print("学习类别下的解释:——————%s" % \
type1)
pan_none(type1,gg,a1_1_type,)
if a1_1_type in ss_2:
print("工作类别下的解释:——————%s" % \
type2)
pan_none(type2,gg,a1_1_type)
if a1_1_type in ss_3:
print("爱情类别下的解释:——————%s" % \
type3)
pan_none(type3,gg,a1_1_type)
if a1_1_type in ss_4:
print("未来发展类别下的解释:——————%s" % \
type4)
pan_none(type4,gg,a1_1_type)
return id, type1, type2, type3, type4
except:
print("牌分类读取存在问题")
db.close()
def rand_num(num_card):
num_card=int(num_card)
cx=np.zeros(int(num_card))
cx_1=np.zeros(int(num_card))
num_card_1=int(num_card)
for i in range(num_card_1):
cx[int(i)]=np.random.randint(1,156)
while cx[i] in cx[1:i-1] and cx[i] in cx[1:i-1]-78:
cx[int(i)] = np.random.randint(1, 156)
if cx[int(i)]>78:
cx_1[int(i)]=1
else:
cx_1[int(i)] =0
print('产生的卡片序号分别是卡片=%s'%\
cx)
return cx_1,cx
def data_fetch(gg,mima):
db= pymysql.connect(host='localhost',
user='root',
password=mima,
database='牌库数据')
cursor = db.cursor()
jj=cursor.execute('select * from 牌直意库 where 牌序= %s;'%\
gg)
db.commit()
results = cursor.fetchall()
try:
for row in results:
id= row[0]
card_num= row[1]
name = row[2]
crad_way=row[3]
mean_crad = row[4]
return crad_way, mean_crad, name, id, card_num
except:
print("读取存在问题")
db.close()
def crade_buzhi(cc_no_1,cc_no_2,cc_no_3,cc_no_4,name):
if len(cc_no_1) != len(cc_no_2):
print("输入的数据数目有误")
os.system("pause")
if cc_no_3 == 3:
print("执行的是3张牌阵")
for i in range(3):
if i == 0:
img2 = img.open(cc_no_4)
img_2 = img2.resize((684, 589),img.ANTIALIAS)
img1 = img.open(cc_no_1[i])
[rows, cols]= img1.size
img_1 = img1.resize( (int(rows / 2), int(cols / 2)))
if cc_no_2[i] == 1:
img_1.transpose(img.ROTATE_180)
img_1_1 = img_1
img_2_1 = img_2
cc_no = i + 1
if cc_no == 1:
a1_where = (100, 200)
img_2_1.paste(img_1_1, a1_where)
elif cc_no == 2:
a2_where = (260, 200)
img_2_1.paste(img_1_1, a2_where)
elif cc_no == 3:
a3_where = (420, 200)
img_2_1.paste(img_1_1, a3_where)
img_2_1.save("ceshi2.jpg")
if cc_no_3==5:
print("执行的是5张牌阵")
for i in range(5):
if i==0:
img2 = cv2.imread(cc_no_4[i])
img_2 = cv2.resize(img2, (601, 733))
cv2.imwrite("ceshi2.jpg", img_2)
img1 = cv2.imread(cc_no_1[i])
rows, cols = img1.shape[0:2]
img_1 = cv2.resize(img1, (int(cols / 2), int(rows / 2)))
if cc_no_2[i]==1:
img_1 = cv2.flip(img_1, -1)
cv2.imwrite("ceshi.jpg",img_1)
img_1_1=img.open('ceshi.jpg')
img_2_1 = img.open('ceshi2.jpg')
cc_no=i+1
if cc_no==3:
a1_where=(120,50)
img_2_1.paste(img_1_1,a1_where)
elif cc_no == 5:
a2_where = (371, 50)
img_2_1.paste(img_1_1,a2_where)
elif cc_no==2:
a3_where=(177,280)
img_2_1.paste(img_1_1,a3_where)
elif cc_no==4:
a4_where=(330,280)
img_2_1.paste(img_1_1,a4_where)
elif cc_no==1:
a5_where=(248,506)
img_2_1.paste(img_1_1,a5_where)
img_2_1.save("ceshi2.jpg")
if cc_no_3==8:
print("执行的是8张牌阵")
for i in range(8):
if i==0:
img2 = cv2.imread(cc_no_4[i])
img_2 = cv2.resize(img2, (638, 878))
img1 = cv2.imread(cc_no_1[i])
rows, cols = img1.shape[0:2]
img_1 = cv2.resize(img1, (int(cols / 2), int(rows / 2)))
if cc_no_2[i]==1:
img_1 = cv2.flip(img_1, -1)
cv2.imwrite("ceshi.jpg",img_1)
img_1_1=img.open('ceshi.jpg')
img_2_1 = img.open('ceshi2.jpg')
cc_no=i+1
if cc_no==1:
a1_where=(160,140)
img_2_1.paste(img_1_1,a1_where)
elif cc_no==2:
a2_where=(400,140)
img_2_1.paste(img_1_1,a2_where)
elif cc_no==3:
a3_where=(279,20)
img_2_1.paste(img_1_1,a3_where)
elif cc_no==4:
a4_where=(279,234)
img_2_1.paste(img_1_1,a4_where)
elif cc_no==5:
a5_where=(160,448)
img_2_1.paste(img_1_1,a5_where)
elif cc_no == 6:
a3_where = (400, 448)
img_2_1.paste(img_1_1, a3_where)
elif cc_no == 7:
a4_where = (279, 448)
img_2_1.paste(img_1_1, a4_where)
elif cc_no == 8:
a5_where = (279, 665)
img_2_1.paste(img_1_1, a5_where)
img_2_1.save("ceshi2.jpg")
img._show(img_2_1)
img_2_1.save("输出结果结果%s.jpg"% \
name)
return
def file_name(file_dir):
for root,dirs,files in os.walk(file_dir):
return files, root
if __name__ == '__main__':
pan_duan_sex_data(a1_1_sex,a1_1_num)
[a11_1,b11_1]=rand_num(a1_1_num)
a13_1=[]
a13_2=[]
a13_3=[]
for i in b11_1:
ptos_flies, root_name = file_name(file_photo_dr)
num_x = ptos_flies.__len__()
ax_name = list(range(num_x))
ptos_flies.sort();
for j in range(num_x-1):
ax_name[j] = root_name + ptos_flies[j]
if (i<=75):
a12_1=ax_name[int(i)]
else:
a12_1 = ax_name[int(i/2)]
a12_2='测试成功'
a12_3='高贵的1'
a13_1.append(a12_1)
a13_2.append(a12_2)
a13_3.append(a12_3)
print("查找到的直接牌意%s"%\
a13_2)
print("查找到的牌名%s"%\
a13_3)
a14_1 = len(a13_1)
for i in b11_1:
a14_2=('bake.jpg')
crade_buzhi(a13_1, a11_1, a14_1, a14_2,name)
a15_1=input('是否保留本次运行的结果1保存,2不保存===')
if int(a15_1)==1:
print('保存')
print('这功能没法用了')
else:
print('不保存')
运行需要的组件
