虽然地图之前的地图只有12块左右的地图块,但是由于图片名为32位随机编码组成,导致图片需要人工一个一个进行获取并进行位置固定。导致流程非常的繁琐麻烦。

这次图片虽然非常的多,最高清的图片为88*88=7744张图片构成,但是图片是有序排序的。这就代表,不需要手工下载和位置确认。代码从头运行到尾,从下载图片到拼接都不需要人工干涉就能完成任务


这次使用到的依赖库为
HTTP请求库requests:pip install requests
(也就是爬出工具,模拟请求获取图片,并将其下载下来)
图像处理库:pip3 install --upgrade Pillow==9.2.0
(用于创造画布并把图片块放到画布中再进行图片保存)
图片格式为“列_行_类型.webp”构成
额,剩下的没什么好说的的,我都将其写进注释里了。
其中图片地址需要粘贴到代码中,记得结尾的/不能也带过来


代码详情
from io import BytesIO
import os
import requests
import time
from PIL import Image
def pseudo_deletion_file(old_path):
""" 伪删除旧文件 """
if os.path.exists(old_path):
path, old_file_name = os.path.split(old_path)
new_name = 'temp' + str(int(time.time())) + "_" + old_file_name
os.rename(old_path, os.path.join(path, new_name))
class ReqImage:
def __init__(self, img_type):
self.image_type = img_type
if self.image_type not in ('N1', 'N2', 'N3', 'P0'):
raise Exception('图片类型错误')
self.image_url_base = 'imageUrl'
if self.image_url_base == 'imageUrl':
raise Exception('请输入正确的图片地址')
self.cur_row_paths = []
self.col_paths = []
pseudo_deletion_file(img_type)
os.makedirs(img_type)
def download_image(self, image_name, save_path):
""" 爬取并保存图片 """
# 发送GET请求获取图片内容
print("图片网址:" + self.image_url_base + "/" + image_name, end="\t")
response = requests.get(self.image_url_base + "/" + image_name)
# 确保请求成功
if response.status_code == 200:
# 使用BytesIO将响应内容转换为二进制流
image_data = BytesIO(response.content)
# 使用PIL库打开图片
image = Image.open(image_data)
# 保存图片到本地
image.save(save_path)
print(f"图片保存成功:{save_path}")
self.cur_row_paths.append(save_path)
return 1
else:
print(f"图片保存失败:{save_path},错误代码:{response.status_code}", end="\n\n")
if self.cur_row_paths:
self.col_paths.append(self.cur_row_paths)
self.cur_row_paths = []
return 2
def req_image_one(self, row_index, col_index):
""" 保存一张图片 """
image_name = '%d_%d_%s.webp' % (row_index, col_index, self.image_type)
image_path = './%s/%s' % (self.image_type, image_name)
return self.download_image(image_name, image_path)
def main(self):
""" 保存整块图片 """
# 过了一个星期,整理代码时,看了半天发现我也忘记我写的代码是怎么遍历的了
count = col_site = row_site = 0
for col in range(1000):
row_curr_site = 0
for row in range(1000):
result = self.req_image_one(col, row)
if result == 2:
row_curr_site = row
break
else:
count += 1
col_site = col
row_site = row
# 限制图片获取速度,每获取20张停止1秒
# if count % 20 == 0:
# time.sleep(1)
if row_curr_site == 0:
break
print(f"保存结束:图片块数量为:{col_site}*{row_site}={col_site * row_site}")
return self.col_paths
def joint_and_save_image(img_paths, img_type):
""" 拼接并保存图片 """
# 单个图片长宽,整体图片长宽,创建整体画布
image_width, image_height = Image.open(img_paths[0][0]).size
count_width, count_height = len(img_paths[0]) * image_width, len(img_paths) * image_height
new_image = Image.new("RGBA", (count_width, count_height))
for col_index, row_image_paths in enumerate(img_paths):
# 打开一排图片
row_image_paths = [Image.open(imgPath) for imgPath in row_image_paths]
for row_index, img_open in enumerate(row_image_paths):
if image_width != img_open.size[0] or image_height != img_open.size[1]:
print("图片长度不一致,拼接错误")
return -1
new_image.paste(img_open, (col_index * image_height, row_index * image_width))
print(f"完成拼接{col_index}/{len(img_paths)}", end="\t")
print("\n拼接结束,开始保存")
image_name = img_type + ".png"
pseudo_deletion_file(image_name)
new_image.save(image_name, "PNG")
print("保存成功")
if __name__ == '__main__':
# 分辨率由低到高 'N1', 'N2', 'N3', 'P0'
image_type = "P0"
image_paths = ReqImage(image_type).main()
joint_and_save_image(image_paths, image_type)
print("代码运行结束")





