Skip to content

Instantly share code, notes, and snippets.

@mkckr0
Created February 3, 2023 02:44
Show Gist options
  • Save mkckr0/fd69f8e75794fa3984a06029341ab92e to your computer and use it in GitHub Desktop.
Save mkckr0/fd69f8e75794fa3984a06029341ab92e to your computer and use it in GitHub Desktop.
Merge multi images to a single image
def merge_and_save_image(bytes_list, save_path):
img_list = []
img_mode = 'RGB'
img_width = 0
img_height = [0]
for i, bytes in enumerate(bytes_list):
try:
img = Image.open(io.BytesIO(bytes))
except PIL.UnidentifiedImageError:
continue
img_list.append(img)
img_mode = img.mode
img_width = img_width if img_width > img.width else img.width
img_height.append(img_height[-1] + img.height)
long_img = Image.new(img_mode, (img_width, img_height[-1]))
for i, img in enumerate(img_list):
long_img.paste(img, (0, img_height[i]))
long_img.save(save_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment