Skip to content

Instantly share code, notes, and snippets.

@innermond
Last active October 13, 2022 10:23
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save innermond/d110d6234123bf87cc04 to your computer and use it in GitHub Desktop.
Save innermond/d110d6234123bf87cc04 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