{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1-Resim Okuma / Kaydetme" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [Matplotlib](https://matplotlib.org/users/image_tutorial.html) ile Resim Dosyasını Okuma " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:14.855050Z", "start_time": "2019-08-22T09:25:14.068570Z" } }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import matplotlib.image as mpimg\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:14.868279Z", "start_time": "2019-08-22T09:25:14.857150Z" } }, "outputs": [], "source": [ "img = mpimg.imread('images/parrot.png')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:15.136711Z", "start_time": "2019-08-22T09:25:14.869850Z" } }, "outputs": [], "source": [ "plt.imshow(img)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [Pillow](https://pillow.readthedocs.io/en/3.1.x/index.html) ile Resim Dosyasını Okuma " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:15.144017Z", "start_time": "2019-08-22T09:25:15.138829Z" } }, "outputs": [], "source": [ "from PIL import Image\n", "from matplotlib.pyplot import imshow\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Grayscale" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:15.794076Z", "start_time": "2019-08-22T09:25:15.782165Z" } }, "outputs": [], "source": [ "img_gray = Image.open(\"images/lena_gray.gif\")\n", "print(img_gray.width, img_gray.height, img_gray.mode, img_gray.format, type(img_gray))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:16.220346Z", "start_time": "2019-08-22T09:25:16.015813Z" } }, "outputs": [], "source": [ "imshow(img_gray)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:16.408485Z", "start_time": "2019-08-22T09:25:16.395030Z" } }, "outputs": [], "source": [ "pixels = list(img_gray.getdata())\n", "len(pixels)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Color" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:17.164406Z", "start_time": "2019-08-22T09:25:17.158206Z" } }, "outputs": [], "source": [ "img_lena = Image.open(\"images/lena.jpg\").convert('RGB')\n", "print(img_lena.width, img_lena.height, img_lena.mode, img_lena.format, type(img_lena))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:17.815534Z", "start_time": "2019-08-22T09:25:17.616595Z" } }, "outputs": [], "source": [ "imshow(img_lena)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:18.287628Z", "start_time": "2019-08-22T09:25:18.284802Z" } }, "outputs": [], "source": [ "width, height = img_lena.size\n", "print(width, height)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Renkli görüntünün 3 kanal için pixel değerlerini okuma**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:22.995670Z", "start_time": "2019-08-22T09:25:19.519736Z" } }, "outputs": [], "source": [ "w,h = img_lena.size\n", "for i in range(w):\n", " for j in range(h):\n", " r,g,b = img_lena.getpixel((i,j))\n", " print(\"({0},{1}) : r:{2},g:{3},b:{4}\".format(i,j, r,g,b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Resim oluşturma ve Resmin negatifini alma" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:23.010486Z", "start_time": "2019-08-22T09:25:22.999265Z" } }, "outputs": [], "source": [ "img_new = np.zeros([220,220,3],dtype=np.uint8)\n", "img_new.fill(0)\n", "img_new" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:23.264825Z", "start_time": "2019-08-22T09:25:23.012943Z" } }, "outputs": [], "source": [ "imshow(img_new)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:23.275612Z", "start_time": "2019-08-22T09:25:23.270691Z" } }, "outputs": [], "source": [ "img_new.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Pixel değerlerini değiştirme**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:24.421108Z", "start_time": "2019-08-22T09:25:24.085220Z" } }, "outputs": [], "source": [ "import ipywidgets as widgets\n", "from ipywidgets import interact, interact_manual\n", "\n", "@interact\n", "def change_color_img(column='colors', r=(0,255), g=(0,255), b=(0,255)):\n", " w = img_new.shape[0]\n", " h = img_new.shape[1]\n", " c = img_new.shape[2]\n", " for i in range(w):\n", " for j in range(h):\n", " img_new[i][j][0] = r\n", " img_new[i][j][1] = g\n", " img_new[i][j][2] = b\n", " \n", " imshow(img_new)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Resmin Negatifini Alma" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:33.175864Z", "start_time": "2019-08-22T09:25:32.893787Z" } }, "outputs": [], "source": [ "img = Image.open('images/lena.jpg')\n", "img_array = np.array(img)\n", "\n", "imshow(img_array)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:33.879535Z", "start_time": "2019-08-22T09:25:33.179116Z" } }, "outputs": [], "source": [ "img_negative = np.zeros([220,220,3],dtype=np.uint8)\n", "\n", "for i in range(img_array.shape[0]):\n", " for j in range(img_array.shape[1]):\n", " r,g,b = img_array[i][j]\n", " \n", " img_negative[i][j][0] = 255-r\n", " img_negative[i][j][1] = 255-g\n", " img_negative[i][j][2] = 255-b\n", "\n", "imshow(img_negative)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Resim Kaydetme" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:33.910994Z", "start_time": "2019-08-22T09:25:33.882280Z" } }, "outputs": [], "source": [ "img_lena_negative = Image.fromarray(img_negative)\n", "img_lena_negative.save('images/lena_negative.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Daha fazla interaktif örnek için: http://setosa.io/ev/image-kernels/" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.5.2" } }, "nbformat": 4, "nbformat_minor": 4 }