Converting images via terminal

Every now and then while working on an app or simply automating some recurrent dev tasks I ran into ImageMagick which you can get installed via brew install convert.

And Now a Touch of Magick
The tool is incredibly powerful and as all Unix command-line tools have more switches and order of using those switches and gibberish sequence of characters than a pack of cats times their lives and power of their cousins' lives.

So this post is for the occasional desperate moment that I figured how to do something very simple and specific for which I have the guarantee that next time that I want to do the same or very similar thing I won't remember how I did it before.

Once you installed you can call convert followed by arguments. Chances are that before you actually convert anything you need to re-organize or find your files that will be subject to the conversions. Let's document that part first. There are millions of ways to that, the below is what worked for me.

If you have all images in multiple folders and subfolders you might want to move to find them first and then move to whatever location you prefer. Here is how you do it:
find . -name "*.pdf" -exec mv '{}' "../" ";"
In that case, I am searching for PDFs and then moving them to a folder above. ../
find . -name "*.URL" -exec rm '{}' ";"
In the scenario above, we are searching for all files with an extension .URL and deleting them. In other words, the command you put after -exec drives the result on the search results.

Let's say you want to convert a bunch of images into PDFs.
convert *.jpg +adjoin page-%d.pdf
The above will produces this type of output.

If you want to generate one PDF out of all the images then you can use this:
convert *.jpg -adjoin output.pdf
Notice the minus sign vs the plus sign in the previous example.

If you want to take that task to the next level, which likely is your default level since how often you everything in one place and not spread across multiple folders... e.g. XCode assets... Then, in that case, you want to use this simple to memorize sequence...sooo simple.

for d in ./*/ ; do (cd "$d" && echo "$d" && convert '*.jpg' -adjoin "${PWD##*/}.pdf"); done
I trust that if you are reading this type of blog you understand that "d" is the current screening object of the file system and the PWD extracts the current folder name ## within the folder name stand for numbered sequences (file01something-else)

If you happen to have assets into your XCODE asset folder and they have an alpha the App Store uploader will puke and invalidate your upload. To fix that in one shot, here is what I came up with:
for i in `ls *.png`; do convert $i -background black -alpha remove -alpha off $i; done
Some images might have text into it and occasionally I find it useful to make those searchable, particularly if I have assets that I use in my apps. For that, I convert the images into PDF
ls -1 ./*jpg | xargs -L1 -I {} img2pdf {} -o {}.pdf
and then I add a layer of OCR with this
pypdfocr combined.pdf  

A word about Python

On occasion, I use Python and I more times than not I ran into the annoying situation that the default Python installed on macOS doesn't fit the bill for the job. This article gives you all the extra details on how to set your environment right. It mostly boils down to:

  1. Make sure homebrew is current, using: brew update && brew upgrade
  2. Install a tool that manages the multiple environments via brew install pyenv
  3. Check which exact version you need by using pyenv install --list
  4. Install that version by using pyenv install version_here
  5. Make it the default version you want to use (the real deal) pyenv global version_installed
  6. Double checked that it worked using pyenv version
  7. Edit you .bash/zshrc using echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.zshrc
  8. Check the version you are set on by using python -V
  9. then brew install pyenv-virtualenvwrapper
  10. and finally add to your bash_profile the following: export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV="true"



Popular posts from this blog

Zen-inification - The art of decluttering

Postgres on Synology

The Making of Basculo