{"id":855,"date":"2020-12-05T19:05:42","date_gmt":"2020-12-05T17:05:42","guid":{"rendered":"https:\/\/www.bashpi.org\/?page_id=855"},"modified":"2020-12-05T19:16:27","modified_gmt":"2020-12-05T17:16:27","slug":"image-processing-with-imagemagics-convert","status":"publish","type":"page","link":"https:\/\/www.bashpi.org\/?page_id=855","title":{"rendered":"convert"},"content":{"rendered":"\n<p>Do you want to make copies of your images with different resolutions quickly?<\/p>\n\n\n\n<p>Imagemagics&#8217;s convert tool is pretty good at this. Following shell script can be easily adjusted to make as many different resolutions as you need. It only reduces images and keeps their aspect ratio&#8230; but you can easily adjust settings for each resolutions.<\/p>\n\n\n\n<p>To speed up processing, you can adjust number of background jobs the script runs.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# this script converts your source images to multiple sets of images with different resolutions \/ settings\n# argument 1 must be directory, where your images reside\n# it will then create subdirectories for each resolution\n# NO SPACES IN FILENAMES!\n# files are only resized down, smaller images are not enlarged\n\n# processing settings\n# number of background jobs to run at once - change it according to your CPU\nbg=8\n\ndeclare -a resolutions\ndeclare -A options\n\n# populate resolutions array with wanted resolutions\nresolutions&#91;0]=\"128x96\"\nresolutions&#91;1]=\"1024x768\"\nresolutions&#91;2]=\"1920x1080\"\n\n# populate options array with wanted options - make sure array keys match with specific resolution\noptions&#91;\"128x96\"]=\"-auto-orient -verbose -quality 80%\"\noptions&#91;\"1024x768\"]=\"-auto-orient -verbose -quality 80%\"\noptions&#91;\"1920x1080\"]=\"-auto-orient -verbose -quality 80%\"\n\n# output image type\/extension\nout=\"jpg\"\n\n# functions\nusage()\n{\n\n        echo \"Usage: $0 &lt;dirname>\"\n        exit 1\n\n}\n\n# check is catalog provided as first argument\nif &#91; $# -ne 1 ]; then\n  echo \"Provide directory containing images as first argument\" \n  usage\nfi\nif &#91; ! -d ${1} ]; then\n  echo \"First argument is not directory\"\n  usage\nfi\n\necho \"started at `date`\"\necho \"Processing files in directory $1\"\ncd $1\n\n# cycle trough all given resolutions \nfor resolution in \"${resolutions&#91;@]}\"; do\n\n  mkdir -p ${resolution}\n\n  for f in `ls -1 *.*`;do\n\n    # get filename and extension\n    ext=`echo $f |awk -F . '{print $NF}'`\n    base=`basename $f .${ext}`\n\n    echo \"convert $f -resize ${resolution}\\> ${options&#91;${resolution}]} .\/${resolution}\/${base}.${out}\"\n    convert $f -resize ${resolution}\\>  ${options&#91;${resolution}]} .\/${resolution}\/${base}.${out} &amp;\n\n    #job control\n    while &#91;&#91; $( jobs | wc -l ) -ge $bg ]]; do\n      sleep 1\n    done\n  \n  done\n\ndone\n\necho \"finished at `date`\"<\/code><\/pre>\n\n\n\n<p>ImageMagic command-line tools and info can be found <a href=\"https:\/\/imagemagick.org\/script\/command-line-processing.php\">here<\/a> <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you want to make copies of your images with different resolutions quickly? Imagemagics&#8217;s convert tool is pretty good at this. Following shell script can be easily adjusted to make as many different resolutions as you need. It only reduces images and keeps their aspect ratio&#8230; but you can easily adjust settings for each resolutions. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-855","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=855"}],"version-history":[{"count":6,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/855\/revisions"}],"predecessor-version":[{"id":863,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/855\/revisions\/863"}],"wp:attachment":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}