Skip to content

Instantly share code, notes, and snippets.

@ming-chu
Forked from glombard/combine.py
Created January 17, 2021 03:33
Show Gist options
  • Save ming-chu/b36ff546d6e73375f633dfe80831ba32 to your computer and use it in GitHub Desktop.
Save ming-chu/b36ff546d6e73375f633dfe80831ba32 to your computer and use it in GitHub Desktop.

Revisions

  1. @glombard glombard created this gist Nov 24, 2014.
    33 changes: 33 additions & 0 deletions combine.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    # Combine multiple images into one.
    #
    # To install the Pillow module on Mac OS X:
    #
    # $ xcode-select --install
    # $ brew install libtiff libjpeg webp little-cms2
    # $ pip install Pillow
    #

    from __future__ import print_function
    import os

    from PIL import Image

    files = [
    '~/Downloads/1.jpg',
    '~/Downloads/2.jpg',
    '~/Downloads/3.jpg',
    '~/Downloads/4.jpg']

    result = Image.new("RGB", (800, 800))

    for index, file in enumerate(files):
    path = os.path.expanduser(file)
    img = Image.open(path)
    img.thumbnail((400, 400), Image.ANTIALIAS)
    x = index // 2 * 400
    y = index % 2 * 400
    w, h = img.size
    print('pos {0},{1} size {2},{3}'.format(x, y, w, h))
    result.paste(img, (x, y, x + w, y + h))

    result.save(os.path.expanduser('~/image.jpg'))