PythonLoop:从opentopomap.org下载.png,并将单个png文件连接到一个大图中

2024-09-28 18:48:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想画一张我现在居住的地区的大图,然后打印在纸上。我非常喜欢opentopomap.org上的地图

我已经完成了一个代码,可以按照特定的顺序提取单个图片。请参阅下面的代码:

import multiprocessing
import pprint
import time


print("The pictures will be saved all what is east-south from Mannheim Quadrate")
x2 = int(input("North-South (latitude) (start: 22369, Mannheim) Type in more than 22369: "))
x3 = int(input("East-West (longitude) (start: 34309, Mannheim) Type in more than 34309: "))

urls = [
    f"https://c.tile.opentopomap.org/16/{j}/{i}.png" for i in range(22369, x2 + 1) for j in range(34309, x3 + 1)]

def download_image(url):
    response = requests.get(url)
    print(f"Downloading from {url}...")
    url = url.replace("/", "_").replace(":", "_")
    with open(f"{url}", "wb") as file:
        file.write(response.content)

if __name__ == "__main__":
    start = time.perf_counter()
    p = multiprocessing.Pool(processes=4)
    p.map(download_image, urls)
    p.close()
    stop = time.perf_counter()


    print(f"It took {round(stop - start, 2)} seconds in total")```

所有图片都按以下顺序保存在我的桌面上:

     https___c.tile.opentopomap.org_16_{j}_{i}
e.g. https___c.tile.opentopomap.org_16_34309_22370 (Mannheim, Germany)

现在,我需要一个好的代码来使用文件名以正确的顺序连接所有图片。有人知道一些要做的代码吗?我已经找到并修改了这个:How do you merge images into a canvas using PIL/Pillow?

import PIL, os, glob
from PIL import Image
from math import ceil, floor

PATH = r"C:\Users\micha\Desktop\test"

frame_width = 3072
images_per_row = 12
padding = 0

os.chdir(PATH)

images = glob.glob("*.png")
images = images[:30]

img_width, img_height = Image.open(images[0]).size

scaled_img_width = ceil(img_width)
scaled_img_height = ceil(img_height)

number_of_rows = ceil(len(images)/images_per_row)
frame_height = ceil(img_height*number_of_rows)

new_im = Image.new('RGB', (frame_width, frame_height))

i,j=0,0
for num, im in enumerate(images):
    if num%images_per_row==0:
        i=0
    im = Image.open(im)
    #Here I resize my opened image, so it is no bigger than 100,100
    im.thumbnail((scaled_img_width,scaled_img_height))
    #Iterate through a 4 by 4 grid with 100 spacing, to place my image
    y_cord = (j//images_per_row)*scaled_img_height
    new_im.paste(im, (i,y_cord))
    print(i, y_cord)
    i=(i+scaled_img_width)+padding
    j+=1

new_im.show()
new_im.save("out.jpg", "JPEG", quality=80, optimize=True, progressive=True)

new_im.save()

问题在于,文件名当前为,例如:

https___c.tile.opentopomap.org_16_34309_22370 

对于合并,文件名应该如下所示:例如

https___c.tile.opentopomap.org_16_222370_34309 

最后两个街区应该切换

有人知道如何解决这个“重命名”问题吗

谢谢你的帮助


Tags: inhttpsorgimporturlimgnewwidth
1条回答
网友
1楼 · 发布于 2024-09-28 18:48:26

也许,你可以用pygame

如果添加import pygame并使用pygame.Surface

import pygame

image1 = pygame.image.load("image1") # First image
image2 = pygame.image.load("image2") # Second image
image3 = pygame.Surface(total width of images, height of an image) #  Free surface
image3.blit(image1, (0, 0)) # Drawing images on image3
image3.blit(image2, (image1s width, 0))
pygame.image.save(image3, "name of image.png") # Saving total of them to harddisk

这不是最好的主意,但可能会有所帮助

相关问题 更多 >