├── .gitattributes ├── img.jpg ├── image.jpg ├── README.md ├── LICENSE ├── .gitignore └── sketch.ipynb /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PrasoonPratham/Sketches-with-Python/HEAD/img.jpg -------------------------------------------------------------------------------- /image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PrasoonPratham/Sketches-with-Python/HEAD/image.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sketches with Python 2 | This Python Script converts an image into a sktech, made using OpenCV. 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Pratham Prasoon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .nox/ 42 | .coverage 43 | .coverage.* 44 | .cache 45 | nosetests.xml 46 | coverage.xml 47 | *.cover 48 | .hypothesis/ 49 | .pytest_cache/ 50 | 51 | # Translations 52 | *.mo 53 | *.pot 54 | 55 | # Django stuff: 56 | *.log 57 | local_settings.py 58 | db.sqlite3 59 | 60 | # Flask stuff: 61 | instance/ 62 | .webassets-cache 63 | 64 | # Scrapy stuff: 65 | .scrapy 66 | 67 | # Sphinx documentation 68 | docs/_build/ 69 | 70 | # PyBuilder 71 | target/ 72 | 73 | # Jupyter Notebook 74 | .ipynb_checkpoints 75 | 76 | # IPython 77 | profile_default/ 78 | ipython_config.py 79 | 80 | # pyenv 81 | .python-version 82 | 83 | # celery beat schedule file 84 | celerybeat-schedule 85 | 86 | # SageMath parsed files 87 | *.sage.py 88 | 89 | # Environments 90 | .env 91 | .venv 92 | env/ 93 | venv/ 94 | ENV/ 95 | env.bak/ 96 | venv.bak/ 97 | 98 | # Spyder project settings 99 | .spyderproject 100 | .spyproject 101 | 102 | # Rope project settings 103 | .ropeproject 104 | 105 | # mkdocs documentation 106 | /site 107 | 108 | # mypy 109 | .mypy_cache/ 110 | .dmypy.json 111 | dmypy.json 112 | 113 | # Pyre type checker 114 | .pyre/ 115 | -------------------------------------------------------------------------------- /sketch.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Create a Pencil Sketch Portrait with Python OpenCV", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3.7.9 64-bit ('tf': conda)", 12 | "metadata": { 13 | "interpreter": { 14 | "hash": "d5a5503ee387615641d7a4a93854ed490d580d7c63f5b4674f909ab8da538278" 15 | } 16 | } 17 | } 18 | }, 19 | "cells": [ 20 | { 21 | "cell_type": "code", 22 | "metadata": { 23 | "id": "61uS0rwiW7l_", 24 | "colab_type": "code", 25 | "colab": {} 26 | }, 27 | "source": [ 28 | "import cv2" 29 | ], 30 | "execution_count": 13, 31 | "outputs": [] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "metadata": { 36 | "id": "d61I51shXPEG", 37 | "colab_type": "code", 38 | "colab": {} 39 | }, 40 | "source": [ 41 | "img = cv2.imread('images.PNG')" 42 | ], 43 | "execution_count": 32, 44 | "outputs": [] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "metadata": { 49 | "id": "xaXJUapjY9zo", 50 | "colab_type": "code", 51 | "colab": { 52 | "base_uri": "https://localhost:8080/", 53 | "height": 277 54 | }, 55 | "outputId": "09085c87-8bd8-45f0-9dce-e8815e471973" 56 | }, 57 | "source": [ 58 | "cv2.imshow('',img)\n", 59 | "cv2.waitKey(0)" 60 | ], 61 | "execution_count": 33, 62 | "outputs": [ 63 | { 64 | "output_type": "execute_result", 65 | "data": { 66 | "text/plain": [ 67 | "-1" 68 | ] 69 | }, 70 | "metadata": {}, 71 | "execution_count": 33 72 | } 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "metadata": { 78 | "id": "sCsEKnCoZG63", 79 | "colab_type": "code", 80 | "colab": {} 81 | }, 82 | "source": [ 83 | "img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n" 84 | ], 85 | "execution_count": 34, 86 | "outputs": [] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "metadata": { 91 | "id": "QGraatoLZXXV", 92 | "colab_type": "code", 93 | "colab": { 94 | "base_uri": "https://localhost:8080/", 95 | "height": 277 96 | }, 97 | "outputId": "508cc594-9a22-4cfd-8ee3-d3767538fd9c" 98 | }, 99 | "source": [ 100 | "cv2.imshow('',img_gray)\n", 101 | "cv2.waitKey(0)" 102 | ], 103 | "execution_count": 35, 104 | "outputs": [ 105 | { 106 | "output_type": "execute_result", 107 | "data": { 108 | "text/plain": [ 109 | "-1" 110 | ] 111 | }, 112 | "metadata": {}, 113 | "execution_count": 35 114 | } 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "metadata": { 120 | "id": "EcIpThzEZY6P", 121 | "colab_type": "code", 122 | "colab": {} 123 | }, 124 | "source": [ 125 | "img_invert = cv2.bitwise_not(img_gray)\n" 126 | ], 127 | "execution_count": 36, 128 | "outputs": [] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "metadata": { 133 | "id": "cawpuG_TZdlv", 134 | "colab_type": "code", 135 | "colab": { 136 | "base_uri": "https://localhost:8080/", 137 | "height": 277 138 | }, 139 | "outputId": "eed14271-d71f-43e7-b291-ee72165c47b0" 140 | }, 141 | "source": [ 142 | "cv2.imshow('',img_invert)\n", 143 | "cv2.waitKey(0)\n" 144 | ], 145 | "execution_count": 37, 146 | "outputs": [ 147 | { 148 | "output_type": "execute_result", 149 | "data": { 150 | "text/plain": [ 151 | "-1" 152 | ] 153 | }, 154 | "metadata": {}, 155 | "execution_count": 37 156 | } 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "metadata": { 162 | "id": "nj309M6DZe7D", 163 | "colab_type": "code", 164 | "colab": { 165 | "base_uri": "https://localhost:8080/", 166 | "height": 277 167 | }, 168 | "outputId": "cc0f97da-d611-4126-aa95-70dcb160fb7a" 169 | }, 170 | "source": [ 171 | "img_smoothing = cv2.GaussianBlur(img_invert, (21, 21),sigmaX=0, sigmaY=0)\n", 172 | "\n", 173 | "cv2.imshow('',img_smoothing)\n", 174 | "cv2.waitKey(0)" 175 | ], 176 | "execution_count": 38, 177 | "outputs": [ 178 | { 179 | "output_type": "execute_result", 180 | "data": { 181 | "text/plain": [ 182 | "-1" 183 | ] 184 | }, 185 | "metadata": {}, 186 | "execution_count": 38 187 | } 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "metadata": { 193 | "id": "kBNzWMCeZpah", 194 | "colab_type": "code", 195 | "colab": {} 196 | }, 197 | "source": [ 198 | "def dodgeV2(x, y):\n", 199 | " return cv2.divide(x, 255 - y, scale=256)" 200 | ], 201 | "execution_count": 39, 202 | "outputs": [] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "metadata": { 207 | "id": "108-XMptZh2d", 208 | "colab_type": "code", 209 | "colab": { 210 | "base_uri": "https://localhost:8080/", 211 | "height": 277 212 | }, 213 | "outputId": "9e9a4f84-9eeb-4f58-e1c7-a5d6ffd8ead6" 214 | }, 215 | "source": [ 216 | "final_img = dodgeV2(img_gray, img_smoothing)\n", 217 | "\n", 218 | "cv2.imshow('',final_img)\n", 219 | "cv2.waitKey(0)" 220 | ], 221 | "execution_count": 40, 222 | "outputs": [ 223 | { 224 | "output_type": "execute_result", 225 | "data": { 226 | "text/plain": [ 227 | "-1" 228 | ] 229 | }, 230 | "metadata": {}, 231 | "execution_count": 40 232 | } 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 41, 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "output_type": "execute_result", 242 | "data": { 243 | "text/plain": [ 244 | "True" 245 | ] 246 | }, 247 | "metadata": {}, 248 | "execution_count": 41 249 | } 250 | ], 251 | "source": [ 252 | "cv2.imwrite('img.jpg', final_img)" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 61, 258 | "metadata": {}, 259 | "outputs": [ 260 | { 261 | "output_type": "execute_result", 262 | "data": { 263 | "text/plain": [ 264 | "True" 265 | ] 266 | }, 267 | "metadata": {}, 268 | "execution_count": 61 269 | } 270 | ], 271 | "source": [ 272 | "import cv2\n", 273 | "img = cv2.imread('image.jpg')\n", 274 | "img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n", 275 | "img_invert = cv2.bitwise_not(img_gray)\n", 276 | "img_smoothing = cv2.GaussianBlur(img_invert, (21, 21),sigmaX=0, sigmaY=0)\n", 277 | "def dodge(x, y):\n", 278 | " return cv2.divide(x, 255 - y, scale=256)\n", 279 | "\n", 280 | "final_img = dodge(img_gray, img_smoothing)\n", 281 | "\n", 282 | "cv2.imwrite('img.jpg', final_img)" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": null, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [] 291 | } 292 | ] 293 | } --------------------------------------------------------------------------------