Resize all the images in directory.

2010-10-04 2 min read bash Linux Photo
Geotagger "Solmeta N2 Kompass" for N...
  <dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">
    Image via <a href="http://commons.wikipedia.org/wiki/File:Solmeta_N2_auf_Nikon_D5000_Rueckansicht.JPG">Wikipedia</a>
  </dd>
</dl>

When I take a photo with my DSLR Nikon D5000, the size of the image comes to around 5MB and I know that with my point and shoot camera also it does not come to any lesser than 3MB. Definitely this is quite good to keep the images in the same size for use but when it comes to on-line sharing I don’t think this is the size that is required. I guess mosr of the time 1024×768 is sufficient. Even if you are sending the images in email, you cannot afford to send a lot of images of this size even with a gmail account.

So, what do you do? Again you cannot open gimp or any such program to modify couple of photos and spend couple of hours in modifying the files before sharing or sending it in email. So, if you are on linux (which you are, if you are reading this blog entry), then here’s a simple bash script that you can use to resize all your images.

  <td>
    <div class="bash codecolorer">
      <span class="re2">size</span>=<span class="st0">"<a class="</span>zem_slink<span class="st0">" title="</span>Display resolution<span class="st0">" rel="</span>wikipedia<span class="st0">" href="</span>http:<span class="sy0">//</span>en.wikipedia.org<span class="sy0">/</span>wiki<span class="sy0">/</span>Display_resolution<span class="st0">">640x480</a>"</span><br /> <br /> <span class="re2">prefix</span>=<span class="st0">"img-"</span><br /> <br /> <span class="kw1">for</span> i <span class="kw1">in</span> <span class="sy0">*</span>;<br /> <span class="kw1">do</span><br /> <span class="re2">t</span>=<span class="sy0">`</span><span class="kw2">file</span> <span class="re1">$i</span><span class="sy0">`</span><br /> <span class="kw1">if</span> <span class="br0">&#91;</span><span class="br0">&#91;</span> <span class="st0">"<span class="es2">$t</span>"</span> =~ <span class="st_h">'image'</span> <span class="br0">&#93;</span><span class="br0">&#93;</span><br /> <span class="kw1">then</span><br /> convert <span class="re5">-resize</span> <span class="re1">$size</span> <span class="re1">$i</span> <span class="re1">$prefix</span><span class="re1">$i</span><br /> <span class="kw3">echo</span> <span class="st0">"<span class="es2">$i</span> resized"</span>;<br /> <span class="kw1">fi</span><br /> <span class="kw1">done</span>
    </div>
  </td>
</tr>
1
2
3
4
5
6
7
8
9
10
11
12
13

Note: You will not get the exact size of image as specified in the size option. For example if you specify 640×640 still you would get 640×480 depending on your photos aspect ratio. Also, you can specify the size in percentage like “50%” 🙂

Enhanced by Zemanta
comments powered by Disqus