{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from PIL import Image\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "im = np.array(Image.open('data/src/lena_square.png'))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "im_R = im.copy()\n", "im_R[:, :, (1, 2)] = 0\n", "im_G = im.copy()\n", "im_G[:, :, (0, 2)] = 0\n", "im_B = im.copy()\n", "im_B[:, :, (0, 1)] = 0" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# 横に並べて結合(どれでもよい)\n", "im_RGB = np.concatenate((im_R, im_G, im_B), axis=1)\n", "# im_RGB = np.hstack((im_R, im_G, im_B))\n", "# im_RGB = np.c_['1', im_R, im_G, im_B]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "pil_img_RGB = Image.fromarray(im_RGB)\n", "pil_img_RGB.save('data/dst/lena_numpy_split_color.jpg')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "im_gray = 0.299 * im[:, :, 0] + 0.587 * im[:, :, 1] + 0.114 * im[:, :, 2]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "uint8\n", "float64\n" ] } ], "source": [ "print(im.dtype)\n", "print(im_gray.dtype)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(512, 512, 3)\n", "(512, 512)\n" ] } ], "source": [ "print(im.shape)\n", "print(im_gray.shape)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "pil_img_gray = Image.fromarray(np.uint8(im_gray))\n", "pil_img_gray.save('data/dst/lena_numpy_gray.jpg')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "im_swap = im.copy()\n", "im_swap[:, :, 0], im_swap[:, :, 2] = im[:, :, 2], im[:, :, 0]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "pil_img_swap = Image.fromarray(im_swap)\n", "pil_img_swap.save('data/dst/lena_numpy_swap_color.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.3" } }, "nbformat": 4, "nbformat_minor": 2 }