{"id":429,"date":"2023-02-13T11:40:33","date_gmt":"2023-02-13T11:40:33","guid":{"rendered":"https:\/\/tinyytopic.com\/?p=429"},"modified":"2023-02-13T11:40:08","modified_gmt":"2023-02-13T11:40:08","slug":"convert-an-image-size-using-python-ready-to-use-function","status":"publish","type":"post","link":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/","title":{"rendered":"Convert an image size using Python ready-to-use function"},"content":{"rendered":"\n<div class=\"wp-block-uagb-advanced-heading uagb-block-80d2bcc2\"><h5 class=\"uagb-heading-text\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><br>How to convert or scale an image size using Python?<\/mark><\/h5><\/div>\n\n\n\n<p style=\"font-size:15px\">Install the following modules if you haven&#8217;t installed them already:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install pillow<\/code><\/pre>\n\n\n\n<p style=\"font-size:15px\">Ready to use Python function to convert or scale or re-size an image using Pytho ready-to-use function:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def image_scale(ImageFile, ScalePercent):\n    # Open the image file\n    img = Image.open(ImageFile)\n\n    # Get the original size of the image\n    width, height = img.size\n\n    # Scale the image to 50% of its original size\n    new_width = int(width * ScalePercent\/100)\n    new_height = int(height * ScalePercent\/100)\n\n    # Resize the image to the new size\n    new_img = img.resize((new_width, new_height))\n\n    # Save the resized image\n    new_img.save(\"scaled_image.jpg\")<\/pre>\n\n\n\n<p style=\"font-size:15px\">Write your main code as a sample below,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from PIL import Image\n\nimage_scale(\"flower.jpg\", 200)<\/code><\/pre>\n\n\n\n<p style=\"font-size:15px\">The output of the code is,<\/p>\n\n\n\n<div class=\"wp-block-uagb-image uagb-block-fe301113 wp-block-uagb-image--layout-default wp-block-uagb-image--effect-static wp-block-uagb-image--align-none\"><figure class=\"wp-block-uagb-image__figure\"><img decoding=\"async\" srcset=\"https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/flower.jpg \" src=\"https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/flower.jpg\" alt=\"\" class=\"uag-image-430\" width=\"\" height=\"\" title=\"\" loading=\"lazy\"\/><figcaption class=\"uagb-image-caption\">Original Image<\/figcaption><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"404\" src=\"https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/scaled_image.jpg\" alt=\"\" class=\"wp-image-431\" srcset=\"https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/scaled_image.jpg 728w, https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/scaled_image-300x166.jpg 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><figcaption class=\"wp-element-caption\">Scaled Image to 200%<\/figcaption><\/figure>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-d903c0fa\"><h5 class=\"uagb-heading-text\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">How does the function work?<\/mark><\/h5><\/div>\n\n\n\n<p style=\"font-size:15px\">This code is written in Python and uses the <code>Pillow<\/code> library to resize an image. In short, this code takes an image file and a scale percent as input, resizes the image to the specified percentage of its original size, and saves the resized image to a file. Here&#8217;s a step-by-step explanation of how it works:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Import the <code>Image<\/code> module from the <code>Pillow<\/code> library<\/li>\n\n\n\n<li>Define a function named <code>image_scale<\/code> which takes two arguments: <code>ImageFile<\/code> and <code>ScalePercent<\/code>.<\/li>\n\n\n\n<li>The function opens an image file using the <code>Image.open<\/code> method and stores the image in the <code>img<\/code> variable.<\/li>\n\n\n\n<li>The size of the original image is obtained using the <code>img.size<\/code> method, which returns a tuple of the width and height of the image.<\/li>\n\n\n\n<li>The new width and height of the image are calculated by multiplying the original width and height by the <code>ScalePercent<\/code> divided by 100.<\/li>\n\n\n\n<li>The <code>resize<\/code> method of the <code>img<\/code> object is used to resize the image to the new size, which is stored in the <code>new_img<\/code> variable.<\/li>\n\n\n\n<li>Finally, the resized image is saved to a file named &#8220;scaled_image.jpg&#8221; using the <code>save<\/code> method.<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Install the following modules if you haven&#8217;t installed them already: Ready to use Python function to convert or scale or re-size an image using Pytho ready-to-use function: Write your main code as a sample below, The output of the code is, This code is written in Python and uses the Pillow library to resize an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12,17],"tags":[23,14,13,16,15,22],"class_list":["post-429","post","type-post","status-publish","format-standard","hentry","category-python","category-useful-function","tag-convert-image-size","tag-programming-language","tag-python","tag-python-code","tag-python-sample-code","tag-scale-image"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Convert an image size using Python ready-to-use function - tinyytopic.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Convert an image size using Python ready-to-use function - tinyytopic.com\" \/>\n<meta property=\"og:description\" content=\"Install the following modules if you haven&#8217;t installed them already: Ready to use Python function to convert or scale or re-size an image using Pytho ready-to-use function: Write your main code as a sample below, The output of the code is, This code is written in Python and uses the Pillow library to resize an [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/\" \/>\n<meta property=\"og:site_name\" content=\"tinyytopic.com\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-13T11:40:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-13T11:40:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/flower.jpg\" \/>\n<meta name=\"author\" content=\"tinyytopic.com\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"tinyytopic.com\" \/>\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\":\"WebPage\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/\",\"url\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/\",\"name\":\"Convert an image size using Python ready-to-use function - tinyytopic.com\",\"isPartOf\":{\"@id\":\"https:\/\/tinyytopic.com\/#website\"},\"datePublished\":\"2023-02-13T11:40:33+00:00\",\"dateModified\":\"2023-02-13T11:40:08+00:00\",\"author\":{\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\"},\"breadcrumb\":{\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tinyytopic.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Convert an image size using Python ready-to-use function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tinyytopic.com\/#website\",\"url\":\"https:\/\/tinyytopic.com\/\",\"name\":\"tinyytopic.com\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tinyytopic.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\",\"name\":\"tinyytopic.com\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"caption\":\"tinyytopic.com\"},\"sameAs\":[\"http:\/\/tinyytopic.com\"],\"url\":\"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Convert an image size using Python ready-to-use function - tinyytopic.com","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:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/","og_locale":"en_US","og_type":"article","og_title":"Convert an image size using Python ready-to-use function - tinyytopic.com","og_description":"Install the following modules if you haven&#8217;t installed them already: Ready to use Python function to convert or scale or re-size an image using Pytho ready-to-use function: Write your main code as a sample below, The output of the code is, This code is written in Python and uses the Pillow library to resize an [&hellip;]","og_url":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/","og_site_name":"tinyytopic.com","article_published_time":"2023-02-13T11:40:33+00:00","article_modified_time":"2023-02-13T11:40:08+00:00","og_image":[{"url":"https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/flower.jpg"}],"author":"tinyytopic.com","twitter_card":"summary_large_image","twitter_misc":{"Written by":"tinyytopic.com","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/","url":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/","name":"Convert an image size using Python ready-to-use function - tinyytopic.com","isPartOf":{"@id":"https:\/\/tinyytopic.com\/#website"},"datePublished":"2023-02-13T11:40:33+00:00","dateModified":"2023-02-13T11:40:08+00:00","author":{"@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb"},"breadcrumb":{"@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/convert-an-image-size-using-python-ready-to-use-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tinyytopic.com\/"},{"@type":"ListItem","position":2,"name":"Convert an image size using Python ready-to-use function"}]},{"@type":"WebSite","@id":"https:\/\/tinyytopic.com\/#website","url":"https:\/\/tinyytopic.com\/","name":"tinyytopic.com","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tinyytopic.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb","name":"tinyytopic.com","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","caption":"tinyytopic.com"},"sameAs":["http:\/\/tinyytopic.com"],"url":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"}]}},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"tinyytopic.com","author_link":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"},"uagb_comment_info":103,"uagb_excerpt":"Install the following modules if you haven&#8217;t installed them already: Ready to use Python function to convert or scale or re-size an image using Pytho ready-to-use function: Write your main code as a sample below, The output of the code is, This code is written in Python and uses the Pillow library to resize an&hellip;","_links":{"self":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/comments?post=429"}],"version-history":[{"count":4,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/429\/revisions"}],"predecessor-version":[{"id":435,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/429\/revisions\/435"}],"wp:attachment":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/media?parent=429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/categories?post=429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/tags?post=429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}