I want to create a large picture of the area where i am living at the moment and print it out on paper. I very like the maps from opentopomap.org.
I already finished a code to extract the single pictures in a specific order. See the code below:
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")```
All pictures are save on my desktop in that order:
https___c.tile.opentopomap.org_16_{j}_{i}
e.g. https___c.tile.opentopomap.org_16_34309_22370 (Mannheim, Germany)
Now, i need a good code to concatenate all pictures in the right order using the filenames. Does anyone know some lines of code to do? I have found and modified this: 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()
The problem is here, that the filename is currently for example:
https___c.tile.opentopomap.org_16_34309_22370
For the merging the filename should look like this: e.g.
https___c.tile.opentopomap.org_16_222370_34309
The last two blocks should be switched...
Does someone have an idea how to solve this "renaming" issue?
Thanks for your help.