{"id":1865,"date":"2018-11-04T19:00:08","date_gmt":"2018-11-04T18:00:08","guid":{"rendered":"https:\/\/www.edy.es\/dev\/?page_id=1865"},"modified":"2024-02-12T16:15:05","modified_gmt":"2024-02-12T15:15:05","slug":"imagemagick-cookbook","status":"publish","type":"page","link":"https:\/\/www.edy.es\/dev\/docs\/imagemagick-cookbook\/","title":{"rendered":"ImageMagick Cookbook"},"content":{"rendered":"<p>My own quick reference for using <a href=\"http:\/\/www.imagemagick.org\">ImageMagick<\/a>\u00a0in specific tasks.<\/p>\n<p>IMPORTANT: the order of the parameters does matter! Operations are applied in order.<\/p>\n<h3>Convert a DDS texture to PNG<\/h3>\n<pre>magick <strong>convert<\/strong> source.dds destination.png<\/pre>\n<p>Valid for any combination of formats.<\/p>\n<h3>Extract DDS layers to individual PNGs<\/h3>\n<pre>magick convert source.psd -set dispose Background -coalesce destination.png<\/pre>\n<p>Writes a sequence of files named destination-0.png, destination-1.png, destination-2.png, etc. 0 is the composed image, 1 is the first layer, 2 the second layer, etc.<\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/a\/7411336\/2519774\">source<\/a><\/p>\n<h3>Batch-convert many images to a different format<\/h3>\n<pre>magick <strong>mogrify<\/strong> -format png *.dds<\/pre>\n<p>*.dds are the source images, png is the target format. Valid for any combination of formats.<\/p>\n<h3>Extract the Alpha channel from an image<\/h3>\n<pre>magick convert source.png -set colorspace RGB <strong>-alpha extract<\/strong> source_alpha.png<\/pre>\n<p><strong>-alpha extract:<\/strong>\u00a0Copies the alpha channel values into all the color channels and turns &#8216;Off&#8217; the the image&#8217;s transparency, so as to generate a grayscale mask of the image&#8217;s shape.<\/p>\n<p><a href=\"https:\/\/imagemagick.org\/script\/command-line-options.php#alpha\">-alpha reference<\/a> |\u00a0<a href=\"http:\/\/www.imagemagick.org\/discourse-server\/viewtopic.php?t=21602\">source<\/a><\/p>\n<h3>Remove the Alpha channel from an image<\/h3>\n<pre>magick convert source.png <strong>-alpha off<\/strong> destination.png<\/pre>\n<p><a href=\"https:\/\/stackoverflow.com\/a\/28937338\/2519774\">source<\/a><\/p>\n<h3>Copy the Alpha channel or grayscale from an image to another<\/h3>\n<pre>magick convert source.png source_alpha.png <strong>-compose copy-opacity -composite PNG32:composed.png<\/strong><\/pre>\n<p>source.png and source_alpha.png should have the same size. Otherwise, the locations are controlled\u00a0according to -gravity and -geometry settings.<\/p>\n<p>If source_alpha.png doesn&#8217;t have an alpha channel then the grayscale values are copied.<\/p>\n<p><strong>-compose copy-*:<\/strong> Copy the specified channel (Red, Green, Blue, Cyan, Magenta, Yellow, Black, or Opacity) in the source image to the same channel in the destination image. If the channel specified does not exist in the source image, (which can only happen for methods, &#8216;copy-opacity&#8217; or &#8216;copy-black&#8217;) then it is assumed that the source image is a special grayscale channel image of the values that is to be copied.<\/p>\n<p><strong>-composite:<\/strong>\u00a0Perform alpha composition on two images and an optional mask. Take the first image &#8216;destination&#8217; and overlay the second &#8216;source&#8217; image according to the current -compose setting.<\/p>\n<p><a href=\"https:\/\/imagemagick.org\/script\/command-line-options.php#compose\">-compose and -composite reference<\/a> | <a href=\"https:\/\/superuser.com\/questions\/718755\/transfer-transparency-from-one-image-to-another-using-imagemagick\">source<\/a><\/p>\n<h3>Convert a PDF file to\u00a0PNG<\/h3>\n<pre>magick convert <strong>-density 150<\/strong> -antialias source.pdf -append <strong>-resize 1024x<\/strong> -quality 100\u00a0destination.png<\/pre>\n<ul>\n<li>remove -append to extract each page separately.<\/li>\n<li>source.pdf[i] to convert only the specified image.<\/li>\n<li>source.pdf[i-j]\u00a0to convert the page range.<\/li>\n<li>Extra high quality: use -density 600 and -resize 8192x<\/li>\n<\/ul>\n<p><a href=\"https:\/\/aleksandarjakovljevic.com\/convert-pdf-images-using-imagemagick\/\">source<\/a><\/p>\n<h3>Convert images to PDF<\/h3>\n<pre>magick convert -resize 2048x *.png <b>-<\/b>quality 100 destination.pdf<\/pre>\n<ul>\n<li>remove -resize to use the original images.<\/li>\n<li>removing -quality reduces the pdf size.<\/li>\n<\/ul>\n<h3>Split an image in\u00a0vertical slices<\/h3>\n<pre>magick convert source.png <strong>-crop 300<\/strong> +repage result.png<\/pre>\n<p>Multiple images will be written. Last one will be the remainder width.<\/p>\n<h3>Modify the canvas size<\/h3>\n<pre>magick convert source.png -background none <strong>-gravity center<\/strong> <strong>-extent 1024x1024<\/strong>\u00a0result.png<\/pre>\n<ul>\n<li>The original image is cropped or the background is extended to fit the -extent parameter. Original image is not scaled.<\/li>\n<li>use -background &lt;color&gt; for specifying the color that will be applied to the canvas, including\u00a0all transparent and semi-transparent zones. Default is white.<\/li>\n<\/ul>\n<p><a href=\"http:\/\/www.imagemagick.org\/Usage\/crop\/#extent\">-extent reference<\/a>\u00a0| <a href=\"http:\/\/www.imagemagick.org\/script\/color.php\">color list<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>My own quick reference for using ImageMagick\u00a0in specific tasks. IMPORTANT: the order of the parameters does matter! Operations are applied in order. Convert a DDS texture to PNG magick convert source.dds destination.png Valid for any combination of formats. Extract DDS layers to individual PNGs magick convert source.psd -set dispose Background -coalesce destination.png Writes a sequence [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1946,"parent":228,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"page-c.php","meta":{"footnotes":""},"class_list":["post-1865","page","type-page","status-publish","has-post-thumbnail","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1PjRF-u5","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.edy.es\/dev\/wp-json\/wp\/v2\/pages\/1865","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edy.es\/dev\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.edy.es\/dev\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.edy.es\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edy.es\/dev\/wp-json\/wp\/v2\/comments?post=1865"}],"version-history":[{"count":20,"href":"https:\/\/www.edy.es\/dev\/wp-json\/wp\/v2\/pages\/1865\/revisions"}],"predecessor-version":[{"id":2026,"href":"https:\/\/www.edy.es\/dev\/wp-json\/wp\/v2\/pages\/1865\/revisions\/2026"}],"up":[{"embeddable":true,"href":"https:\/\/www.edy.es\/dev\/wp-json\/wp\/v2\/pages\/228"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.edy.es\/dev\/wp-json\/wp\/v2\/media\/1946"}],"wp:attachment":[{"href":"https:\/\/www.edy.es\/dev\/wp-json\/wp\/v2\/media?parent=1865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}