Skip to content

Instantly share code, notes, and snippets.

@HUGHNew
Created April 3, 2023 12:40
Show Gist options
  • Save HUGHNew/1cf12b19bfd7624638bf3988b88a1fae to your computer and use it in GitHub Desktop.
Save HUGHNew/1cf12b19bfd7624638bf3988b88a1fae to your computer and use it in GitHub Desktop.
Stitching equal parts of the picture
import requests as reqs
from PIL import Image
urls = [ # your images in group
[
"", "", ""
],
[],
[]
]
def get_images(urls: list[list[str]]) -> list[list[str]]:
files = []
for idx, row in enumerate(urls):
files.append([])
for i, col in enumerate(row):
image = reqs.get(col).content
f = f"{idx}_{i}.jpg"
files[idx].append(f)
with open(f, "wb") as img:
img.write(image)
return files
def compose(image_files: list[list[str]], output_image: str):
images = [
[
Image.open(file)
for file in row
]
for row in image_files
]
rows = len(images)
cols = len(images[0])
width = sum([
images[0][i].size[0]
for i in range(cols)
])
height = sum([
images[i][0].size[1]
for i in range(rows)
])
output = Image.new("RGB", (width, height))
w, h = images[0][0].size
for idx, row in enumerate(urls):
for i, col in enumerate(row):
w_off = i * w
h_off = idx * h
output.paste(images[idx][i], (w_off, h_off))
output.save(output_image)
if __name__ == "__main__":
imgs = get_images()
compose(imgs, "jas.30w.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment