|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Simple Batch Breakpoint generator python" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "This notebook script will easy generate responsive image for Frontend website, maintain aspect ratio, no stretch.\n", |
| 15 | + "Normally I have to generate it via https://www.responsivebreakpoints.com/ which quite slow for file by file.\n", |
| 16 | + "So I create some Vue component with fixed Breakpoint size [200,763,1132,1400] in other site production (not in this project) feel free to modify the code " |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "metadata": {}, |
| 22 | + "source": [ |
| 23 | + "## what it do ?" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "Specify the folder path that have all the image (right now is all .jpg) and breakpoint width and run script and done.\n" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "## Import Library" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": 18, |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [], |
| 45 | + "source": [ |
| 46 | + "import os, sys\n", |
| 47 | + "from PIL import Image\n", |
| 48 | + "import math \n", |
| 49 | + "import glob " |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "metadata": {}, |
| 55 | + "source": [ |
| 56 | + "## Function" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": 25, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "def resize_maintain_aspect_by_width(PIL_Image,new_width):\n", |
| 66 | + " '''\n", |
| 67 | + " This function get image and resize by width (maintain aspect ratio) without strech\n", |
| 68 | + " '''\n", |
| 69 | + " width, height = PIL_Image.size\n", |
| 70 | + " # if new width is greater than original width, return the image\n", |
| 71 | + " if new_width > width:\n", |
| 72 | + " return PIL_Image\n", |
| 73 | + " \n", |
| 74 | + " new_height = math.floor(new_width * height / width )\n", |
| 75 | + " return PIL_Image.resize((new_width, new_height), Image.ANTIALIAS)" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": 26, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [ |
| 84 | + "def savebreakpoint(filename,PIL_Image,breakpoint):\n", |
| 85 | + " '''\n", |
| 86 | + " This function save PIL image by add postfix from specify breakpoint\n", |
| 87 | + " '''\n", |
| 88 | + " path = filename.rsplit('.', 1)\n", |
| 89 | + " path[0] = path[0]+'_'+str(breakpoint)\n", |
| 90 | + " PIL_Image.save(r'.'.join(path))" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "markdown", |
| 95 | + "metadata": {}, |
| 96 | + "source": [ |
| 97 | + "## Edit here" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": 27, |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [], |
| 105 | + "source": [ |
| 106 | + "# set breakpoints width\n", |
| 107 | + "breakpoints = [200,763,1132,1400]\n", |
| 108 | + "# set the folder path\n", |
| 109 | + "files = glob.glob('./testbatch/**/*.jpg',recursive = True) " |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "markdown", |
| 114 | + "metadata": {}, |
| 115 | + "source": [ |
| 116 | + "## Run" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "code", |
| 121 | + "execution_count": 28, |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [], |
| 124 | + "source": [ |
| 125 | + "for file in files:\n", |
| 126 | + " im = Image.open(file) \n", |
| 127 | + " for breakpoint in breakpoints:\n", |
| 128 | + " tmp = resize_maintain_aspect_by_width(im,breakpoint)\n", |
| 129 | + " savebreakpoint(file,tmp,breakpoint) " |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "markdown", |
| 134 | + "metadata": {}, |
| 135 | + "source": [ |
| 136 | + "### Output: Sample.jpg to Sample_200.jpg, Sample_763.jpg, Sample_1132.jpg, Sample_1400.jpg" |
| 137 | + ] |
| 138 | + } |
| 139 | + ], |
| 140 | + "metadata": { |
| 141 | + "kernelspec": { |
| 142 | + "display_name": "Python 3", |
| 143 | + "language": "python", |
| 144 | + "name": "python3" |
| 145 | + }, |
| 146 | + "language_info": { |
| 147 | + "codemirror_mode": { |
| 148 | + "name": "ipython", |
| 149 | + "version": 3 |
| 150 | + }, |
| 151 | + "file_extension": ".py", |
| 152 | + "mimetype": "text/x-python", |
| 153 | + "name": "python", |
| 154 | + "nbconvert_exporter": "python", |
| 155 | + "pygments_lexer": "ipython3", |
| 156 | + "version": "3.8.3" |
| 157 | + } |
| 158 | + }, |
| 159 | + "nbformat": 4, |
| 160 | + "nbformat_minor": 4 |
| 161 | +} |
0 commit comments