python从postgis获取数据写入本地(Postgre入门四)
python从postgis获取数据写入本地(Postgre入门四)
·
1、准备数据库数据
2、python读取写入本地
from PIL import Image
import psycopg2 as ps
import os
from io import BytesIO
def getconn():
return ps.connect(database=database,user=user,password=password,host=host,port=port)
# 查询表数据
def query(sql):
conn= getconn()
cursor = conn.cursor()
cursor.execute(sql)
# 返回所有数据
class_list = cursor.fetchall()
cursor.close()
conn.close()
return class_list
if __name__ == "__main__":
tablename="img_test"
sql = "select * from cataract;"
# 1、从postgis中获取数据
datas=query(sql)
# 2、数据写入本地
for data in datas:
name = data[0]
if os.path.exists(os.path.dirname(name))!=True:
os.makedirs(os.path.dirname(name))
content = data[1]
img = Image.open(BytesIO(content))
img.save(name)
3、结果

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)