Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] The Power of Automation Using Python – Segregating Images Based on Dimensions

#1
The Power of Automation Using Python – Segregating Images Based on Dimensions

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;990756&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&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;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> <span class="kksr-muted">Rate this post</span> </div>
</p></div>
<h2>Project Description</h2>
<p>Recently, I was tasked with a manual project where my client had a directory where he had downloaded tons of wallpapers/images. Some of these were Desktop wallpapers, while the other images were mobile wallpapers. He wanted me to separate these images and store them in two separate folders and also name them in a serial order. </p>
<p>Well! The challenge here was – there were lots of images and separating them out by checking the dimension of each image individually and then copying them to separate folders was a tedious task. This is where I thought of automating the entire process without having to do anything manually. Not only would this save my time and energy, but it also eliminates/reduces the chances of errors while separating the images. </p>
<p>Thus, in this project, I will demonstrate how I segregated the images as Desktop and Mobile wallpapers and then renamed them – all using a single script! </p>
<p>Since the client data is confidential, I will be using my own set of images (10 images) which will be a blend of desktop and mobile wallpapers. So, this is how the directory containing the images looks –</p>
<figure class="wp-block-image size-large is-style-default"><img loading="lazy" decoding="async" width="1024" height="187" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-263-1024x187.png" alt="" class="wp-image-990774" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-263-1024x187.png 1024w, https://blog.finxter.com/wp-content/uplo...300x55.png 300w, https://blog.finxter.com/wp-content/uplo...68x140.png 768w, https://blog.finxter.com/wp-content/uplo...ge-263.png 1365w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>Note that it doesn’t matter how many images you have within the folder or in which order they are placed. The script can deal with any number of images. So, without further delay, let the game begin!</p>
<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" />
<h2><strong>Step 1: Import the Necessary Libraries and Create Two Separate Directories</strong></h2>
<p>Since we will be working with directories and image files, we will need the help of specific libraries that allow us to work on files and folders. Here’s the list of libraries that will aid us in doing so –</p>
<ul>
<li>The <code>Image</code> module from the <code>PIL</code> Library</li>
<li>The <code>glob</code> module</li>
<li>The <code>os</code> module</li>
<li>The <code>shutil</code> module</li>
</ul>
<p>You will soon find out the importance of each module used in our script. Let’s go ahead and import these modules in our script.</p>
<p><strong>Code:</strong></p>
<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
import glob
import os
import shutil</pre>
<p>Now that you have all the required libraries and modules at your disposal, your first task should be to create two separate folders – One to store the Desktop wallpapers and another to store the Mobile wallpapers. This can be done using the <code>makedirs</code> function of the <code>os</code> module.</p>
<p>The <strong>os.makedirs()</strong> method constructs a directory recursively. It takes the path as an input and creates the missing intermediate directories. We can even use the <code>os.makedirs</code> method to create a folder inside an empty folder. In this case, the path to the folder you want to create will be the only single argument to <code>os.makedirs()</code>.</p>
<p><strong>Code:</strong></p>
<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="">if not os.path.exists('Mobile Wallpapers'): os.makedirs('Mobile Wallpapers')
if not os.path.exists('Desktop Wallpapers'): os.makedirs('Desktop Wallpapers')</pre>
<p> Wonderful! This should create a couple of folders named – <em>‘Mobile Wallpapers’</em> and <em>‘Desktop Wallpapers’</em>. </p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4e2.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read: <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-create-a-nested-directory-in-python/" target="_blank">How to Create a Nested Directory in Python?</a></strong></p>
<h2><strong>Step 2: Segregating the Images</strong></h2>
<p>Now, in order to separate the images as mobile wallpapers and desktop wallpapers, we need to work with their dimensions.</p>
<p>Though the following piece of code wouldn’t be a part of our script but it can prove to be instrumental in finding the dimensions (height and width) of the images.</p>
<p><strong>Approach:</strong> </p>
<ul>
<li>Open all the image files present in the directory one by one. To open an image the <code>Image.open(filename)</code> function can be used and the image can be stored as an object. </li>
<li> Once you have the image object, you can extract the height and width of the image using the <code>height</code> and <code>width</code> properties.
<ul>
<li>In our case, each Desktop Wallpaper had a fixed width of 1920. This is going to be instrumental in our next steps to identify if an image is a desktop image or a mobile image. In other words, every image that has a width of 1920 will be a Desktop image and every other image will be a mobile image. This might vary in your case. Nevertheless, you will certainly find a defining width or height to distinguish between the types of images. </li>
</ul>
</li>
</ul>
<p><strong>Code:</strong></p>
<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="">for filename in glob.glob(r'.\Christmas\*.jpg'): im = Image.open(filename) print(f"{im.width}x{im.height}")</pre>
<p><strong>Output:</strong></p>
<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="">1920x1224
1920x1020
1920x1280
1920x1280
3264x4928
2848x4288
4000x6000
3290x5040
3278x4912
1920x1280</pre>
<p>There we go! It is evident that all the desktop images have a width of 1920. This was also a pre-defined condition which made things easier for me to separate out the images. </p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4e2.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Recommended Read: </strong><a href="https://blog.finxter.com/how-to-get-the-size-of-an-image-with-pil-in-python/"><strong>How to Get the Size of an Image with PIL in Python</strong></a></p>
<p>Once you know that the images with 1920 width are desktop images, you can simply use an if condition to check if the width property is equal to 1920 or not. If yes, then use the <code>shutil.copy</code> method to copy the file from its location into the previously created <code>Desktop Wallpapers</code> folder. Otherwise, copy the file to the <code>Mobile Wallpapers</code> folder. </p>
<p><strong>Code:</strong></p>
<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="">for filename in glob.glob(r'.\Christmas\*.jpg'): im = Image.open(filename) img_path = os.path.abspath(filename) if im.width == 1920: shutil.copy(img_path, r'.\Desktop Wallpapers') else: shutil.copy(img_path, r'.\Mobile Wallpapers')</pre>
<h2><strong>Step 3: Rename the Files Sequentially</strong></h2>
<p>All that remains to be done is to open the Desktop Wallpapers folder and Mobile Wallpapers folder and rename each image inside the respective folders sequentially. </p>
<p><strong>Approach:</strong></p>
<ul>
<li>Open the images in both the folders separately using the <code>glob</code> module.</li>
<li>Rename the images sequentially using <code>os.rename</code> method.</li>
<li>To maintain a sequence while naming the images you can use a counter variable and increment it after using it to name each image. </li>
</ul>
<p><strong>Code:</strong></p>
<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="">count = 1
for my_file in glob.glob(r'.\Desktop Wallpapers\*.jpg'): img_name = os.path.abspath(my_file) os.rename(img_name, r'.\Desktop Wallpapers\Desktop-img_'+str(count)+'.jpg') count += 1 flag = 1
for my_file in glob.glob(r'.\Mobile Wallpapers\*.jpg'): img_name = os.path.abspath(my_file) os.rename(img_name, r'.\Mobile Wallpapers\Mobile-img_'+str(flag)+'.jpg') flag += 1</pre>
<h2>Putting It All Together</h2>
<p>Finally, when you put everything together, this is how the complete script looks like –</p>
<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
import glob
import os
import shutil if not os.path.exists('Mobile Wallpapers'): os.makedirs('Mobile Wallpapers')
if not os.path.exists('Desktop Wallpapers'): os.makedirs('Desktop Wallpapers') for filename in glob.glob(r'.\Christmas\*.jpg'): im = Image.open(filename) img_path = os.path.abspath(filename) if im.width == 1920: shutil.copy(img_path, r'.\Desktop Wallpapers') else: shutil.copy(img_path, r'.\Mobile Wallpapers') count = 1
for my_file in glob.glob(r'.\Desktop Wallpapers\*.jpg'): img_name = os.path.abspath(my_file) os.rename(img_name, r'.\Desktop Wallpapers\Desktop-img_'+str(count)+'.jpg') count += 1 flag = 1
for my_file in glob.glob(r'.\Mobile Wallpapers\*.jpg'): img_name = os.path.abspath(my_file) os.rename(img_name, r'.\Mobile Wallpapers\Mobile-img_'+str(flag)+'.jpg') flag += 1 </pre>
<p>Note that the paths used in the above script are strictly limited to my system. In your case, please specify the path where you have stored the images. </p>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="281" height="307" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-264.png" alt="" class="wp-image-990861" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-264.png 281w, https://blog.finxter.com/wp-content/uplo...75x300.png 275w" sizes="(max-width: 281px) 100vw, 281px" /></figure>
<h2><strong>Summary</strong></h2>
<p>Thus, thirty lines of code can save you several hours of tedious manual work. This is how I completed my project and submitted the entire work to my happy client in a matter of one hour (even less). Now, I can download as many wallpapers as I want for my mobile and desktop screens and separate them sequentially in different directories using the same script. Isn’t that wonderful? </p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4e2.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Recommended Read: <a href="https://blog.finxter.com/list-all-files-of-a-directory-python/" target="_blank" rel="noreferrer noopener">How Do I List All Files of a Directory in Python?</a></strong></p>
</div>


https://www.sickgaming.net/blog/2022/12/...imensions/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016