Skip to content

Instantly share code, notes, and snippets.

@xErik
Last active March 5, 2018 18:50
Show Gist options
  • Save xErik/5c5193934505d582bd57fe05c69cd5e7 to your computer and use it in GitHub Desktop.
Save xErik/5c5193934505d582bd57fe05c69cd5e7 to your computer and use it in GitHub Desktop.

Get with and height

randomFileW=$(identify -format "%w\n" $randomFile) randomFileH=$(identify -format "%h\n" $randomFile) randomFileName=$(identify -format "%h\i" $randomFile)

Get maximum height and width of images in directory

maxWidth=$(identify -format "%w\n" dir/* | sort -n -r | head -n 1) maxHeight=$(identify -format "%h\n" dir/* | sort -n -r | head -n 1)

Remove single BW pixels in BW image

-morphology thicken '1x3>:1,0,1'
-morphology thinning '1x3>:0,1,0' \

Removing single border

-gravity west -chop 300x0 +repage \

Adding single border

-gravity west -splice 200x0 +repage \

Create BW image

-colorspace gray -quantize gray +dither -colors 2 -depth 2 \

Aggresive Cropping

convert -verbose $limit $f \
            -colorspace gray -auto-level -crop \
                `convert $limit $f -fill white -fuzz 60% \
                -draw 'color 0,0 replace' -flop  -draw 'color 0,0 replace' -flip \
                -draw 'color 0,0 replace' -flip  -draw 'color 0,0 replace' -flop \
                -fuzz 50% -trim -format '%wx%h%O' info:` \
            +repage -threshold 50% out.jp2

Combine images into PDF of a certain site

convert image.tif -resize 575x823^> -gravity center -background white -extent 595x842 image.pdf

Threads

echo "a.jpg b.jpg c.jpg" | xargs -P 4 -n 50 mogrify ...

find dir/ -name "*.jp2" -type f -print | xargs -P 2 -n 100 mogrify ...

Replace specific, fuzzed color with white:

-fuzz 7% -fill white -opaque 'rgb(118,105,58)' \

Skip first line

awk 'NR > 1'

Specify max output file size

convert original.jpeg -define jpeg:extent=300kb output.jpg

Floating point operations in shell

imgWidth=$(echo "$imgWidth*ratio" | bc -l)

Looping through shell arguments

for f in ${@}; do

tmp=$(basename "$f")

# get file extension
ext="${tmp##*.}"

# Get file name without extension
name="${tmp%.*}"

# Strip leading zeros from file name: convert to number 
page=$(echo $name | sed -E 's,^0+,,g')

if [ $(( $page % 2 )) -eq 0 ]; then
    # even number
else
    # odd number
fi

done

Build Image Magick 7 on OSX HighSierra

When make results in an (ld) error, exclude with --without-(DEPENDCY)

Enabbling OpenMP by installing gcc7 via hombrew (brew install gcc) and ./configure --with-quantum-depth=8 --enable-openmp CC=gcc-7 CFLAGS=-fopenmp CXX=g++-7 CXXFLAGS=-fpermissive

Python: Reinstall in Virtual Environment

ve/bin/pip install --upgrade --force-reinstall --no-deps . \
    && ve/bin/img2pdf -o out.pdf -S 6inx9in -b 0.25in:0.25in -f into -C L  in.jp2 && \
    sleep 1 && \
    open out.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment