{"cells": [{"cell_type": "code", "execution_count": null, "outputs": [], "source": ["from PIL import Image\n", "import glob\n", "import cv2\n", "import numpy as np\n", "from random import shuffle\n", "import matplotlib.pyplot as plt"], "metadata": {"_uuid": "92097dbfc5796a7029ef6faecf9a2c67e5eb29c7", "_cell_guid": "c8d336f2-77f3-43fe-9fe7-ede81b9d3ef9", "collapsed": true}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["def crop(img):\n", "    width, height = img.size  # Get dimensions\n", "\n", "    left = (width - 512) / 2\n", "    top = (height - 512) / 2\n", "    right = (width + 512) / 2\n", "    bottom = (height + 512) / 2\n", "\n", "    return img.crop((left, top, right, bottom))\n", "\n", "\n", "def gamma_correction(array_img, gamma=1.0):\n", "    invGamma = 1.0 / gamma\n", "    table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype(\"uint8\")\n", "\n", "    return cv2.LUT(array_img, table)\n", "\n", "\n", "def jpg_compression(array, quality):\n", "    img = Image.fromarray(array)\n", "    img.save('img.jpg', \"JPEG\", quality=quality)\n", "    return cv2.cvtColor(cv2.imread('img.jpg'), cv2.COLOR_BGR2RGB)\n", "\n", "\n", "def resizing(array_img, factor):\n", "    h, w, ch = array_img.shape\n", "    return cv2.resize(array_img, (int(factor * w), int(factor * h)), interpolation=cv2.INTER_CUBIC)\n"], "metadata": {"_uuid": "0b7b39294ba812e722d0410f263529adf13bfad4", "_cell_guid": "1811b18a-06bf-40f4-8872-864a59699e25", "collapsed": true}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["img = Image.open('../input/image-for-test/(HTC-1-M7)22.jpg')\n", "plt.imshow(img)\n", "plt.show()"], "metadata": {"_uuid": "d0a2a3095d84cc656e38c01496c1c6ba9ad5a250", "_cell_guid": "3e9285a4-633c-4711-818c-f05d145a29e5"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["array_img = np.array(img)\n", "manip_img = gamma_correction(array_img, 0.8)"], "metadata": {"_uuid": "06671f5ac7dc15c02bbcf17b8e4b665d16705889", "_cell_guid": "5ee72ddd-4865-42ad-8a1c-d7cb79169112", "collapsed": true}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["manip_img = jpg_compression(manip_img, 70)\n", "manip_img = resizing(manip_img, 2.0)"], "metadata": {"_uuid": "cde1e607cbab5542bd3bd85a94e5dc8034af3d4e", "_cell_guid": "b01e5885-2bad-4a45-99c1-24ba7347a64b", "collapsed": true}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["manip_img = Image.fromarray(manip_img)"], "metadata": {"_uuid": "dcbfc293eba58d0546104d8622011565913ec371", "_cell_guid": "f7a08ff1-a354-4aed-9ae4-c7c12ddc06b9", "collapsed": true}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": ["manip_img = crop(manip_img)\n", "plt.imshow(manip_img)\n", "plt.show()"], "metadata": {"_uuid": "634cd171310c81c37c5a931fe7d57c539e6be703", "_cell_guid": "53da7acb-d743-4e39-810b-b4568e3341f1"}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": [], "metadata": {"_uuid": "ce084184076ba2dc597b7274541c79f83149b43a", "_cell_guid": "0a915d10-e3b2-42d8-9f0c-76521a6a7ee8", "collapsed": true}}], "nbformat": 4, "nbformat_minor": 1, "metadata": {"kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}, "widgets": {"state": {}, "version": "1.1.2"}, "language_info": {"file_extension": ".py", "pygments_lexer": "ipython3", "mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "3.6.4", "name": "python", "codemirror_mode": {"name": "ipython", "version": 3}}}}