900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > python批量添加经纬度照片水印 照片水印 坐标转换

python批量添加经纬度照片水印 照片水印 坐标转换

时间:2020-09-20 12:48:38

相关推荐

python批量添加经纬度照片水印 照片水印 坐标转换

通过获取shp文件的大地2000坐标,然后转换为经纬度坐标,最后将经纬度坐标添加到照片中,实现照片添加水印,实现批量添加经纬度坐标水印。

# -*- coding: utf-8 -*-import cv2, randomimport cv2, os, sysfrom PIL import ImageFont, ImageDraw, Imageimport numpy as npimport shutilfrom pyproj import CRSfrom pyproj import Transformerimport timeimport randomfrom osgeo import ogrimport os, sys, globimport sys, tracebackfrom PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QActionfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtWidgets import QMessageBoxlist_cmsj = {}def yes(zm, cm, wd, jd):global list_cmsjpath_jpg = 'D:/path_jpg.jpg'try:date_rq = list_cmsj[cm]except:date_rq = time_factory_rz()list_cmsj[cm] = date_rq# 编辑图片路径bk_img = cv2.imread(path_jpg)sp = bk_img.shapeheight = sp[0] # height(rows) of imagewidth = sp[1] # width(colums) of image# 设置需要显示的字体fontpath = "msyhl.ttc"# 32为字体大小font = ImageFont.truetype(fontpath, 36)img_pil = Image.fromarray(bk_img)draw = ImageDraw.Draw(img_pil)# 绘制文字信息# (100,300/350)为字体的位置,(255,255,255)为白色,(0,0,0)为黑色draw.text((20, height-80), "备注:", font=font, fill=(255, 255, 255))draw.text((20, height-140), "海拔:%s米"%str(random.randint(4000, 5000)/100), font=font, fill=(255, 255, 255))draw.text((20, height-200), "时间:%s %s"%(date_rq, time_factory_xs()), font=font, fill=(255, 255, 255))draw.text((20, height-260), "地址:河源市紫金县%s%s"%(zm, cm), font=font, fill=(255, 255, 255))draw.text((20, height-320), "纬度:%s"%wd, font=font, fill=(255, 255, 255))draw.text((20, height-380), "经度:%s"%jd, font=font, fill=(255, 255, 255))bk_img = np.array(img_pil)cv2.imwrite('D:/path_jpg_x.jpg', bk_img)def marcator(x,y):lonlat = {}# CGCS2000_3_Degree_GK_Zone_40 的wkidcrs_CGCS2000 = CRS.from_epsg(4526)#4526crs_CGCS2000 = CRS.from_wkt('PROJCS["CGCS2000_3_Degree_GK_Zone_38",GEOGCS["GCS_China_Geodetic_Coordinate_System_2000",DATUM["D_China_2000",SPHEROID["CGCS2000",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Gauss_Kruger"],PARAMETER["False_Easting",38500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",114.0],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0],AUTHORITY["EPSG",4526]]')#我们需要转换的目标坐标系的WKIDcrs_WGS84 = CRS.from_epsg(4490)#4490#crs_WGS84 = CRS.from_wkt('PROJCS["Xian_1980_3_Degree_GK_CM_114E",GEOGCS["GCS_Xian_1980",DATUM["D_Xian_1980",SPHEROID["Xian_1980",6378140.0,298.257]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Gauss_Kruger"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",114.0],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]')from_crs = crs_CGCS2000to_crs = crs_WGS84transformer = Transformer.from_crs(from_crs, to_crs)# 即为转换后的坐标,也可以分别使⽤数组new_x, new_y = transformer.transform(x, y)lonlat['x'] = new_xlonlat['y'] = new_yreturn new_x, new_ydef select_shp(oath_shp, list_bh):ds = ogr.Open(oath_shp, 0)layer = ds.GetLayer()# featuredefn = layer.GetLayerDefn() # 获取图层属性表定义# fieldcount = featuredefn.GetFieldCount() # 获取属性表中字段数# for attr in range(fieldcount):#fielddefn = featuredefn.GetFieldDefn(attr)#print("%s: %s" % ( fielddefn.GetNameRef(),fielddefn.GetFieldTypeName(fielddefn.GetType())))#outtxtfile = open('JZX1.txt', 'w') # 以可写方式打开feature = layer.GetNextFeature() # 遍历list = {}while feature:# 读取字段值bh = feature.GetFieldAsString('编号')if bh in list_bh:pt = feature.geometry()x = pt.GetX()if x < 1000000:x += 38000000y = pt.GetY()jwd = marcator(x, y)zm = feature.GetFieldAsString('镇')cm = feature.GetFieldAsString('行政村')list.setdefault(bh, []).append(zm)list.setdefault(bh, []).append(cm)list.setdefault(bh, []).append(jwd[0])list.setdefault(bh, []).append(jwd[1])feature.Destroy()feature = layer.GetNextFeature()# 清除DataSource缓存并关闭TXT文件ds.Destroy()return listdef time_factory_xs():start_time = (, 4, 30, 8, 0, 0, 0, 0, 0)end_time = (, 4, 30, 17, 59, 59, 0, 0, 0)start = time.mktime(start_time) # 生成开始时间戳end = time.mktime(end_time) # 生成结束时间戳t = random.randint(start, end) # 在开始和结束时间戳中随机取出一个date_touple = time.localtime(t) # 将时间戳生成时间元组date1 = time.strftime("%H:%M:%S", date_touple) # 将时间元组转成格式化字符串return date1def time_factory_rz():start_time = (, 8, 29, 8, 0, 0, 0, 0, 0)end_time = (, 10, 1, 17, 59, 59, 0, 0, 0)start = time.mktime(start_time) # 生成开始时间戳end = time.mktime(end_time) # 生成结束时间戳t = random.randint(start, end) # 在开始和结束时间戳中随机取出一个date_touple = time.localtime(t) # 将时间戳生成时间元组date2 = time.strftime("%Y-%m-%d", date_touple) # 将时间元组转成格式化字符串return date2def ye_main(self, path, path_bc, oath_shp):list_name_jpg = os.listdir(path)list_bh = []for name_jpg in list_name_jpg:a, b = os.path.splitext(name_jpg)list_bh.append(a)get_text(self, '正在获取shp信息')list_shp = select_shp(oath_shp, list_bh)print(list_shp)get_text(self, '开始对照片添加文字')jd_pl = 100 / len(list_name_jpg)jd = 100 / len(list_name_jpg)for name_jpg in list_name_jpg:try:path_jpg = os.path.join(path, name_jpg)shutil.copy(path_jpg, 'D:/path_jpg.jpg')path_bc_jpg = os.path.join(path_bc, name_jpg)a, b = os.path.splitext(name_jpg)shpxx = list_shp[a]zm = shpxx[0]cm = shpxx[1]jd = shpxx[3]wd = shpxx[2]yes(zm, cm, wd, jd)shutil.copy('D:/path_jpg_x.jpg', path_bc_jpg)get_text(self, '%s-成功'%name_jpg)except:get_text(self, '%s-失败' % name_jpg)jd = jd + jd_plself.progressBar.setProperty("value", jd)QApplication.processEvents()if QMessageBox.information(None, '提示', "转换完成,是否打开文件夹进行查看",QMessageBox.Yes | QMessageBox.No) == 16384: # 给出确定与否的弹窗,是的话则下一步os.startfile(path_bc) # 打开文件def get_text(self, text):self.textEdit.append(text) # 设置报错文本QApplication.processEvents()def main(self):def askdirectory_word_pdf_xz():""""""_translate = QtCore.QCoreApplication.translatefolder = QFileDialog.getExistingDirectory()self.lineEdit.setText(_translate("Form", folder))def askdirectory_word_pdf_bc():""""""_translate = QtCore.QCoreApplication.translatefolder = QFileDialog.getExistingDirectory()self.lineEdit_3.setText(_translate("Form", folder))def askdirectory():_translate = QtCore.QCoreApplication.translatefolder = QFileDialog.getOpenFileName(caption='选择SHP文件', filter='Txt files(*.shp)')# print(folder[0])self.lineEdit_2.setText(_translate("Form", folder[0]))def ye():try:get_text(self, '######################运行参数######################')path = self.lineEdit.text()oath_shp = self.lineEdit_2.text()path_bc = self.lineEdit_3.text()get_text(self, 'JPG路径:%s'%path)get_text(self, 'SHP路径:%s'%oath_shp)get_text(self, '保存路径:%s'%path_bc)get_text(self, '######################运行信息######################')ye_main(self, path, path_bc, oath_shp)get_text(self, '######################运行完成######################')except:get_text(self, '######################运行出错######################')get_text(self, traceback.format_exc())self.toolButton.clicked.connect(askdirectory_word_pdf_xz)self.toolButton_2.clicked.connect(askdirectory)self.toolButton_3.clicked.connect(askdirectory_word_pdf_bc)self.pushButton.clicked.connect(ye)if __name__ == '__main__':import jmapp = QApplication(sys.argv)MainWindow = QMainWindow() # QMainWindow模块名self = jm.Ui_MainWindow()self.setupUi(MainWindow)main(self)MainWindow.show()sys.exit(app.exec_())

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。