一、常见库
- exifread 读取图片中的信息,如GPS信息
https://blog.csdn.net/qq1198768105/article/details/128159598 - tkinter 创建窗口程序
信息弹窗
https://www.jb51.net/article/241488.htm
布局
https://www.cnblogs.com/weidawang/p/15426468.html - csv 创建,增加csv文件
- os 读取目录等系统操作
- python-docx 对word进行操作
http://www.taodudu.cc/news/show-5764012.html?action=onClick - pyinstaller 对python打包处理
pyinstaller -F -w mergeWord.py
然后exe文件在dist文件夹中
二、写的两个案例
根据图片目录生成word
from docx import Document# 创建 Document 对象,等价于在电脑上打开一个 Word 文档
from docx.shared import Inches, Cm # Inches=英寸 Cm=厘米 1英尺=2.54厘米
import os
from tkinter import *
import tkinter.messagebox
def getImgList(dir):
dirList = []
for single_img in os.listdir(dir):
dirList.append(r'{folder}\{file}'.format(folder=dir, file=single_img))
return dirList
def main():
d1 = Ent_Dir1.get()
d2 = Ent_Dir2.get()
wordloc = Ent_WordLoc.get()
if(d1 == ""):
tkinter.messagebox.showerror(title = '提示',message='图片目录1为空')
return
if(d2 == ""):
tkinter.messagebox.showerror(title = '提示',message='图片目录2为空')
return
if(wordloc == ""):
tkinter.messagebox.showerror(title = '提示',message='word位置为空')
return
file_name = d1.split("\\")[-1]+"_"+d2.split("\\")[-1]
document = Document()# 在 Word 文档中添加一个标题
# dir1 = r"J:\图片\2022一期"
# dir2 = r"J:\图片\2323一期"
dir1 = r'{file}'.format(file=d1)
dir2 = r'{file}'.format(file=d2)
dirlist1 = getImgList(dir1)
dirlist2 = getImgList(dir2)
# print(dirlist1)
for i in range(len(dirlist1)):
a = document.add_paragraph().add_run()
a.add_picture(r'{file}'.format(file=dirlist1[i]), width=Cm(7.5))
a.add_picture(r'{file}'.format(file=dirlist2[i]), width=Cm(7.5))
document.save(r'{wordloc}\{file_name}.docx'.format(wordloc = wordloc,file_name = file_name))
tkinter.messagebox.showinfo(title = '提示',message='word正在持续生成中(可打开word查看),请勿关闭程序')
root = Tk()
root.title("根据图片目录生成word")
root.geometry("700x200+500+300")
# 创建一个按钮,按钮名字为”点击“
# 图片目录1
label1 = Label(root,text="图片目录1:")
label1.grid(row=0,column=0)
Ent_Dir1=Entry(root,width=100)
Ent_Dir1.grid(row=0,column=1,ipadx=10)
# 图片目录2
label2 = Label(root,text="图片目录2:")
label2.grid(row=1,column=0)
Ent_Dir2=Entry(root,width=100)
Ent_Dir2.grid(row=1,column=1,ipadx=10)
# 生成word位置
label3 = Label(root,text="生成word位置:")
label3.grid(row=2,column=0)
Ent_WordLoc=Entry(root,width=100)
Ent_WordLoc.grid(row=2,column=1,ipadx=10)
# 生成
btn_gen = Button(root,text="生成(点击后程序未响应是正常的)",command=main,width=30)
btn_gen.grid(row=3)
# 让根窗口保持运行
root.mainloop()
读取图片坐标生成csv
import os
from tkinter import *
import tkinter.messagebox
import exifread
import csv
def get_single_gps(img):
with open(img, 'rb') as f:
# 直接读取度分秒格式的经纬度数据
contents = exifread.process_file(f)
try:
longitude = contents["GPS GPSLongitude"].values
has_longitude = True
except:
has_longitude = False
if not has_longitude:
return '', ''
else:
longitude = contents["GPS GPSLongitude"].values
latitude = contents["GPS GPSLatitude"].values
height = contents["GPS GPSAltitude"].values
# 度分秒转换成十进制数据
longitude_f = longitude[0].num / longitude[0].den + (longitude[1].num / longitude[1].den / 60) + (
longitude[2].num / longitude[2].den / 3600)
latitude_f = latitude[0].num / latitude[0].den + (latitude[1].num / latitude[1].den / 60) + (
latitude[2].num / latitude[2].den / 3600)
altitude_f = height[0].num / height[0].den
# 打印验证数据
# print(img,end=",")
# print(longitude_f,end=",")
# print(latitude_f,end=",")
# print(altitude_f)
return longitude_f, latitude_f,altitude_f
def get_gps(img_path,postfix):
img_name = []
logitude_list = []
latitude_list = []
altitude_list = []
for single_img in os.listdir(img_path):
if(single_img.split(".")[-1] != postfix):
continue
img_name.append(single_img)
longitude, latitude,altitude = get_single_gps(img_path + '/' + single_img)
logitude_list.append(longitude)
latitude_list.append(latitude)
altitude_list.append(altitude)
return img_name, logitude_list, latitude_list,altitude_list
def create_csv(root,file_name):
header = ['img_path', 'Longitude', 'Latitude','Altitude']
with open(root + '/'+file_name+'.csv', 'w', encoding='utf-8-sig', newline="") as f:
writer = csv.writer(f)
writer.writerow(header)
def write_csv(root,file_name, result_list):
for i in result_list:
with open(root + '/'+file_name+'.csv', 'a', encoding='utf-8-sig', newline="") as f:
writer = csv.writer(f)
writer.writerow(i)
def main():
img = Ent_ImgDir.get()
csv_loc = Ent_CSV.get()
postfix = Ent_postfix.get()
if(img == ""):
tkinter.messagebox.showerror(title = '提示',message='图片目录为空')
return
if(csv_loc == ""):
tkinter.messagebox.showerror(title = '提示',message='csv目录为空')
return
if(postfix == ""):
tkinter.messagebox.showerror(title = '提示',message='图片后缀为空')
return
file_name = img.split("\\")[-1]
print(file_name)
root = os.getcwd()
create_csv(csv_loc,file_name)
img_name, logitude_list,latitude_list ,altitude_list= get_gps(img,postfix)
index = range(len(img_name))
row_list = [[img_name[i], logitude_list[i], latitude_list[i],altitude_list[i]] for i in index]
write_csv(csv_loc,file_name, row_list)
tkinter.messagebox.showinfo(title = '提示',message='生成完毕')
# if __name__ == '__main__':
# createTk()
# img = 'E:\img\DJI_202309041627_013_kzy3'
# main(img)
root = Tk()
root.title("读取图片坐标生成csv")
root.geometry("700x200+500+300")
# 创建一个按钮,按钮名字为”点击“
# 图片目录
label1 = Label(root,text="图片目录位置:")
label1.grid(row=0,column=0)
Ent_ImgDir=Entry(root,width=100)
Ent_ImgDir.grid(row=0,column=1,ipadx=10)
# csv生成位置
label2 = Label(root,text="生成csv目录位置:")
label2.grid(row=1,column=0)
Ent_CSV=Entry(root,width=100)
Ent_CSV.grid(row=1,column=1,ipadx=10)
# csv生成位置
defaultvalue = tkinter.StringVar(value="DNG")
label3 = Label(root,text="图片后缀(注意大小写):")
label3.grid(row=2,column=0)
Ent_postfix=Entry(root,width=100,textvariable=defaultvalue)
Ent_postfix.grid(row=2,column=1,ipadx=10)
# 生成
btn_gen = Button(root,text="生成",command=main,width=20)
btn_gen.grid(row=3)
# 让根窗口保持运行
root.mainloop()
标签:入门,img,python,list,构造,file,csv,root,name
From: https://www.cnblogs.com/CoderWangEx/p/17687561.html