nav2

Wednesday, April 2, 2014

Image scripts - useful batch operations - image utilities - unpack gifs, mass resize images with a single click and other useful operations

I have been a linux user for more than 10 years now. There is a rant about it from a while ago.
Over the years, I had to use the terminal, had to edit config files, compile and package software. It was one of the "joys" of the platform.
So while I can't call myself an experienced programmer, I had to learn a little bit about scripting and debugging. Today if you are a Ubuntu user, you absolutely dont need to know anything about linux in order to use it happily. But Ubuntu and debian are such a drag. They might have all the packages and resources in the world, but they are no fun any more. I am a manjaro user myself, but these tricks should work for you on any distro that has the required utilities.

So without further adieu, in this nifty trick, I am going to show you how to configure the "Thunar" file manager to acquire basic image conversion and editing capabilities. If you are not a thunar user, there are alternatives to do this.

Unpack Gif action
What does it do: It lets you extract all the frames in an animated gif file and make a png image sequence out of them.


1. Open thunar. and then Edit-configure custom actions
2. In the dialog , click on the plus (+) icon to create a new one.
3. Call it "unpack gif" or whatever you like.
In the command box type this:

If you are wondering how it works, the convert operator is a part of "imagemagick" - an image processing library that most linux distros have. If you dont have it, jut install it. -coalesce is an option specific for gif animated files. %n  is explained in the custom actions editing window

4. In the "appearance conditions" tab, specify the conditions:

Dont forget to tick the "images" box only.
This will tell the "unpack gif" menu entry to appear in the right click menu only when a gif file has been selected.

5. Now save it and test it. Right click on an animated gif file and select "unpack gif" :)

We can now unpack animated gif files from the web!! If you are a programmer and you need some placeholder graphics for your game, this script is already very useful to you!
Most game engines do not accept gif files, and take png image sequences instead. You might of course also need a sprite sheet packer.

Now lets say that you are not happy with the size of the image sequence. You would like to shrink all of the selected png files by 50% or 100%. How do we do that without wasting 10 minutes of our lives?

Pack a Gif action
The drill is the same, only the command is slightly different:

This will create a looping animated gif out of all the png images in a directory where you execute the command. It will name the animated gif with the name of the frame you selected.

Resize PNG images action
This one is just like the previous, but it has a number of extra steps before adding the custom action. This time we are first going to create our own bash script to use as an operator.
0. Create an empty bash script file somewhere. Call it
"resizeImagesScript.sh"
Don't forget to make it an executable. And finally open it with your favorite text editor.
Then paste this:

Save it.

1. Open thunar. and then Edit-configure custom actions
2. In the dialog , click on the plus (+) icon to create a new one.
3. Call it "resize overwrite /2"
In the command box type this:

4. In the "appearance conditions" tab, specify the conditions:

Dont forget to tick the "images" box only.
This will tell the "resize overwrite /2" menu entry to appear in the right click menu only when an image file has been selected.

5. Now save it and test it. Right click on multiple image files and select "resize overwrite /2" :)

it should make all of the selected images 2 times smaller than their original size.

6.If you would like to alter this script or create more custom actions in thunar's right click menu, you absolutely can.
-The "50" in our example from step 3 is the new size in %. It is telling the script to shrink the images to 50% of their original size. If you would like to shrink images by smaller increments, you can try with 75 instead. This will make it 1.5x smaller instead of 2x.

-Alternatively you can make selected images bigger! If you use 150 in the command box, it will multiply the size by 1.5. If you use 200, it will make it double the original size.

-Also if you dont like that the script is overwriting the images, you can check out the last commented line in it.
Copy

and paste it in the place of

in the sixth line of code.

Now you can unpack any animated gif file into an image sequence. Then shrink or grow the size of the image sequence sprite. The beauty of this solution is that you dont have to open any software, then open files and waste time importing and exporting. All you do is right click on the file(s).

Finally if you are wondering, where do I find animated gif files to use as placeholder sprites in my game?
Let me give you a few hints. :P

Some more cheat codes:

Extract Alpha channel mask from PNG (useful for 3d artists)


Extract layers from SVG

and in the script:



Trim images by their alpha channel:

Via imagemagick. This is useful when you want to batch auto crop a lot of body part images for a 2d character rig that needs to be animated as a cutout animation in some game engine.

Create a picture Atlas from all the png images in the directory:

Useful when you want to pack all of your images into a single image file.

Note that all of these commands work on windows and mac as well - with some alteration in some cases.

No comments:

Post a Comment