My 404s Don't code and drive

About

Batch Resize Images With Padding

ImageMagick comes with a tool to perform batch image transforms, it is called mogrify. To perform a resize of a set of images keeping the aspect ratio and padding with a white background we can use the following command,

mogrify -resize 100x100 -background white -gravity center -extent 100x100 -path ./resize_directory *.png

This command resizes all png images in the directory to a 100x100 and pads with white to keep the aspect ratio.

Caffe import - TypeError got an unexpected keyword argument 'syntax' error

Got the following error when importing the caffe package for python on Mac and OSX 10.12.

from caffe.proto import caffe_pb2
File "caffe/proto/caffe_pb2.py", line 23, in <module>
x01(\x0b\x32\x1a.caffe.HDF5OutputParameter\".\n\nPoolMethod\x12\x07\n\x03MAX\x10\x00\x12\x07\n\x03\x41VE\x10\x01\x12\x0e\n\nSTOCHASTIC\x10\x02\"W\n\x0ePReLUParameter\x12&\n\x06\x66iller\x18\x01 \x01(\x0b\x32\x16.caffe.FillerParameter\x12\x1d\n\x0e\x63hannel_shared\x18\x02 \x01(\x08:\x05\x66\x61lse*\x1c\n\x05Phase\x12\t\n\x05TRAIN\x10\x00\x12\x08\n\x04TEST\x10\x01')
TypeError: __init__() got an unexpected keyword argument 'syntax'

This was resolved by editing the caffe_pb2.py file and commenting out all lines which said syntax='proto2'

Python Pip Install Under Different Python Versions

Due to python 2 and 3 it usual to have different version of installs working. A smooth method for installing packages using pip for different versions is to call pip via the python version. Like this (for Ubuntu python version 3.5),

python3.5 -m pip install ipython numpy --user

Setting Install Directory When Using CMake

Sometimes we want to install a build to a different directory. In CMake this can be done by setting the CMAKE_INSTALL_PREFIX variable. When we are building OpenCV3 for examle we would do,

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ../opencv-3

Batch Crop Images Using ImageMagick

ImageMagick comes with a tool to perform batch image transforms, it is called mogrify. To crop a specific rectangular area in all images in a directory the formula looks like this,

mogrify -crop [rectWidth]x[rectHeight]+[rectTopX]+[rectTopY] -path pathToStoreCroppedFiles pathToImgDirectory/*.extension

A realization of this might look like this

mogrify -crop 300x300+290+170 -path ./myCroppedFilesDir *.png

This crops a 300x300 rectangle whos top is positioned at 290x170 from all the png images in the current directory and saves them to the directory myCroppedFilesDir(make sure the dir exists before you run).