Category: command-line magic

Processing a group of images for Cordova

Drudgery
Courtesy State Library of NSW

When you have a group of images that you want to use for your cordova app’s splash screen, it can be tedious to get their dimensions and then add them to the cordova.xml file.

You have to examine every image, get the height and width, then create the entry for it:

<splash src="" width="#" height="#" />

The Scenario

I was recently faced with doing just this for a mobile app, which had group of 18 icons. The Rule of Three comes right into play here, so a short while later, I banged out a one-liner using sed, of course!

The Solution

for i in *.png; do 
    identify $i|sed -e 's/^/<splash src="/' -e 's/png[^ ]*/png"/' -e 's/ PNG / width="/' -e 's/x[0-9]* [0-9]*x/" height="/' -e 's/+.*/" />/g' 
done

Dependencies

The code assumes you have imagemagick installed and available in your path, specifically the identify utility.

It also only works against PNGs as that’s what I use for mobile apps. It shouldn’t be too hard to change this by examining the output of the identify utility and adjusting the sed commands accordingly.

Output

The output looks like this:

<splash src="bitmoji1272828.png" width="398" height="398" />
<splash src="flag_final.png" width="229" height="146" />
<splash src="line_guy.png" width="302" height="455" />
<splash src="ikcron_92.png" width="128" height="128" />

 

See Also

Bulk process RAW image files

Recently I had to convert about 250 RAW image files to PNGs. For neatness, I wanted to convert the upper-case filenames the camera assigned with a lower-case name.

A little bash script-fu is all it took:

clean_pics.sh

#!/bin/bash
# Extract from SD Card
for i in /Volumes/SDCARD/DCIM/100ND40X/DSC_0*; do
 filename=$(basename "$i")
 lower_file="$(echo $filename | tr '[A-Z]' '[a-z]')"
 # verify it doesn't already exist
 newfile="${lower_file%.*}.png"
 echo -e "Processingnt$i tont$newfile"
 if [[ -e $newfile ]]; then
  echo "****SKIPPING"
 else
  convert "$i" "$newfile"
 fi
done

echo -e "Detoxing..."
find . -iname "*.png" -exec detox "{}" ;

echo "Procedure complete."

(“SDCARD”, etc is the path to the source files)

Once the script was up and running, it took about 1/2 hour to process all the files. Meanwhile, I was off doing something else!

Bulk fix a list of words to the proper case

A set of metal types
A set of metal types (Photo credit: Wikipedia)

Problem

Given a long list of upper-case words (one word per line), such as:

MARY
JOE
SCOTT
FRED
... (for thousands of rows) ...

You want to change this to

Mary
Joe
Scott
Fred
(etc)

Solution

Perl
Perl (Photo credit: Wikipedia)

Use perl from the command line. Assume all your names are in a file called ‘names.txt’. We’re going to send the output to a new file called ‘cap_names.txt’.

$ perl -pe 's/^(.)(.*)$/1L2/g' names.txt > cap_names.txt

How this works

On the matching side:
^(.) → get the first character. The parenthesis allow us to refer to it as 1.

(.*)$ → get the rest of the line. The parenthesis allow us to refer to it as 2.

On the substitution side:

1 → outputs that first character, as is.

L2 → outputs everything else, lower case (the L makes everything in 2 lower case).

Neat!

Inspired by Regexident’s answer at StackOverflow.

Enhanced by Zemanta

What are your top commands?

Sometimes it is interesting to navel-graze. What commands do you type the most?

history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

Here’s mine:

95 ll
93 git
67 cd
37 vi
29 identify
15 find
12 agvtool
11 history
11 exit
10 cat

“ll” is an alias for ls -l
Interesting that I’ve been using imagemagick (identify) a lot. I’ve been formatting/sizing a bunch of images for inclusion in an iOS app, so that’s probably what that is from.