On Making GIFs and Scripting

Jan 16, 2013 10:38


What this is not: a tutorial on how to make GIFs, a tutorial on how to make GIFs pretty

What this is: something that might make your GIF-making process a little smoother

Written for: Photoshop, people with previous GIF-ing experience, people who have little to no programming experience, and people who don't mind reading text-heavy monologues

Warning: largeish GIFs


The Advantages of Scripting

I've seen some GIF tutorials floating around Tumblr and I've come to the conclusion that not a lot of people know about or bother with Photoshop Actions and even fewer people deal with scripts. And the few tutorials I see that deal with scripts, none of them really explain what's going on. It's a shame, really. While Photoshop's Extendscript does have certain annoying limitations, it is a powerful tool (given that you know how to program).

Scripting allows you to do away with repetitive aspects of GIF making. What I used to do with god-knows-how-many mouse clicks, I can now achieve with a single shortcut, leaving me more time to play with a GIF's coloring and other such concerns.

Life Without Scripts (and Actions)

Let's begin at the point where all the images you need for your GIF have already been loaded into a layer stack.

What I find the most tedious in making GIFs is the pre-processing of the image layers and then the animation of said layers. I usually go for a simple Duplicate Layer + Blur + Screen (50%) + Merge + Sharpen to brighten and sharpen the picture up a bit. After I've done that particular set of tasks for each layer, I proceed to animate the GIF.



Imagine doing this for more than 30 layers! Ugh, talk about tiring, right?

Here's where Photoshop Actions and scripts get really useful.

Photoshop Actions

An Action is basically a record of all the things you do on Photoshop from the moment you start recording to the moment you press the stop recording button. Once you've recorded the Action, you can replay it to create the same effect you just made on one image.

To create an Action, first fire up the Actions window (Window > Actions). The figure below shows you the different parts of the Action tab.



An action set allows you to better organize your Actions. It's up to you whether or not to create one; in any case, you should take note of which set your Action is to be found.

Once you click the Create New Action button, the Record button will turn red, meaning all your activities on Photoshop henceforth will be recorded. When you've finished, press the Stop button to complete creating the Action.

So suppose we've already recorded the tasks I outlined earlier and we named it Preprocessing and placed it in a set called My Action Set. Instead of clicking each layer and clicking all those buttons, we now just click one button to play Preprocessing. This is good; we're getting more efficient! :D



But unfortunately, it's not efficient enough for my lazy ass. I still have to click each layer and play the Action over and over and over again.

Now, this is why it's pretty useful to have some knowledge of programming. Programming and laziness practically go hand in hand, ngl. Besides, it's the age of the geek, baby. Computers are faster and more powerful than ever, it'd be a shame to let that go to waste.

Photoshop Scripts

So... scripts. If you've been making GIFs in Photoshop before this, you've already used a script yourself: the 'Load Files into Stack' script. A script is basically a program, a sequence of instructions that is designed to automate tasks for the user. (Note: There is a difference between a script and a program, academically speaking. Or at least there was. But that's not within the scope of this tutorial.)

Photoshop supports three scripting languages: AppleScript, JavaScript, and VBScript. We'll be using JavaScript here. Scripts can be stored anywhere in your machine and can be loaded via menu (File > Scripts > Browse). If you want your script to be available when you fire up the Scripts submenu, just like how the Load Files into Stack script is always available, you have to place it in your Adobe/Adobe Photoshop CSx/Presets/Scripts/ folder.

Our First Script

Here's our first script: (Copy and paste this into a text editor, name it anything you want, and save the file with a .jsx extension.)

var doc = activeDocument;
for (var i = 0; i < doc.layers.length; i++) {
doc.activeLayer = doc.layers[i];
app.doAction('Preprocessing', 'My Action Set');
}

Daunting? Fear not. It's really quite simple what this does.

The first line var doc = activeDocument tells the script that we will be working on the document that is open and active in Photoshop. (So, what happens if we don't have anything open in Photoshop?) Once assigned, the variable doc will contain all sorts of information pertinent to the document, such as its height, width, layers, name, etc.

The for (var i = 0; i < doc.layers.length; i++) line is called a for loop. It's a statement that allows code enclosed within the {} that succeed it to be repeatedly executed. It consists of three things: 
a. variable initialization (eg. var i = 0)
b. condition (eg. i < doc.layers.length)
c. variable update (eg. i++)

The variable i is a placeholder, it's like the number you keep in your head when you count sheep in an attempt to go to sleep at night. Its initial value is set to zero (var i = 0).

The condition constrains the loop to repeat statements for so long as i is less than doc.layers.length, which is Javascript for "the number of layers in the document." Until that number is reached i will be incremented by 1 (that's what i++ does).

The lines enclosed within the {} are the instructions to be executed until the loop has finished counting from zero to doc.layers.length. The first statement doc.activeLayer = doc.layers[i], tells Photoshop which layer of doc to make active (this is equivalent to you clicking on the layer). The layer it's going to activate is dictated by the value of i.

Wait a minute, you might be saying, i is set to zero! Shouldn't it be set to 1 to access the first layer?

Answer: Good catch! Programmers start counting from zero. The first item in a set will almost always be considered the zeroth item. (Almost always because some languages, like MATLAB, actually start with one.) This is called zero indexing if you're interested in Googling it.

The next line app.doAction('Preprocessing', 'My Action Set'), tells the app, which is Photoshop in our case, to perform an Action. The Action it's going to perform is 'Preprocessing,' which is found in the 'My Action Set' set.

Recall that earlier I made an Action of the same name and placed it in My Action Set. If the Action you made goes by a completely different name and is under a completely different action set, replace the names in the text file accordingly. Don't forget to enclose the names with quotes!

Now that we're done with that, let's see what the script does. :D

Our Script in Action



Look ma, no hands! My cursor is dancing around the screen like that just to show you can go do something else while your script does the boring work for you. Also, in case you're wondering, the picture doesn't update until the script is done. This might just be specific to my relatively slow processor though.

Improving Our Script

Now that we've sharpened all the images, we can start animating. There are a variety of methods out there, but I like the Make Frames From Layers + Reverse Frames + Select All Frames + Set Delay approach.



This is pretty repetitive too, to be honest. And what do we do when things are repetitive? That's right, we delegate the task to our computer.

Create an Action that animates the GIF for you. I named mine "Create GIF."

Here's our new script:

var doc = activeDocument;
for (var i = 0; i < doc.layers.length; i++) {
doc.activeLayer = doc.layers[i];
app.doAction('Preprocessing', 'My Action Set');
}
app.doAction('Create GIF', 'My Action Set');

The statements in a script are executed sequentially. That is, what comes first is executed first, and so on. That's why the line app.doAction('Create GIF', 'My Action Set') is located after the for loop. We only want to animate the GIF when all the layers have already been brightened and sharpened.

That's pretty much it! I hope it wasn't too hard to follow; I know I tend to infodump. Load a few images and test your script out, and know that Eliot Spencer is proud of you for trying this out. :)



(Hardison's been teaching him this shit, okay? It isn't fair he's the only one who gets to suffer. /Leverage)

Questions You Might Ask

1. You mentioned using a shortcut in the introduction...?
Yep! Assuming you've placed your script in you Presets/Scripts/ older, you can go t Edit > Keyboard Shortcuts and Menus, look for your script and then assign a shortcut to it. As you can probably surmise, you can assign a shortcut to any menu item this way. :)

2. So what does happen when you don't have a document open in Photoshop and you run the script?
It vomits an error message that might be incomprehensible, but just basically means, "I can't find an active document, maybe there's something wrong with your code in line x?" When your script does this, it effectively stops. Anything else you might want it to do, such as count from 1 to 100 in multiples of three, will not be executed. You can fix this by introducing a try-catch statement, especially if your script has grown and you don't want to stop your operations just because of one measly error.

(Granted, not having a document to operate on is not a measly error, but this goes for all other errors that occur when the script is running.)

3. So what else can I do with scripting?
A lot! It really depends on you. You can make your script more intelligent by letting it resize your image when it senses it's bigger than 500px, for example. Or you could give yourself more freedom by letting it ask you whether or not you want to resize it and to what size. You can also let your script automatically save a GIF.

You can even create a user interface like the one below that asks you what custom-made filters you want to apply on your GIF.



Here are some of the things you can't do purely just by scripting (read: not using Actions to help you out) though: 
- applying Stroke to a text layer
- manipulating animation frames (as of CS5 anyway... there might be support for this in CS6)
- applying adjustment layers (oh, well, yo can ut it requires some solid dedication to wade through existing and complicated scripts to find out how-you're better off creating Actions and applying them... this does mean that you can't tweak the parameters of the adjustment layer within the script though)

4. This is fun stuff! Where can I learn more?
You might want to start learning Javascript basics before you jump into learning more Photoshop scripting. Here's a nice tutorial to get you started on scripting for Photoshop.

If you want the documentation for Photoshop scripting from Adobe, they can be found here.

5. What version of Photoshop do you use?
CS5.

This has also been posted on Tumblr.

animation: miscellaneous, animation: animated gifs

Previous post Next post
Up