0

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.

MichaelKo
  • 41
  • 6
  • 1
    I don't understand, do you have problems concatenating pictures or finding the order of concatenation? – RMPR Apr 08 '20 at 14:29
  • -> The full part of concatenating the pictures (including the right ordering). I guess it makes sense to merge first all pictures in "one line", means all pictures for varying {j} and constant {i}. Second, merging the full lines to the final picture. Is this understandable? – MichaelKo Apr 08 '20 at 14:35
  • @RMPR do you have an idea for a code? – MichaelKo Apr 09 '20 at 18:55
  • I added above some lines of code to solve the concate issue, but now the filename needs to be re-structured... – MichaelKo Apr 10 '20 at 22:17

1 Answers1

-1

Maybe, you can use pygame

If you add import pygame and use 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

This is not the best idea but it may help

Just_Me
  • 111
  • 8