fogbound.net




Sun, 25 Sep 2011

Photoshop scripting with Javascript

— SjG @ 6:41 pm

I’ve played with the Javascript interface to Photoshop for a couple of years. Conceptually, it’s great — a simple, powerful, interpreted language like Javascript, with an API to interface to one of the best image-processing packages available. In practice, it’s not as good as it is in concept, but it’s still pretty good. The API doesn’t include all of Photoshop’s functionality directly, and there are a lot of things you need to execute as fairly obscure event actions. These event actions aren’t documented, but can be determined by activating a plug-in which logs everything that you do using the Photoshop GUI — you can then read through these logs, and copy the actions you need.

Still, there are some real advantages to using this Javascript interface, as opposed to something designed for the purpose like, say, Processing. You can use the Photoshop UI for controlling inputs to your script (set foreground colors, select portions of the image, select specific layers, etc.), and output your manipulations directly into Photoshop layers.

I’ll be posting here shortly a library I’ve created for easily building dialog panels for setting script options. I find that most manipulations I want to do have a set of variables, and I’d rather not tweak the code each time I want to change them.

This library was originally written under Photoshop 10 (aka CS3). Under version 11 (aka CS4), it was less stable. Sometimes it would crash out at odd places complaining that I was referencing properties of undefined objects. Because there have been memory leaks and other issues with the Javascript interpreter, these seemingly random failures were annoying but not too surprising. When it came to version 12 (aka CS5), I was rarely able to run my scripts at all. What made it frustrating was the apparent randomness of the crashes. I could print a variable to the console, and the very next line would crash out with an “undefined object” error when referencing that variable.
To make a long story short, I was able to track down the issue. It turns out that in iterations, declaring variables matters. That is to say:
for (i in someCollection)
{
$.writeln(i['someAttribute']);
}

will cause random crashes, but
for (var i in someCollection)
{
$.writeln(i['someAttribute']);
}

runs beautifully. Now, I “knew” that the var keyword is optional and used for specifying scope, but I never had any idea that there could be an issue within the scope of a simple loop. Obviously, Javascript didn’t know that I intended i to be a variable on each iteration — perhaps it thinks I meant for i to be a 1957 Chevy Belair on some iterations.

In any case, having cracked the code as it were, I have proceeded to enhance and add to my library. After a little more testing, I’ll be posting it here or on GitHub.


Sat, 24 Sep 2011

Ffun ffmpeg ffunctionality

— SjG @ 2:59 pm

I’ve been processing a collection of product videos which came to me in a huge variety of sizes, aspect ratios, and qualities. I need to re-encode them to work in HTML 5, but, more importantly, I need to make them fit into a common player space on the web page.

It turns out that newer versions of ffmpeg support not only cropping, but also padding, and you can even do both operations at once!

For example, I had a source video that was originally 16:9, but had been letterboxed to 4:3, and then had two different sets of labels added. I needed to crop out the letterboxed portion and the top set of labels, and make the result fit nicely into 16:9. So I used VLC, a screen capture utility, and Photoshop to get the measurements. Then I used ffmpeg to crop the relevant section and pad it out to fit into my space (in this case, I’m left aligning the video in the padded output):


ffmpeg -i original/converted.wmv -vf crop=394:295:6:0,pad=524:295:0:0:0xFFFF00 -sameq converted.mov

That’s cropping a 394 x 295 piece out of the original video (with the origin at 6 pixels from the left, and 0 pixels from the top), and then padding it out to 524 x 295 filling the padded area with bright yellow. The 524 x 295 is really close to 16:9 — and in a later process, it gets resized to the more standard 480 x 2721.

You can string together the padding and cropping in either order, depending on the effect you’re trying to achieve.

1I’m sure some educated person out there could tell me why video standards are so confused/confusing, down to the non-square pixels. While a true 16:9 would dictate 480 x 270 pixels, everybody seems to use 480 x 272. Why? The only thing I can figure out is that 272 is evenly divisible by a power of 2, which probably made display hardware cheaper to manufacture. As you can see, my resizing adds a bit of distortion, but at these resolutions, it doesn’t really matter.


Wed, 14 Sep 2011

Drinking with H. P. Lovecraft

— SjG @ 9:23 am

This blog, such as it is, operates upon the deeper philosophical principles of revealing only that mystical knowledge which is meet for the human mind. For within the awesome grandeur of the vast and forbidden extents of the universe, humanity is but writ small, and mere glimpses of these greater vistas could surely drive us into irredeemable madness. Yet horrible knowledge may be revealed in accidental circumstances, giving unfortunate philosophers a tiny seed of terrifying understanding which then inflames their feverish dreams, growing into frightful guesses and half-apprehension so hideous that it freezes the blood in their veins and robs them of all peace. Such philosophers, maddened by their dread, flee from the deadly conception and seek solace in the silent ocean of death rather than sharing their terror with minds unable to withstand the loathsome implications.

Regrettably, one philosopher’s torment and anguish led him to end his own life before destroying his notebooks. Alas! For then, through banal circumstances not worthy of mention, these notebooks found their way into my possession in the winter of 2010-2011. On one cursed day, I was going over the papers, wherein were puzzling jottings and disjointed ramblings, when I found the most peculiar formula, which will haunt me to the very end of my days:

Miskatonic Gin & Chthonic
4 to 5 Ice Cubes
3.5 ounces gin
4 ounces tonic water
0.5 ounces squid ink
1 small thousand-year-old pearl onion, wrested from the accursed soil at Cthulhu’s house at R’lyeh (or optionally a twist of lime)

Place the ice into a tall, narrow glass which is chilled with the eldritch horror of Yog-Sothoth’s malign suggestiveness (the cubes should fill the glass). Add gin, then the tonic water, then the squid ink. Stir well. Garnish with the onion (or lime), and serve before the aeons of blank horror drive you to madness.

Filed in:

Wed, 7 Sep 2011

Goldstein’s Laws, and Personal Adventures in Epistemology.

— SjG @ 7:28 pm

When I was a kid, my parents attended a lecture on the threat of religious cults. This must have been the early to mid 70s, in the heyday of the Moonies and countless counter-cultural communes. Being somewhat concerned at my credulity (and perhaps that of my siblings) and our inclination to trust the things we read, my father codified Goldstein’s First and Second Laws1:

  1. It’s ain’t necessarily so.
  2. But it might be.

The exact language for The First, of course, comes from Porgy & Bess, one of Dad’s favorites, but the sentiment often extended to a much more general skepticism than merely doubt. I can’t remember how many times that I’d repeat something I’d heard, or read, or just happened to believe, to be told “apply the First, son.” While this was first and foremost a warning to reconsider the “facts” involved in the subject at hand, it could also invite an invocation of The Second.

The thing is, though, you can’t just call upon The Second. Simply replying “apply the Second, Dad” is a wholly inadequate defense — akin to “is so!” on the playground. No, invoking The Second really requires offering more support for the questioned ideas, and is often the entrée into a much longer discussion. Goldstein’s first two laws are a prompting towards skepticism, or at least the application of critical thinking.

In my teens and twenties, I sometimes complained that the first two of Goldstein’s Laws were really a recipe for ambivalence. How could I hold a strong opinion on anything? This is a question that rarely plagues self-identified “skeptics,” nor did it really stop me from holding strong opinions. Yet this question is a key lesson from these laws. Questioning everything is not only exhausting, it’s impossible. You have to make some base assumptions to live by, assumptions you don’t continuously question. As Hume suggests, there are things you have to just accept as known. But how do you know what can you safely accept?

Along related lines, I once told my grandmother Ilse that I had known something, and later discovered that it was incorrect. “Then you didn’t really know it,” she corrected me. “You believed it.” With her definition, you can’t know something that’s untrue, because knowledge is understanding of what is true. Belief, on the other hand, does not require factual truth2. Given Goldstein’s First and Second, this definition suggested to me that I didn’t actually know anything, but that I simply had a collection of beliefs.

As I’ve gotten older, I worry less about what I can believe. Goldstein’s Laws, along with the inevitable lessons of experience, have imbued me with a healthy sense of skepticism. But it’s less about the beliefs themselves than it is the process of belief, or rather, the process of examination. In a sense, ideas are like shiny pebbles on the beach: fascinating to pick up, turn this way and that, examine and admire, but, in the end, few are worth keeping.

Skeptic or not, I can still get taken in by the better Internet hoaxes, and I’m not immune to the influence of the siren-song of advertising — but I do feel like I have a foundation I can rely on. I can comfortably consider new ideas without the need to immediately accept or reject them, and it seems that half of gullibility is in the speed of judgment3.

There is also the realization that Truth is a slippery creature, and often somewhat difficult to grasp. While I am convinced that there are, in fact, absolutes, most of the subjects where the issue of truth arises are less well defined. For example, there are hard, physical truths: pure gold melts at 1064.43 °C, an electron has a charge of 1.60217646 x 10-19 coulombs, and the planet earth weighs around 5.972 x 1024kg 4. Similarly, there are defined and a priori truths: the internal angles of a triangle on a plane will add up to 180 degrees, eΠi = -1, and there are no prime numbers between 3 and 5. There are improvable beliefs that are identified as Truths: Science will eventually explain the workings of the universe, or (my) Religion tells people the only right way to live their lives. Then there are the Truths that are considered true by convention or repetition but which have no bearing on reality: the Democrats are on the side of the poor, or the Republicans are fiscally responsible.

But most of the time, when we’re talking about Truth, we’re more concerned about a human dimension, whether it’s a recounting of history (“the compass was invented in China” or “the American Civil War was fought to free the slaves”), a character description (“Kurt Vonnegut was a misanthropist” or “Marie Antoinette was willfully ignorant”), or even self description (“I can’t paint” or “I’ll never understand quantum mechanics”). Many of these Truths are best replied to along the lines of “well, yes, but …” because there is some element that may be true, delivered in a thick coating of supposition. This, of course, is the infamous nuance problem, in that most things are surprisingly complicated, and a simple statement can’t adequately address that complexity.

Rather than argue about the Truths or dissecting the degrees of truth in complex issues, I find that I’m increasingly interested in less tangible things that don’t easily accept labels like “true” or “untrue.” These are matters like wonder, beauty, love, and even melancholy. They are emotional, or induce emotion. Unlike the shiny idea pebbles mentioned earlier, they are more experiential — the ephemeral process of picking them up, turning them about, examining them and admiring them is effectively the same as keeping them.


1 For the sake of completeness, there are two other formal laws:

  1. Not all errors must be corrected and not all insults must be avenged.
  2. But some of them must.

These latter two laws are also often accompanied by a paraphrase of Ecclesiastes 9:11: “the race is not always to the swift nor the battle to the strong.”
2 Her definition, to be fair, is the one more or less accepted by philosophers as far back as Plato. It may date earlier still.
3 The human brain is highly optimized for pattern recognition, and the optimization is biased towards speed over accuracy (although, in general, it’s very good at both). While this touches on a much larger subject, it’s clear that rapid decision-making is key for survival in some contexts, yet it introduces an as-yet unpatched vulnerability in human consciousness — a vulnerability which is systematically exploited in everything from advertising to politics to religion.
4 If these measurements are inexact, it should not invalidate the idea that there are physical truths — the measurement is not the fact. Perhaps this measurement of the earth’s weight is wrong, but the earth still has a weight. In the face of imprecise measurement, we could potentially argue whether or not these things are invariant. Furthermore, solid physical truths can get a little slippery when you get outside of the “classical” range of physics. For example, in Quantum Mechanics, a lot of measurements replace absolutes with probability functions. That, however, is a topic for other discussions.

Filed in: