{"id":124787,"date":"2022-05-13T09:59:22","date_gmt":"2022-05-13T09:59:22","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=362009"},"modified":"2022-05-13T09:59:22","modified_gmt":"2022-05-13T09:59:22","slug":"how-to-get-the-size-of-an-image-with-pil-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/05\/13\/how-to-get-the-size-of-an-image-with-pil-in-python\/","title":{"rendered":"How to Get the Size of an Image with PIL in Python"},"content":{"rendered":"<div class=\"kk-star-ratings kksr-valign-top kksr-align-left \" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;362009&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;count&quot;:&quot;0&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\"> <span class=\"kksr-muted\">Rate this post<\/span> <\/div>\n<\/div>\n<h2><strong>Problem Formulation<\/strong><\/h2>\n<p><strong>Given an image, how to get the size of the image with PIL or any other Python library?<\/strong><\/p>\n<p>Getting the size of an image with Python PIL(Python Image Library) basically implies that you want to get its height and width in pixels. <\/p>\n<p><strong>Example:<\/strong> Consider the following image:<\/p>\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"604\" height=\"396\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/05\/image-153.png\" alt=\"\" class=\"wp-image-362174\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/05\/image-153.png 604w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/05\/image-153-300x197.png 300w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/figure>\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/05\/image.jpg\" class=\"wp-block-file__button\" download aria-describedby=\"wp-block-file--media-a63084d8-f30a-43ff-8494-6f90888c1b37\"><strong>Download Image Here<\/strong><\/a><\/div>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.1.0\/72x72\/1f525.png\" alt=\"\ud83d\udd25\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Challenge: <\/strong>Get the size of the above image with PIL or any other Python library.<\/p>\n<h2><strong>Method 1: <\/strong>Using img.size<\/h2>\n<p class=\"has-background\" style=\"background-color:#dff2fc\"><strong>Approach:<\/strong> To get the size of the image:<br \/>\u27a2\u00a0open it up by calling the PIL function <code>Image.open('image file path')<\/code>.<br \/>\u27a2\u00a0Then use the PIL property <code>image.size<\/code> on the opened image. This returns a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" target=\"_blank\"><strong>tuple<\/strong><\/a> containing the width and height of the image in pixels.<br \/>\u27a2\u00a0Unpack the tuple in two different variables to store the width and height individually.<\/p>\n<p><strong>code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from PIL import Image\nimg = Image.open(\"image.jpg\")\nwidth, height = img.size\nprint(f'width: {width}\\nheight: {height}')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">width: 640\nheight: 426<\/pre>\n<p><strong>#Alternative Formulation<\/strong><\/p>\n<p>Instead of using the img.size property, you can use the <strong>img.width <\/strong>and <strong>img.height <\/strong>properties to store\/display the height and width of the image separately.<\/p>\n<p>Here&#8217;s the code that demonstrates how you can use the width and height properties of the PIL library.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3,4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from PIL import Image\nimg = Image.open(\"image.jpg\")\nwidth = img.width\nheight = img.height\nprint(f'width: {width}\\nheight: {height}')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">width: 640\nheight: 426<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.1.0\/72x72\/1f5e8.png\" alt=\"\ud83d\udde8\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>TRIVIA: <\/strong><em>PIL<\/em>(Python Imaging Library) is a Python library that is used for image processing. It supports numerous image formats which include \u201c<code>jpeg<\/code>\u201d, \u201c<code>png<\/code>\u201d, \u201c<code>ppm<\/code>\u201d, \u201c<code>tiff<\/code>\u201d, \u201c<code>bmp<\/code>\u201d, \u201c<code>gif<\/code>\u201d, etc. It is an extremely useful library that provides a plethora of image editing capabilities like getting the size of an image, cropping an image, etc. The <code>Image<\/code> module of the PIL library is used to represent the\u00a0<em>PIL<\/em>\u00a0images.\u00a0<\/p>\n<ul>\n<li>To install the PIL, execute the following command in your terminal: <code>pip install pillow <\/code><\/li>\n<\/ul>\n<h2><strong>Method 2: Get Image Size Using Open-CV<\/strong><\/h2>\n<p class=\"has-background\" style=\"background-color:#dff2f2\"><strong>Approach: <\/strong>To get the size of an image using <em>OpenC<\/em>V:<br \/>\u27a2 Load the image from the specified path by calling OpenCV function <strong><code>imread('file_path')<\/code><\/strong>.<br \/>\u27a2 Use the <strong>.shape<\/strong> property on this image. This returns a tuple consisting of the height, width in pixels, and the number of channels of the image.<br \/>\u27a2 Unpack these values individually in three different variables and display the output accordingly.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import cv2\nimage = cv2.imread('image.jpg')\nheight, width, channel = image.shape\nprint(f\"width:{width}\\nheight:{height}\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">width: 640\nheight: 426<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.1.0\/72x72\/270f.png\" alt=\"\u270f\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Note: <\/strong>Use the following command to install OpenCV: <code>pip install opencv-python<\/code><\/p>\n<h2><strong>Method 3:<\/strong> Get the Image Size Using Pygame<\/h2>\n<ul class=\"has-background\" style=\"background-color:#dff2f2\">\n<li><strong>Approach: <\/strong>\n<ul>\n<li>Load the image from its path using the pygame function <code>pygame.image.load('filepath')<\/code>.<\/li>\n<li>Use the methods <code>img.get_width()<\/code> and <code>img.get_height()<\/code> to get the image width and height in pixels.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2-4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame\nimg = pygame.image.load('image.jpg')\nwidth = img.get_width()\nheight = img.get_height()\nprint(\"width: \", width)\nprint(\"height: \", height)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pygame 2.1.2 (SDL 2.0.18, Python 3.9.10)\nHello from the pygame community. https:\/\/www.pygame.org\/contribute.html\nwidth: 640\nheight: 426<\/pre>\n<p><strong><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.1.0\/72x72\/1f5e8.png\" alt=\"\ud83d\udde8\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>TRIVIA: <\/strong><code>pygame<\/code>\u00a0is a Python wrapper for the\u00a0<a href=\"https:\/\/www.libsdl.org\/\">SDL library<\/a>\u00a0(<strong>Simple DirectMedia Layer)<\/strong>. SDL allows cross-platform access to the system\u2019s underlying multimedia hardware components, such as video, sound, keyboard, mouse, and joystick. Since both SDL and\u00a0<code>pygame<\/code> facilitate us with a cross-platform nature, we can write game programs and create rich multimedia Python programs for almost every platform that is supported by them.<\/p>\n<p>To install\u00a0<code>pygame<\/code>, use the following pip command on your terminal: <code>pip install pygame<\/code><\/p>\n<h2>\u25a3 How to Get Image Size in Bytes?<\/h2>\n<p>Previously, we found out the size of the image, that is, the image dimensions (width and height). However, you may also need to find out the file size in bytes to decide how to use the image file. Hence, let us dive into the methods that will help us to find the image size in <strong>bytes<\/strong>.<\/p>\n<h3>\u25e9 <strong>Using <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/stat.html\" target=\"_blank\">os.stat<\/a><\/strong><\/h3>\n<p><strong>Approach: <\/strong>Call the <code>os.stat()<\/code> method on the image file and then use the <code>st_size<\/code> property upon this image to get its size in bytes.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nprint(os.stat('image.jpg').st_size) # 48297<\/pre>\n<h3><strong>\u25e9 Using os.path.getsize<\/strong><\/h3>\n<p><strong>Approach:<\/strong> <code>os.path.getsize()<\/code> is a method of the os module that is used to get the size of a specified path. Pass the image path to this function to get the size of the image file in bytes.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nprint(os.path.getsize('image.jpg')) # 48297<\/pre>\n<h3><strong>\u25e9 Using PIL<\/strong><\/h3>\n<p><strong>Approach: <\/strong>Call the PIL function <code>Image.open()<\/code> to open the image and then use the <code>len()<\/code> method to find its length after reading the image. <\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from PIL import Image\nimg = Image.open('image.jpg')\nprint(\"File Size In Bytes:- \"+str(len(img.fp.read())))<\/pre>\n<h2><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.1.0\/72x72\/1f381.png\" alt=\"\ud83c\udf81\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Bonus: Get the Size of an Image by Loading Image from an URL<\/strong><\/h2>\n<p>The following code demonstrates how you can load an image from an URL and then get its size.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import requests\nfrom PIL import Image\nfrom io import BytesIO\nheader = {'User-Agent': 'Mozilla\/5.0 (X11; CrOS x86_64 14588.98.0) AppleWebKit\/537.36 (KHTML, like Gecko) ' 'Chrome\/101.0.4951.59 Safari\/537.36'}\nres = requests.get(\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/05\/sample_img.png\", headers=header)\n# create image from binary content\nimg = Image.open(BytesIO(res.content))\nwidth, height = img.size\nprint(f'width:{width}, height:{height}')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">width:867, height:489<\/pre>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>We successfully unearthed the answer to numerous questions, including how to use different libraries to find the image size &#8211; dimensions as well as the size in bytes. I hope this tutorial helped you. Please <strong><a href=\"https:\/\/blog.finxter.com\/subscribe\/\" target=\"_blank\" rel=\"noreferrer noopener\">subscribe<\/a><\/strong> and stay tuned for more interesting tutorials. <\/p>\n<ul class=\"has-base-background-color has-background\">\n<li><strong>Recommended Tutorials<\/strong>\n<ul>\n<li><strong><a href=\"https:\/\/blog.finxter.com\/how-to-crop-an-image-using-pil\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Crop an Image Using PIL?<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/blog.finxter.com\/how-to-crop-an-image-using-opencv\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Crop an Image Using OpenCV?<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/blog.finxter.com\/five-useful-image-processing-techniques-in-python-using-opencv\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python OpenCV Image Processing \u2013 Resize, Blend, Blur, Threshold, Convert<\/a><\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Happy learning!<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.1.0\/72x72\/1f60a.png\" alt=\"\ud83d\ude0a\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<hr class=\"wp-block-separator\" \/>\n<ul>\n<li>One of the most sought-after skills on Fiverr and Upwork is&nbsp;<strong>web scraping<\/strong>. Make no mistake:&nbsp;<em><strong>extracting data programmatically from websites&nbsp;<\/strong><\/em>is a critical life skill in today\u2019s world that\u2019s shaped by the web and remote work.<\/li>\n<li>So, do you want to master the art of web scraping using Python\u2019s BeautifulSoup?<\/li>\n<li>If the answer is yes \u2013 this course will take you from beginner to expert in Web Scraping.<\/li>\n<\/ul>\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/academy.finxter.com\/university\/web-scraping-with-beautifulsoup\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" loading=\"lazy\" width=\"480\" height=\"360\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/06\/scrape_bs4.png\" alt=\"\" class=\"wp-image-32250\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/06\/scrape_bs4.png 480w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/06\/scrape_bs4-300x225.png 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><figcaption><strong><a href=\"https:\/\/academy.finxter.com\/university\/web-scraping-with-beautifulsoup\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the Web Scraping with BeautifulSoup Masterclass<\/a><\/strong>&nbsp;now, and master it by tomorrow!<\/figcaption><\/figure>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Rate this post Problem Formulation Given an image, how to get the size of the image with PIL or any other Python library? Getting the size of an image with Python PIL(Python Image Library) basically implies that you want to get its height and width in pixels. Example: Consider the following image: Download Image Here [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-124787","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/124787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=124787"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/124787\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=124787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=124787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=124787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}