{"id":2796,"date":"2021-05-02T22:26:32","date_gmt":"2021-05-02T20:26:32","guid":{"rendered":"https:\/\/davidlebech.com\/thoughtflow\/?p=2796"},"modified":"2021-05-02T22:26:32","modified_gmt":"2021-05-02T20:26:32","slug":"simple-photo-effects-with-imagemagick","status":"publish","type":"post","link":"https:\/\/davidlebech.com\/thoughtflow\/simple-photo-effects-with-imagemagick\/","title":{"rendered":"Simple photo effects with ImageMagick"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I recently explored the capabilities of converting photos to something that looks hand-drawn with a pencil using <a href=\"https:\/\/imagemagick.org\/index.php\">ImageMagick<\/a>. It is difficult to get good results, but I thought I would share a few one-liners that I found useful to play around with, as well as a bonus effect that turns a photo into something resembling a miniature.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I have collected all the commands in a gist <a href=\"https:\/\/gist.github.com\/dlebech\/63d305a9bb7ca530faf20fc1629cd756\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ImageMagick, as the name implies, can do a lot of basic and advanced image manipulation out of the box. For example, this is my current go-to command for resizing all images in a folder:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mogrify -resize 1200 *.jpg<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mogrify<\/code> and <code>convert<\/code> are two commands included with ImageMagick, and they can do a lot more than just resize photos.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I will be using this photo of the Geirangerfjorden in Norway as an example throughout:<\/p>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger-1024x683.jpg\" alt=\"\" class=\"wp-image-2830\" srcset=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger-1024x683.jpg 1024w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger-300x200.jpg 300w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger-150x100.jpg 150w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger-768x512.jpg 768w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Photo of <a href=\"https:\/\/www.flickr.com\/photos\/dlebech\/27755389194\/\">Geirangerfjorden in Norway<\/a>.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">By the way, I also used Imagemagick to reduce the resolution and size of that photo so it was better suited for this post:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>convert -format jpg \\\n    -strip \\\n    -interlace Plane \\\n    -quality 50% photo.jpg \\\n    -resize 1200 photo_resized.jpg<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Before running any of the commands in the following sections, you might want to make sure your terminal has extendedglob enabled:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># zsh\nsetopt extendedglob\n# bash\nshopt -s extglob<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Finding outlines<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">My use case for finding outlines was to simplify photos into basic lines that I could use as models for my own pencil drawings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This command finds very sparse and minimal outlines using the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Canny_edge_detector\">canny edge detector<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>convert ^*canny*.jpg \\\n    -set filename:original %t \\\n    -resize 1200 \\\n    -canny 0x3+5%+15% \\\n    -negate '%&#91;filename:original]_canny1.jpg'<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image alignwide\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_canny1_resized-1024x683.jpg\" alt=\"\" class=\"wp-image-2831\" srcset=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_canny1_resized-1024x683.jpg 1024w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_canny1_resized-300x200.jpg 300w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_canny1_resized-150x100.jpg 150w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_canny1_resized-768x512.jpg 768w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_canny1_resized.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Image outline using canny edge detector &#8211; v1<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">With a small adjustment to the parameters, we can get a bit more detail and more lines.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>convert ^*canny*.jpg \\\n    -set filename:original %t \\\n    -resize 1200 \\\n    -canny 0x1+10%+20% \\\n    -negate '%&#91;filename:original]_canny2.jpg'<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_canny2_resized-1024x683.jpg\" alt=\"\" class=\"wp-image-2833\" srcset=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_canny2_resized-1024x683.jpg 1024w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_canny2_resized-300x200.jpg 300w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_canny2_resized-150x100.jpg 150w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_canny2_resized-768x512.jpg 768w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_canny2_resized.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Image outline using canny edge detector &#8211; v2<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can play around with the <a href=\"https:\/\/imagemagick.org\/script\/command-line-options.php#canny\">canny parameters<\/a> for different results..<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pencil\/sketch effect<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Trying to get closer to an actual pencil-like effect, I played around with various methods. Most of the commands were modified from <a href=\"https:\/\/legacy.imagemagick.org\/Usage\/photos\/#pencil\">this article<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ImageMagick has a convert-to-pencil effect that is called <code><a href=\"https:\/\/imagemagick.org\/script\/command-line-options.php#sketch\">sketch<\/a><\/code>. It is a bit difficult to get good results though, and the command can be very slow if a large radius (one of the parameters) is used.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This was the best I could do when playing around with the parameters for a few minutes. Notice the weird trick where I negate the colors &#8212; it was the only way to avoid the photo becoming completely white in the middle where the ship is. That is why the sky becomes so dark:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>convert geiranger.jpg \\\n    -resize 1200 \\\n    -colorspace gray \\\n    -negate \\\n    -sketch 0x20+20 geiranger_sketch.jpg<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_sketch_resized-1024x683.jpg\" alt=\"\" class=\"wp-image-2835\" srcset=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_sketch_resized-1024x683.jpg 1024w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_sketch_resized-300x200.jpg 300w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_sketch_resized-150x100.jpg 150w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_sketch_resized-768x512.jpg 768w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_sketch_resized.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Convert photo to pencil-like format using the sketch parameter.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Another way to create pencil-like photo conversions is using the technique that the sketch parameter is based on. This involved creating an intermediate image which is used as a filter over the image to give the pencil-stroke effect.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I have to admit, I have no idea what is going on in these commands, and I mostly got them from <a href=\"https:\/\/legacy.imagemagick.org\/Usage\/photos\/#pencil\">the article<\/a> mentioned above. First, create some noise with motion blur:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>convert -size 256x256 \\\n    xc: +noise Random \\\n    -virtual-pixel tile \\\n    -motion-blur 0x20+20 \\\n    -charcoal 1 \\\n    -resize 50% pencil_tile.gif<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then convert the photo:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>convert geiranger.jpg \\\n    -colorspace gray \\\n    \\( +clone -tile pencil_tile.gif -draw \"color 0,0 reset\" \\\n    +clone +swap -compose color_dodge -composite \\) \\\n    -fx 'u*.2+v*.8' geiranger_sketch2.jpg<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The result is slightly different, but not perfect:<\/p>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_sketch2_resized-1024x683.jpg\" alt=\"\" class=\"wp-image-2836\" srcset=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_sketch2_resized-1024x683.jpg 1024w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_sketch2_resized-300x200.jpg 300w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_sketch2_resized-150x100.jpg 150w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_sketch2_resized-768x512.jpg 768w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_sketch2_resized.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Convert photo to pencil-like format using intermediate noise tile.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">A third technique is to use the charcoal parameter which is actually being used to create the intermediate image above:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>convert geiranger.jpg \\\n    -charcoal 1 geiranger_char1.jpg<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_char1_resized-1024x683.jpg\" alt=\"\" class=\"wp-image-2837\" srcset=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_char1_resized-1024x683.jpg 1024w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_char1_resized-300x200.jpg 300w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_char1_resized-150x100.jpg 150w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_char1_resized-768x512.jpg 768w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_char1_resized.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Convert photo to pencil-like format using -charcoal parameter<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Miniature faking<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">I have always been fascinated by those photos that have a blurry effect that make them look like they are miniatures. The effect is called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Miniature_faking\">miniature faking<\/a> and is related to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Tilt%E2%80%93shift_photography\">tilt-shift photography<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/legacy.imagemagick.org\/Usage\/photos\/#tilt_shift\">aforementioned article<\/a> has an example. It works in some cases, but most of the time, the outcome is very bad. It happens to be ok for the Geiranger photo though. Slightly adjusted from the article:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>convert geiranger.jpg -sigmoidal-contrast 10x50% \\\n    \\( +clone -sparse-color Barycentric '0,0 black 0,%h gray80' \\\n    -solarize 50% -level 30%,0 -write mpr:blur_map \\) \\\n    -compose Blur -set option:compose:args 10x0 \\\n    -composite mpr:blur_map \\\n    -compose Blur \\\n    -set option:compose:args 0x10 \\\n    -composite geiranger_blur.jpg<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_miniature_resized-1024x683.jpg\" alt=\"\" class=\"wp-image-2839\" srcset=\"https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_miniature_resized-1024x683.jpg 1024w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_miniature_resized-300x200.jpg 300w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_miniature_resized-150x100.jpg 150w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_miniature_resized-768x512.jpg 768w, https:\/\/davidlebech.com\/thoughtflow\/wp-content\/uploads\/geiranger_miniature_resized.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Convert photo to a &#8220;miniature&#8221; scene. This does not work well on all photos.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">That is it for now. Good night :-)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently explored the capabilities of converting photos to something that looks hand-drawn with a pencil using ImageMagick. It is difficult to get good results, but I thought I would share a few one-liners that I found useful to play around with, as well as a bonus effect that turns a photo into something resembling [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2839,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[228,66],"class_list":["post-2796","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips","tag-imagemagick","tag-photos"],"_links":{"self":[{"href":"https:\/\/davidlebech.com\/thoughtflow\/wp-json\/wp\/v2\/posts\/2796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidlebech.com\/thoughtflow\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidlebech.com\/thoughtflow\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidlebech.com\/thoughtflow\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidlebech.com\/thoughtflow\/wp-json\/wp\/v2\/comments?post=2796"}],"version-history":[{"count":0,"href":"https:\/\/davidlebech.com\/thoughtflow\/wp-json\/wp\/v2\/posts\/2796\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/davidlebech.com\/thoughtflow\/wp-json\/wp\/v2\/media\/2839"}],"wp:attachment":[{"href":"https:\/\/davidlebech.com\/thoughtflow\/wp-json\/wp\/v2\/media?parent=2796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidlebech.com\/thoughtflow\/wp-json\/wp\/v2\/categories?post=2796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidlebech.com\/thoughtflow\/wp-json\/wp\/v2\/tags?post=2796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}