{"id":68394,"date":"2022-11-05T09:02:27","date_gmt":"2022-11-05T09:02:27","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=68394"},"modified":"2022-11-05T09:02:27","modified_gmt":"2022-11-05T09:02:27","slug":"fix-scanned-images-with-imagemagick","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=68394","title":{"rendered":"Fix scanned images with ImageMagick"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">Fix scanned images with ImageMagick<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/seth\" class=\"username\">Seth Kenlon<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Sat, 11\/05\/2022 &#8211; 03:00<\/span><\/p>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>Years ago while rummaging through the contents of a shelf in a used bookstore, I happened upon a booklet titled &#8220;UNIX System Command Summary for Berkeley 4.2 &amp; 4.3 BSD,&#8221; published by\u00a0Specialized Systems Consultants. I bought it as a curiosity item because it was nearly 20 years old yet still largely applicable to modern Linux and BSD.<\/p>\n<p>That amused me then and now. A booklet written in 1986 was still largely relevant in 2016, while books on the same shelf about a proprietary OS weren&#8217;t worth the paper they were printed on. (Think about it: What technology do\u00a0<em>you<\/em>\u00a0think is going to survive a zombie apocalypse?) I&#8217;ve had the booklet on my own bookshelf for several years now, but it occurred to me that it&#8217;s probably worth doing a little digital preservation of this artifact, so I decided to scan the booklet to create a\u00a0<a href=\"https:\/\/opensource.com\/article\/19\/3\/comic-book-archive-djvu\">CBZ ebook<\/a>.<\/p>\n<p>Scanning was easy, albeit time-consuming, with\u00a0<a href=\"https:\/\/opensource.com\/article\/22\/2\/scan-documents-skanlite-linux-kde\">Skanlite<\/a>. After I was finished, however, I discovered that some pages weren&#8217;t quite level.<\/p>\n<article class=\"align-center media media--type-image media--view-mode-default\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/11\/imagemagick-crook_1.png\" width=\"400\" height=\"1000\" alt=\"A page of text, including a table of contents and a glossary, that is crooked and distorted\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>(Seth Kenlon, CC BY-SA 4.0)<\/p>\n<\/div>\n<\/article>\n<p>In printing, this is called a registration problem, meaning that the position of what&#8217;s being printed isn&#8217;t correctly orientated on the page.<\/p>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">More open source alternatives<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/business\/16\/3\/top-project-management-tools-2016?intcmp=7013a000002D4iFAAS\">Open source project management tools<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/alternatives\/trello?intcmp=7013a000002D4iFAAS\">Trello alternatives<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/article\/18\/4\/new-state-video-editing-linux?intcmp=7013a000002D4iFAAS\">Linux video editors<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/life\/12\/6\/design-without-debt-five-tools-for-designers?intcmp=7013a000002D4iFAAS\">Open source alternatives to Photoshop<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/alternatives?intcmp=7013a000002D4iFAAS\">List of open source alternatives<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/alternatives?intcmp=7013a000002D4iFAAS\">Latest articles about open source alternatives<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<h2>ImageMagick<\/h2>\n<p><a href=\"https:\/\/opensource.com\/article\/17\/8\/imagemagick\">ImageMagick<\/a>\u00a0is a non-interactive terminal-based graphics editor. It might seem counterintuitive to try to edit a graphic in a graphic-less environment like a text-only terminal, but it&#8217;s actually very common. For instance, when you upload an image to use as a profile picture to a web application, it&#8217;s likely that a script on the application&#8217;s server processes your image using ImageMagick or its libraries. The advantage of a non-interactive editor is that you can formulate what needs to be done to a sample image, then apply those effects to hundreds of other images at the press of a button.<\/p>\n<p>ImageMagick is generally just as capable as any graphics editor, as long as you take the time to uncover its many functions and how to combine them to achieve the desired effects. In this case, I want to rotate pages that are askew. After searching through ImageMagick&#8217;s documentation, I discovered that the ImageMagick term for the solution I needed was called deskew. Aligning your terminology with somebody else&#8217;s terminology is a challenge in anything that you don&#8217;t already know, so when you approach ImageMagick (or anything), keep in mind that the word\u00a0<em>you&#8217;ve<\/em>\u00a0decided describes a problem or solution may not be the same word used by someone else.<\/p>\n<p>To deskew an image with crooked text using ImageMagick:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span>convert page_0052.webp <span class=\"re5\">-deskew<\/span> <span class=\"nu0\">25<\/span><span class=\"sy0\">%<\/span> fix_0052.webp<\/code><\/span><\/pre>\n<p>The\u00a0<code>-deskew<\/code>\u00a0option represents the threshold of acceptable skew. A skew is determined by tracing peaks and valleys of objects that appear to be letters. Depending on how crooked your scan is, you may need more or less than 25% threshold. I&#8217;ve gone as high as 80%, and so far nothing under 25% has had an effect.<\/p>\n<p>Here&#8217;s the result:<\/p>\n<article class=\"align-center media media--type-image media--view-mode-default\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/11\/imagemagick-deskew-fix.png\" width=\"400\" height=\"922\" alt=\"The same page of text, now with the text properly aligned\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>(Seth Kenlon, CC BY-SA 4.0)<\/p>\n<\/div>\n<\/article>\n<p>Fixed! Applying this to the remaining 55 pages of the document fixed skewed pages while doing nothing to pages that were already straight. In other words, it was safe to run this command on pages that needed no adjustment, thanks to my threshold setting.<\/p>\n<h2>Cropping an image with ImageMagick<\/h2>\n<p>After correcting for a skew, and because I scanned more of each page than necessary anyway to prevent accidentally cutting off words, I decided that it made sense to crop my corrected pages. I was happy to keep some space around the margins, but not quite as much as I had. I use the\u00a0<code>crop<\/code>\u00a0function of ImageMagick often enough for images on this very website, so I was familiar with the option. However, I needed to determine how to crop each page.<\/p>\n<p>First, I needed the size of the image:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ identify fixed_0052.webp<br>\nWEBP 1128x2593 1128x2593+<span class=\"nu0\">0<\/span>+<span class=\"nu0\">0<\/span> <span class=\"nu0\">8<\/span>-bit sRGB 114732B 0.020u <span class=\"nu0\">0<\/span>:<span class=\"nu0\">00.021<\/span><\/div><\/div><\/pre>\n<p>Knowing the size, I was able to make some estimations about how many pixels I could stand to lose. After a few trial runs, I came up with this:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\">convert fix_0052.webp <span class=\"re5\">-gravity<\/span> Center <span class=\"re5\">-crop<\/span> 950x2450+<span class=\"nu0\">0<\/span>+<span class=\"nu0\">0<\/span> crop_0052.webp<\/code><\/span><\/pre>\n<p>This isn&#8217;t an exact fit, but it proved important when I applied it to other images in the booklet. The pages varied in content and scanner placement here and there, so I was happy to give each one a little breathing room.<\/p>\n<p>Here&#8217;s the corrected and cropped image:<\/p>\n<article class=\"align-center media media--type-image media--view-mode-default\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/11\/imagemagick-deskew-crop.png\" width=\"400\" height=\"1005\" alt=\"The same page of text, with the previous fixes applied and crooked white margins around the page cropped out.\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>(Seth Kenlon, CC BY-SA 4.0)<\/p>\n<\/div>\n<\/article>\n<h2>Batch image editing with open source<\/h2>\n<p>The beauty of ImageMagick is that once you&#8217;ve figured out the formula for fixing your image, you can apply that fix to\u00a0all\u00a0images requiring the same fix. I do this with\u00a0<a href=\"http:\/\/link-to-seth-gnu-parallel-redhat.com\/SYSADMIN\">GNU Parallel<\/a>, which uses all my CPU cores to finish image correction across hundreds of pages. It doesn&#8217;t take long, and the results speak for themselves. More importantly, I&#8217;ve got a digital archive of a fun artifact of UNIX history.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>It&#8217;s easy to correct images, even in batches, with this open source tool.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/11\/OSDC_women_computing_3-1.png\" width=\"520\" height=\"292\" alt=\"Woman sitting in front of her computer\" title=\"Women in computing and open source\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>Ray Smith <\/p>\n<\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/tools\" hreflang=\"en\">Tools<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/art-and-design\" hreflang=\"en\">Art and design<\/a><\/div>\n<\/p><\/div>\n<div class=\"hidden field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/11\/cc-by-sa-4-6.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<div class=\"field field--name-field-credits field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\">\n<article class=\"profile\"><a href=\"https:\/\/opensource.com\/points-and-badges\" class=\"profile__points\">5014 points<\/a><\/p>\n<div class=\"profile__container\">\n<div class=\"field field--name-field-user-picture field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-no-captions\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/11\/jimhall1.jpg\" width=\"667\" height=\"667\" alt=\"photo of Jim Hall\"><\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-location field--type-string field--label-hidden field__item\">Minnesota<\/div>\n<div class=\"clearfix text-formatted field field--name-field-user-about field--type-text-with-summary field--label-hidden field__item\">\n<p>Jim Hall is an open source software advocate and developer, best known for usability testing in <a href=\"https:\/\/www.gnome.org\/\" rel=\"ugc\">GNOME<\/a> and as the founder + project coordinator of <a href=\"https:\/\/www.freedos.org\/\" rel=\"ugc\">FreeDOS<\/a>. At work, Jim is CEO of <a href=\"https:\/\/hallmentum.com\/\" rel=\"ugc\">Hallmentum<\/a>, an IT executive consulting company that provides hands-on IT Leadership training, workshops, and coaching.<\/p>\n<\/div>\n<div class=\"field field--name-field-twitter-id field--type-string field--label-hidden profile__twitter\">| Follow jimfhall<\/div>\n<div class=\"field field--name-field-linkedin-id field--type-string field--label-hidden profile__linkedin\">| Connect  jimfhall<\/div>\n<div class=\"field field--name-field-user-attributes field--type-entity-reference field--label-above\">\n<div class=\"field__label\">User Attributes<\/div>\n<div class=\"field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/taxonomy\/term\/16038\" hreflang=\"en\">Correspondent<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"profile__badges\">\n<p>  <a href=\"https:\/\/opensource.com\/points-and-badges\" class=\"field field--name-name field--type-string field--label-above profile__badges__name profile__badges__color-\">Open Sourcerer<\/a><\/p>\n<p>  <a href=\"https:\/\/opensource.com\/points-and-badges\" class=\"field field--name-name field--type-string field--label-above profile__badges__name profile__badges__color-\">People&#8217;s Choice Award<\/a><\/p>\n<p>  <a href=\"https:\/\/opensource.com\/points-and-badges\" class=\"field field--name-name field--type-string field--label-above profile__badges__name profile__badges__color-\">People&#8217;s Choice Award 2018<\/a><\/p>\n<p>  <a href=\"https:\/\/opensource.com\/points-and-badges\" class=\"field field--name-name field--type-string field--label-above profile__badges__name profile__badges__color-\">Author<\/a><\/p>\n<p>  <a href=\"https:\/\/opensource.com\/correspondent-program\" class=\"field field--name-name field--type-string field--label-above profile__badges__name profile__badges__color-\">Correspondent<\/a><\/p>\n<p>  <a href=\"https:\/\/opensource.com\/resources\/contributor-club\" class=\"field field--name-name field--type-string field--label-above profile__badges__name profile__badges__color-\">Contributor Club<\/a><\/p>\n<\/div>\n<\/article>\n<\/div><\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?destination=\/feed&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fix scanned images with ImageMagick Seth Kenlon Sat, 11\/05\/2022 &#8211; 03:00 Years ago while rummaging through the contents of a shelf in a used bookstore, I happened upon a booklet titled &#8220;UNIX System Command Summary for Berkeley 4.2 &amp; 4.3 BSD,&#8221; published by\u00a0Specialized Systems Consultants. I bought it as a curiosity item because it was [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":68395,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-68394","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/68394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68394"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/68394\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/68395"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}