{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from PIL import Image" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "im1 = Image.open('data/src/lena.jpg')\n", "im2 = Image.open('data/src/rocket.jpg')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_h(im1, im2):\n", " dst = Image.new('RGB', (im1.width + im2.width, im1.height))\n", " dst.paste(im1, (0, 0))\n", " dst.paste(im2, (im1.width, 0))\n", " return dst" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_v(im1, im2):\n", " dst = Image.new('RGB', (im1.width, im1.height + im2.height))\n", " dst.paste(im1, (0, 0))\n", " dst.paste(im2, (0, im1.height))\n", " return dst" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "get_concat_h(im1, im1).save('data/dst/pillow_concat_h.jpg')\n", "get_concat_v(im1, im1).save('data/dst/pillow_concat_v.jpg')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_h_cut(im1, im2):\n", " dst = Image.new('RGB', (im1.width + im2.width, min(im1.height, im2.height)))\n", " dst.paste(im1, (0, 0))\n", " dst.paste(im2, (im1.width, 0))\n", " return dst" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_v_cut(im1, im2):\n", " dst = Image.new('RGB', (min(im1.width, im2.width), im1.height + im2.height))\n", " dst.paste(im1, (0, 0))\n", " dst.paste(im2, (0, im1.height))\n", " return dst" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "get_concat_h_cut(im1, im2).save('data/dst/pillow_concat_h_cut.jpg')\n", "get_concat_v_cut(im1, im2).save('data/dst/pillow_concat_v_cut.jpg')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_h_cut_center(im1, im2):\n", " dst = Image.new('RGB', (im1.width + im2.width, min(im1.height, im2.height)))\n", " dst.paste(im1, (0, 0))\n", " dst.paste(im2, (im1.width, (im1.height - im2.height) // 2))\n", " return dst" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_v_cut_center(im1, im2):\n", " dst = Image.new('RGB', (min(im1.width, im2.width), im1.height + im2.height))\n", " dst.paste(im1, (0, 0))\n", " dst.paste(im2, ((im1.width - im2.width) // 2, im1.height))\n", " return dst" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "get_concat_h_cut_center(im1, im2).save('data/dst/pillow_concat_h_cut_center.jpg')\n", "get_concat_v_cut_center(im1, im2).save('data/dst/pillow_concat_v_cut_center.jpg')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_h_blank(im1, im2, color=(0, 0, 0)):\n", " dst = Image.new('RGB', (im1.width + im2.width, max(im1.height, im2.height)), color)\n", " dst.paste(im1, (0, 0))\n", " dst.paste(im2, (im1.width, 0))\n", " return dst" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_v_blank(im1, im2, color=(0, 0, 0)):\n", " dst = Image.new('RGB', (max(im1.width, im2.width), im1.height + im2.height), color)\n", " dst.paste(im1, (0, 0))\n", " dst.paste(im2, (0, im1.height))\n", " return dst" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "get_concat_h_blank(im1, im2).save('data/dst/pillow_concat_h_blank.jpg')\n", "get_concat_v_blank(im1, im2, (0, 64, 128)).save('data/dst/pillow_concat_v_blank.jpg')" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_h_resize(im1, im2, resample=Image.BICUBIC, resize_big_image=True):\n", " if im1.height == im2.height:\n", " _im1 = im1\n", " _im2 = im2\n", " elif (((im1.height > im2.height) and resize_big_image) or\n", " ((im1.height < im2.height) and not resize_big_image)):\n", " _im1 = im1.resize((int(im1.width * im2.height / im1.height), im2.height), resample=resample)\n", " _im2 = im2\n", " else:\n", " _im1 = im1\n", " _im2 = im2.resize((int(im2.width * im1.height / im2.height), im1.height), resample=resample)\n", " dst = Image.new('RGB', (_im1.width + _im2.width, _im1.height))\n", " dst.paste(_im1, (0, 0))\n", " dst.paste(_im2, (_im1.width, 0))\n", " return dst" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_v_resize(im1, im2, resample=Image.BICUBIC, resize_big_image=True):\n", " if im1.width == im2.width:\n", " _im1 = im1\n", " _im2 = im2\n", " elif (((im1.width > im2.width) and resize_big_image) or\n", " ((im1.width < im2.width) and not resize_big_image)):\n", " _im1 = im1.resize((im2.width, int(im1.height * im2.width / im1.width)), resample=resample)\n", " _im2 = im2\n", " else:\n", " _im1 = im1\n", " _im2 = im2.resize((im1.width, int(im2.height * im1.width / im2.width)), resample=resample)\n", " dst = Image.new('RGB', (_im1.width, _im1.height + _im2.height))\n", " dst.paste(_im1, (0, 0))\n", " dst.paste(_im2, (0, _im1.height))\n", " return dst" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "get_concat_h_resize(im1, im2).save('data/dst/pillow_concat_h_resize.jpg')\n", "get_concat_v_resize(im1, im2, resize_big_image=False).save('data/dst/pillow_concat_v_resize.jpg')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_h_multi_blank(im_list):\n", " _im = im_list.pop(0)\n", " for im in im_list:\n", " _im = get_concat_h_blank(_im, im)\n", " return _im" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "get_concat_h_multi_blank([im1, im2, im1]).save('data/dst/pillow_concat_h_multi_blank.jpg')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_h_multi_resize(im_list, resample=Image.BICUBIC):\n", " min_height = min(im.height for im in im_list)\n", " im_list_resize = [im.resize((int(im.width * min_height / im.height), min_height),resample=resample)\n", " for im in im_list]\n", " total_width = sum(im.width for im in im_list_resize)\n", " dst = Image.new('RGB', (total_width, min_height))\n", " pos_x = 0\n", " for im in im_list_resize:\n", " dst.paste(im, (pos_x, 0))\n", " pos_x += im.width\n", " return dst" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_v_multi_resize(im_list, resample=Image.BICUBIC):\n", " min_width = min(im.width for im in im_list)\n", " im_list_resize = [im.resize((min_width, int(im.height * min_width / im.width)),resample=resample)\n", " for im in im_list]\n", " total_height = sum(im.height for im in im_list_resize)\n", " dst = Image.new('RGB', (min_width, total_height))\n", " pos_y = 0\n", " for im in im_list_resize:\n", " dst.paste(im, (0, pos_y))\n", " pos_y += im.height\n", " return dst" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "get_concat_h_multi_resize([im1, im2, im1]).save('data/dst/pillow_concat_h_multi_resize.jpg')\n", "get_concat_v_multi_resize([im1, im2, im1]).save('data/dst/pillow_concat_v_multi_resize.jpg')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_tile_resize(im_list_2d, resample=Image.BICUBIC):\n", " im_list_v = [get_concat_h_multi_resize(im_list_h, resample=resample) for im_list_h in im_list_2d]\n", " return get_concat_v_multi_resize(im_list_v, resample=resample)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "get_concat_tile_resize([[im1],\n", " [im1, im2],\n", " [im1, im2, im1]]).save('data/dst/pillow_concat_tile_resize.jpg')" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_h_repeat(im, column):\n", " dst = Image.new('RGB', (im.width * column, im.height))\n", " for x in range(column):\n", " dst.paste(im, (x * im.width, 0))\n", " return dst" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_v_repeat(im, row):\n", " dst = Image.new('RGB', (im.width, im.height * row))\n", " for y in range(row):\n", " dst.paste(im, (0, y * im.height))\n", " return dst" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_concat_tile_repeat(im, row, column):\n", " dst_h = get_concat_h_repeat(im, column)\n", " return get_concat_v_repeat(dst_h, row)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "im_s = im1.resize((im1.width // 2, im1.height // 2))\n", "get_concat_tile_repeat(im_s, 3, 4).save('data/dst/pillow_concat_tile_repeat.jpg')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }