Skip to content

Instantly share code, notes, and snippets.

@joujiahe
Forked from innermond/pdfimage
Created October 12, 2020 15:46
Show Gist options
  • Save joujiahe/cc03fc9ed914e092339d10c07f63b3c7 to your computer and use it in GitHub Desktop.
Save joujiahe/cc03fc9ed914e092339d10c07f63b3c7 to your computer and use it in GitHub Desktop.
Extract images from pdf but, surprise! The images that contain alpha channels are extracted as two images. The source and the mask, as they are stored inside pdf file. This script recombine them into an one png file with given transparency
#/usr/bin/bash
# extract images from pdf at first-hand
#prefix=pict
#echo extract images from "$1"
#pdfimages -j "$1" $prefix
# IMAGES ARE SAVED SECVENTIALLY AS IMAGE AND THE NEXT IS A MASK IMAGE !!!!
declare -a files=($(ls *.ppm *.pbm))
mask=
image=
for (( i = 0; i < ${#files[*]}; i = i + 2 ))
do
image=${files[$i]}
mask=${files[$i+1]}
echo converting "${image%.*}".png
convert $image $mask -compose CopyOpacity -composite "${image%.*}".png
echo removing image and it\'s mask
rm "$image" "$mask"
done
echo done!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment