{"id":937,"date":"2025-01-04T21:02:04","date_gmt":"2025-01-04T21:02:04","guid":{"rendered":"https:\/\/kourentzes.com\/konstantinos\/?p=937"},"modified":"2025-01-04T21:02:04","modified_gmt":"2025-01-04T21:02:04","slug":"optimize-images-python-script","status":"publish","type":"post","link":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/","title":{"rendered":"Creating a Python Image Optimization Script"},"content":{"rendered":"\n<p>Optimizing images for web use is essential for faster loading times and better performance. In this guide, we\u2019ll show you how to create a Python script to optimize PNG and WebP images, and even convert it into an executable file for ease of use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python installed on your machine (version 3.6 or above).<\/li>\n\n\n\n<li>Basic knowledge of Python and the command line.<\/li>\n\n\n\n<li>Install the required library using: <\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#212121\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"pip install pillow\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme-darker\" style=\"background-color: #212121\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #EEFFFF\">pip install pillow<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#212121;color:#d5ffff;font-size:12px;line-height:1;position:relative\">PowerShell<\/span><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">The Python Script<\/h2>\n\n\n\n<p>Below is the complete Python script to optimize images:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#212121\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"import os\nfrom PIL import Image\n\ndef optimize_images():\n    # Define folders\n    current_folder = os.getcwd()\n    optimized_folder = os.path.join(current_folder, &quot;optimized&quot;)\n\n    # Ensure the output folder exists\n    if not os.path.exists(optimized_folder):\n        os.makedirs(optimized_folder)\n\n    # Supported formats\n    supported_formats = (&quot;.png&quot;, &quot;.webp&quot;)\n\n    # Iterate through files in the current folder\n    for filename in os.listdir(current_folder):\n        if filename.lower().endswith(supported_formats):\n            input_path = os.path.join(current_folder, filename)\n            output_path = os.path.join(optimized_folder, filename)\n\n            # Skip already optimized images\n            if os.path.exists(output_path):\n                print(f&quot;Skipping already optimized image: {filename}&quot;)\n                continue\n\n            try:\n                # Open the image\n                with Image.open(input_path) as img:\n                    if filename.lower().endswith(&quot;.png&quot;):\n                        # Optimize PNG\n                        img.save(output_path, format=&quot;PNG&quot;, optimize=True)\n                    elif filename.lower().endswith(&quot;.webp&quot;):\n                        # Optimize WebP with maximum compression settings\n                        img.save(output_path, format=&quot;WEBP&quot;, quality=75, method=6)\n                    print(f&quot;Optimized: {filename}&quot;)\n            except Exception as e:\n                print(f&quot;Failed to optimize {filename}: {e}&quot;)\n\nif __name__ == &quot;__main__&quot;:\n    optimize_images()\n\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme-darker\" style=\"background-color: #212121\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #89DDFF; font-style: italic\">import<\/span><span style=\"color: #EEFFFF\"> os<\/span><\/span>\n<span class=\"line\"><span style=\"color: #89DDFF; font-style: italic\">from<\/span><span style=\"color: #EEFFFF\"> PIL <\/span><span style=\"color: #89DDFF; font-style: italic\">import<\/span><span style=\"color: #EEFFFF\"> Image<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C792EA\">def<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">optimize_images<\/span><span style=\"color: #89DDFF\">():<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #545454; font-style: italic\"># Define folders<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    current_folder <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> os<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">getcwd<\/span><span style=\"color: #89DDFF\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    optimized_folder <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> os<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #F07178\">path<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">join<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">current_folder<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> <\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #C3E88D\">optimized<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #89DDFF\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #545454; font-style: italic\"># Ensure the output folder exists<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #89DDFF; font-style: italic\">if<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF\">not<\/span><span style=\"color: #EEFFFF\"> os<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #F07178\">path<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">exists<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">optimized_folder<\/span><span style=\"color: #89DDFF\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">        os<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">makedirs<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">optimized_folder<\/span><span style=\"color: #89DDFF\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #545454; font-style: italic\"># Supported formats<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    supported_formats <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #C3E88D\">.png<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #C3E88D\">.webp<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #89DDFF\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #545454; font-style: italic\"># Iterate through files in the current folder<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #89DDFF; font-style: italic\">for<\/span><span style=\"color: #EEFFFF\"> filename <\/span><span style=\"color: #89DDFF; font-style: italic\">in<\/span><span style=\"color: #EEFFFF\"> os<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">listdir<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">current_folder<\/span><span style=\"color: #89DDFF\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">        <\/span><span style=\"color: #89DDFF; font-style: italic\">if<\/span><span style=\"color: #EEFFFF\"> filename<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">lower<\/span><span style=\"color: #89DDFF\">().<\/span><span style=\"color: #82AAFF\">endswith<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">supported_formats<\/span><span style=\"color: #89DDFF\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">            input_path <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> os<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #F07178\">path<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">join<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">current_folder<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> filename<\/span><span style=\"color: #89DDFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">            output_path <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> os<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #F07178\">path<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">join<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">optimized_folder<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> filename<\/span><span style=\"color: #89DDFF\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">            <\/span><span style=\"color: #545454; font-style: italic\"># Skip already optimized images<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">            <\/span><span style=\"color: #89DDFF; font-style: italic\">if<\/span><span style=\"color: #EEFFFF\"> os<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #F07178\">path<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">exists<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">output_path<\/span><span style=\"color: #89DDFF\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                <\/span><span style=\"color: #82AAFF\">print<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #C792EA\">f<\/span><span style=\"color: #C3E88D\">&quot;Skipping already optimized image: <\/span><span style=\"color: #F78C6C\">{<\/span><span style=\"color: #82AAFF\">filename<\/span><span style=\"color: #F78C6C\">}<\/span><span style=\"color: #C3E88D\">&quot;<\/span><span style=\"color: #89DDFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                <\/span><span style=\"color: #89DDFF; font-style: italic\">continue<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">            <\/span><span style=\"color: #89DDFF; font-style: italic\">try<\/span><span style=\"color: #89DDFF\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                <\/span><span style=\"color: #545454; font-style: italic\"># Open the image<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                <\/span><span style=\"color: #89DDFF; font-style: italic\">with<\/span><span style=\"color: #EEFFFF\"> Image<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">open<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">input_path<\/span><span style=\"color: #89DDFF\">)<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF; font-style: italic\">as<\/span><span style=\"color: #EEFFFF\"> img<\/span><span style=\"color: #89DDFF\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                    <\/span><span style=\"color: #89DDFF; font-style: italic\">if<\/span><span style=\"color: #EEFFFF\"> filename<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">lower<\/span><span style=\"color: #89DDFF\">().<\/span><span style=\"color: #82AAFF\">endswith<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #C3E88D\">.png<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #89DDFF\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                        <\/span><span style=\"color: #545454; font-style: italic\"># Optimize PNG<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                        img<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">save<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">output_path<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> <\/span><span style=\"color: #EEFFFF; font-style: italic\">format<\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #C3E88D\">PNG<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> <\/span><span style=\"color: #EEFFFF; font-style: italic\">optimize<\/span><span style=\"color: #89DDFF\">=True)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                    <\/span><span style=\"color: #89DDFF; font-style: italic\">elif<\/span><span style=\"color: #EEFFFF\"> filename<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">lower<\/span><span style=\"color: #89DDFF\">().<\/span><span style=\"color: #82AAFF\">endswith<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #C3E88D\">.webp<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #89DDFF\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                        <\/span><span style=\"color: #545454; font-style: italic\"># Optimize WebP with maximum compression settings<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                        img<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">save<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">output_path<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> <\/span><span style=\"color: #EEFFFF; font-style: italic\">format<\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #C3E88D\">WEBP<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> <\/span><span style=\"color: #EEFFFF; font-style: italic\">quality<\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #F78C6C\">75<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> <\/span><span style=\"color: #EEFFFF; font-style: italic\">method<\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #F78C6C\">6<\/span><span style=\"color: #89DDFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                    <\/span><span style=\"color: #82AAFF\">print<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #C792EA\">f<\/span><span style=\"color: #C3E88D\">&quot;Optimized: <\/span><span style=\"color: #F78C6C\">{<\/span><span style=\"color: #82AAFF\">filename<\/span><span style=\"color: #F78C6C\">}<\/span><span style=\"color: #C3E88D\">&quot;<\/span><span style=\"color: #89DDFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">            <\/span><span style=\"color: #89DDFF; font-style: italic\">except<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #FFCB6B\">Exception<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF; font-style: italic\">as<\/span><span style=\"color: #EEFFFF\"> e<\/span><span style=\"color: #89DDFF\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                <\/span><span style=\"color: #82AAFF\">print<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #C792EA\">f<\/span><span style=\"color: #C3E88D\">&quot;Failed to optimize <\/span><span style=\"color: #F78C6C\">{<\/span><span style=\"color: #82AAFF\">filename<\/span><span style=\"color: #F78C6C\">}<\/span><span style=\"color: #C3E88D\">: <\/span><span style=\"color: #F78C6C\">{<\/span><span style=\"color: #82AAFF\">e<\/span><span style=\"color: #F78C6C\">}<\/span><span style=\"color: #C3E88D\">&quot;<\/span><span style=\"color: #89DDFF\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #89DDFF; font-style: italic\">if<\/span><span style=\"color: #EEFFFF\"> __name__ <\/span><span style=\"color: #89DDFF\">==<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #C3E88D\">__main__<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #89DDFF\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #82AAFF\">optimize_images<\/span><span style=\"color: #89DDFF\">()<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#212121;color:#d5ffff;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Explanation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Supported Formats<\/strong>: The script works with <code>.png<\/code> and <code>.webp<\/code> files.<\/li>\n\n\n\n<li><strong>Optimized Folder<\/strong>: All optimized files are saved in a subfolder named <code>optimized<\/code>.<\/li>\n\n\n\n<li><strong>Skipping Optimized Images<\/strong>: The script skips images that already exist in the <code>optimized<\/code> folder.<\/li>\n\n\n\n<li><strong>Compression Settings<\/strong>: For PNGs, it uses Pillow\u2019s <code>optimize=True<\/code>, and for WebP, it applies <code>quality=75<\/code> with <code>method=6<\/code> for maximum compression.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Creating an Executable<\/h2>\n\n\n\n<p>You can convert this script into an executable file for ease of use.<\/p>\n\n\n\n<p><strong>Install PyInstaller<\/strong>: <\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#212121\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"pip install pyinstaller\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme-darker\" style=\"background-color: #212121\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #EEFFFF\">pip install pyinstaller<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#212121;color:#d5ffff;font-size:12px;line-height:1;position:relative\">PowerShell<\/span><\/div>\n\n\n\n<p><strong>Create Executable<\/strong>: <\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#212121\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"pyinstaller --onefile image_optimizer.py\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme-darker\" style=\"background-color: #212121\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #EEFFFF\">pyinstaller <\/span><span style=\"color: #89DDFF\">--<\/span><span style=\"color: #EEFFFF\">onefile image_optimizer.py<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#212121;color:#d5ffff;font-size:12px;line-height:1;position:relative\">PowerShell<\/span><\/div>\n\n\n\n<p><strong>Locate Executable<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The executable will be in the <strong><code>dist<\/code> folder<\/strong> as <code>image_optimizer.exe<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><strong>Test the Executable<\/strong>: Place the <code>.exe<\/code> file into a directory with images to optimize. Double-click or run it via the command line to start the optimization process.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This script provides an efficient solution for optimizing images for the web, ensuring fast performance. Converting it into an executable enhances usability, even for non-technical users. Start optimizing today!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">README.mc<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#212121\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Image Optimizer\nThis script optimizes PNG and WebP images for web use, reducing their size without compromising quality.\n\n## Features\n- Supports PNG and WebP formats.\n- Saves optimized images in a separate folder.\n- Skips already optimized files.\n\n## How to Use\n1. Place images in the same directory as the script.\n2. Run the script:\n   ```bash\n   python image_optimizer.py\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme-darker\" style=\"background-color: #212121\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #EEFFFF\"># Image Optimizer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">This script optimizes PNG and WebP images for web use, reducing their size without compromising quality.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">## Features<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">- Supports PNG and WebP formats.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">- Saves optimized images in a separate folder.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">- Skips already optimized files.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">## How to Use<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">1. Place images in the same directory as the script.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">2. Run the script:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">   ```bash<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">   python image_optimizer.py<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#212121;color:#d5ffff;font-size:12px;line-height:1;position:relative\">XML<\/span><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Requirements<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python 3.6+<\/li>\n\n\n\n<li>Pillow library<\/li>\n<\/ul>\n\n\n\n<p>Install dependencies:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#212121\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"pip install pillow\npip install pyinstaller\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme-darker\" style=\"background-color: #212121\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #EEFFFF\">pip install pillow<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">pip install pyinstaller<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#212121;color:#d5ffff;font-size:12px;line-height:1;position:relative\">PowerShell<\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Discover how to create a Python script that optimizes images for the web and converts it into an executable file for easier usage.<\/p>\n","protected":false},"author":1,"featured_media":938,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[511,501,505,510,502,507,508,504,506,512,503,500,509],"class_list":["post-937","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code-snipperts","tag-automation","tag-def","tag-executable-creation","tag-image-compression","tag-image-optimization","tag-image-processing","tag-png","tag-pyinstaller","tag-python","tag-python-tools","tag-scripting","tag-web-development","tag-webp"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Creating a Python Image Optimization Script - konstantinos.kourentzes.com<\/title>\n<meta name=\"description\" content=\"Learn how to create a Python script to optimize PNG and WebP images for web use. Includes code, explanation, and how to create an executable.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating a Python Image Optimization Script - konstantinos.kourentzes.com\" \/>\n<meta property=\"og:description\" content=\"Learn how to create a Python script to optimize PNG and WebP images for web use. Includes code, explanation, and how to create an executable.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/\" \/>\n<meta property=\"og:site_name\" content=\"konstantinos.kourentzes.com\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-04T21:02:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2025\/01\/emperor_kk_Discover_how_to_create_a_Python_script_that_optimi_297b6e39-fc9d-49e1-b334-7ba12a087dc5_11.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Konstantinos Kourentzes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@kkourentzes\" \/>\n<meta name=\"twitter:site\" content=\"@kkourentzes\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Konstantinos Kourentzes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/\"},\"author\":{\"name\":\"Konstantinos Kourentzes\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/#\\\/schema\\\/person\\\/2693fb0ad7f7638a020431ffe372c822\"},\"headline\":\"Creating a Python Image Optimization Script\",\"datePublished\":\"2025-01-04T21:02:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/\"},\"wordCount\":235,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/#\\\/schema\\\/person\\\/2693fb0ad7f7638a020431ffe372c822\"},\"image\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/emperor_kk_Discover_how_to_create_a_Python_script_that_optimi_297b6e39-fc9d-49e1-b334-7ba12a087dc5_11.png\",\"keywords\":[\"automation\",\"def\",\"executable creation\",\"image compression\",\"image optimization\",\"image processing\",\"png\",\"pyinstaller\",\"python\",\"python tools\",\"scripting\",\"web development\",\"webp\"],\"articleSection\":[\"Code Snippets\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/\",\"url\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/\",\"name\":\"Creating a Python Image Optimization Script - konstantinos.kourentzes.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/emperor_kk_Discover_how_to_create_a_Python_script_that_optimi_297b6e39-fc9d-49e1-b334-7ba12a087dc5_11.png\",\"datePublished\":\"2025-01-04T21:02:04+00:00\",\"description\":\"Learn how to create a Python script to optimize PNG and WebP images for web use. Includes code, explanation, and how to create an executable.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/#primaryimage\",\"url\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/emperor_kk_Discover_how_to_create_a_Python_script_that_optimi_297b6e39-fc9d-49e1-b334-7ba12a087dc5_11.png\",\"contentUrl\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/emperor_kk_Discover_how_to_create_a_Python_script_that_optimi_297b6e39-fc9d-49e1-b334-7ba12a087dc5_11.png\",\"width\":1024,\"height\":1024,\"caption\":\"Python Script\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/2025\\\/01\\\/04\\\/optimize-images-python-script\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating a Python Image Optimization Script\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/#website\",\"url\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/\",\"name\":\"kourentzes.com\\\/konstantinos\",\"description\":\"Konstantinos Kourentzes\",\"publisher\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/#\\\/schema\\\/person\\\/2693fb0ad7f7638a020431ffe372c822\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/#\\\/schema\\\/person\\\/2693fb0ad7f7638a020431ffe372c822\",\"name\":\"Konstantinos Kourentzes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/kko.png\",\"url\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/kko.png\",\"contentUrl\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/kko.png\",\"width\":2835,\"height\":2268,\"caption\":\"Konstantinos Kourentzes\"},\"logo\":{\"@id\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/kko.png\"},\"description\":\"Konstantinos Kourentzes is a distinguished technologist and Enterprise Resource Planning (ERP) consultant renowned for his expertise in delivering cutting-edge technology solutions. Based in Marousi, Greece, he has a knack for seamlessly integrating data-driven systems, empowering businesses to streamline their operations and achieve peak efficiency. A fervent proponent of innovation, Konstantinos is committed to instigating revolutionary shifts within organizations. His approach revolves around delivering custom-tailored ERP solutions that seamlessly align with each business's distinctive requirements. This catalyzes enduring collaborations rooted in unwavering trust and tangible outcomes. With a background rooted in technology and a passion for optimizing business processes, Konstantinos is your go-to partner for harnessing the power of ERP systems to unlock operational excellence. Connect with Konstantinos on LinkedIn to explore how his technological insights can drive your business to new heights.\",\"sameAs\":[\"https:\\\/\\\/kourentzes.com\\\/konstantinos\",\"https:\\\/\\\/x.com\\\/kkourentzes\"],\"url\":\"https:\\\/\\\/kourentzes.com\\\/konstantinos\\\/index.php\\\/author\\\/administrator\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating a Python Image Optimization Script - konstantinos.kourentzes.com","description":"Learn how to create a Python script to optimize PNG and WebP images for web use. Includes code, explanation, and how to create an executable.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/","og_locale":"en_US","og_type":"article","og_title":"Creating a Python Image Optimization Script - konstantinos.kourentzes.com","og_description":"Learn how to create a Python script to optimize PNG and WebP images for web use. Includes code, explanation, and how to create an executable.","og_url":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/","og_site_name":"konstantinos.kourentzes.com","article_published_time":"2025-01-04T21:02:04+00:00","og_image":[{"width":1024,"height":1024,"url":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2025\/01\/emperor_kk_Discover_how_to_create_a_Python_script_that_optimi_297b6e39-fc9d-49e1-b334-7ba12a087dc5_11.png","type":"image\/png"}],"author":"Konstantinos Kourentzes","twitter_card":"summary_large_image","twitter_creator":"@kkourentzes","twitter_site":"@kkourentzes","twitter_misc":{"Written by":"Konstantinos Kourentzes","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/#article","isPartOf":{"@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/"},"author":{"name":"Konstantinos Kourentzes","@id":"https:\/\/kourentzes.com\/konstantinos\/#\/schema\/person\/2693fb0ad7f7638a020431ffe372c822"},"headline":"Creating a Python Image Optimization Script","datePublished":"2025-01-04T21:02:04+00:00","mainEntityOfPage":{"@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/"},"wordCount":235,"commentCount":0,"publisher":{"@id":"https:\/\/kourentzes.com\/konstantinos\/#\/schema\/person\/2693fb0ad7f7638a020431ffe372c822"},"image":{"@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/#primaryimage"},"thumbnailUrl":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2025\/01\/emperor_kk_Discover_how_to_create_a_Python_script_that_optimi_297b6e39-fc9d-49e1-b334-7ba12a087dc5_11.png","keywords":["automation","def","executable creation","image compression","image optimization","image processing","png","pyinstaller","python","python tools","scripting","web development","webp"],"articleSection":["Code Snippets"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/","url":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/","name":"Creating a Python Image Optimization Script - konstantinos.kourentzes.com","isPartOf":{"@id":"https:\/\/kourentzes.com\/konstantinos\/#website"},"primaryImageOfPage":{"@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/#primaryimage"},"image":{"@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/#primaryimage"},"thumbnailUrl":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2025\/01\/emperor_kk_Discover_how_to_create_a_Python_script_that_optimi_297b6e39-fc9d-49e1-b334-7ba12a087dc5_11.png","datePublished":"2025-01-04T21:02:04+00:00","description":"Learn how to create a Python script to optimize PNG and WebP images for web use. Includes code, explanation, and how to create an executable.","breadcrumb":{"@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/#primaryimage","url":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2025\/01\/emperor_kk_Discover_how_to_create_a_Python_script_that_optimi_297b6e39-fc9d-49e1-b334-7ba12a087dc5_11.png","contentUrl":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2025\/01\/emperor_kk_Discover_how_to_create_a_Python_script_that_optimi_297b6e39-fc9d-49e1-b334-7ba12a087dc5_11.png","width":1024,"height":1024,"caption":"Python Script"},{"@type":"BreadcrumbList","@id":"https:\/\/kourentzes.com\/konstantinos\/index.php\/2025\/01\/04\/optimize-images-python-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kourentzes.com\/konstantinos\/"},{"@type":"ListItem","position":2,"name":"Creating a Python Image Optimization Script"}]},{"@type":"WebSite","@id":"https:\/\/kourentzes.com\/konstantinos\/#website","url":"https:\/\/kourentzes.com\/konstantinos\/","name":"kourentzes.com\/konstantinos","description":"Konstantinos Kourentzes","publisher":{"@id":"https:\/\/kourentzes.com\/konstantinos\/#\/schema\/person\/2693fb0ad7f7638a020431ffe372c822"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kourentzes.com\/konstantinos\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/kourentzes.com\/konstantinos\/#\/schema\/person\/2693fb0ad7f7638a020431ffe372c822","name":"Konstantinos Kourentzes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2022\/02\/kko.png","url":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2022\/02\/kko.png","contentUrl":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2022\/02\/kko.png","width":2835,"height":2268,"caption":"Konstantinos Kourentzes"},"logo":{"@id":"https:\/\/kourentzes.com\/konstantinos\/wp-content\/uploads\/2022\/02\/kko.png"},"description":"Konstantinos Kourentzes is a distinguished technologist and Enterprise Resource Planning (ERP) consultant renowned for his expertise in delivering cutting-edge technology solutions. Based in Marousi, Greece, he has a knack for seamlessly integrating data-driven systems, empowering businesses to streamline their operations and achieve peak efficiency. A fervent proponent of innovation, Konstantinos is committed to instigating revolutionary shifts within organizations. His approach revolves around delivering custom-tailored ERP solutions that seamlessly align with each business's distinctive requirements. This catalyzes enduring collaborations rooted in unwavering trust and tangible outcomes. With a background rooted in technology and a passion for optimizing business processes, Konstantinos is your go-to partner for harnessing the power of ERP systems to unlock operational excellence. Connect with Konstantinos on LinkedIn to explore how his technological insights can drive your business to new heights.","sameAs":["https:\/\/kourentzes.com\/konstantinos","https:\/\/x.com\/kkourentzes"],"url":"https:\/\/kourentzes.com\/konstantinos\/index.php\/author\/administrator\/"}]}},"_links":{"self":[{"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/posts\/937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/comments?post=937"}],"version-history":[{"count":1,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/posts\/937\/revisions"}],"predecessor-version":[{"id":939,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/posts\/937\/revisions\/939"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/media\/938"}],"wp:attachment":[{"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/media?parent=937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/categories?post=937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kourentzes.com\/konstantinos\/index.php\/wp-json\/wp\/v2\/tags?post=937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}