Science and technology

Repair scanned pictures with ImageMagick

Years in the past whereas rummaging by way of the contents of a shelf in a used bookstore, I occurred upon a booklet titled “UNIX System Command Summary for Berkeley 4.2 & 4.3 BSD,” printed by Specialized Systems Consultants. I purchased it as a curiosity merchandise as a result of it was almost 20 years previous but nonetheless largely relevant to trendy Linux and BSD.

That amused me then and now. A booklet written in 1986 was nonetheless largely related in 2016, whereas books on the identical shelf a couple of proprietary OS weren’t well worth the paper they have been printed on. (Think about it: What expertise do you suppose goes to outlive a zombie apocalypse?) I’ve had the booklet by myself bookshelf for a number of years now, nevertheless it occurred to me that it is most likely value doing a bit of digital preservation of this artifact, so I made a decision to scan the booklet to create a CBZ ebook.

Scanning was simple, albeit time-consuming, with Skanlite. After I used to be completed, nevertheless, I found that some pages weren’t fairly stage.

(Seth Kenlon, CC BY-SA 4.0)

In printing, that is known as a registration drawback, that means that the place of what is being printed is not accurately oriented on the web page.

More open supply options

ImageMagick

ImageMagick is a non-interactive terminal-based graphics editor. It might sound counterintuitive to attempt to edit a graphic in a graphic-less atmosphere like a text-only terminal, nevertheless it’s really quite common. For occasion, once you add a picture to make use of as a profile image to an online utility, it is probably {that a} script on the appliance’s server processes your picture utilizing ImageMagick or its libraries. The benefit of a non-interactive editor is that you may formulate what must be finished to a pattern picture, then apply these results to lots of of different pictures on the press of a button.

ImageMagick is mostly simply as succesful as any graphics editor, so long as you are taking the time to uncover its many capabilities and tips on how to mix them to realize the specified results. In this case, I need to rotate pages which are askew. After looking out by way of ImageMagick’s documentation, I found that the ImageMagick time period for the answer I wanted was known as deskew. Aligning your terminology with someone else’s terminology is a problem in something that you do not already know, so once you method ImageMagick (or something), understand that the phrase you have determined describes an issue or answer is probably not the identical phrase utilized by another person.

To deskew a picture with crooked textual content utilizing ImageMagick:

$ convert page_0052.webp -deskew 25% fix_0052.webp

The -deskew possibility represents the edge of acceptable skew. A skew is set by tracing peaks and valleys of objects that look like letters. Depending on how crooked your scan is, it’s possible you’ll want roughly than 25% threshold. I’ve gone as excessive as 80%, and up to now nothing beneath 25% has had an impact.

Here’s the consequence:

(Seth Kenlon, CC BY-SA 4.0)

Fixed! Applying this to the remaining 55 pages of the doc fastened skewed pages whereas doing nothing to pages that have been already straight. In different phrases, it was protected to run this command on pages that wanted no adjustment, because of my threshold setting.

Cropping a picture with ImageMagick

After correcting for a skew, and since I scanned extra of every web page than vital anyway to forestall unintentionally slicing off phrases, I made a decision that it made sense to crop my corrected pages. I used to be joyful to maintain some area across the margins, however not fairly as a lot as I had. I take advantage of the crop perform of ImageMagick usually sufficient for pictures on this very web site, so I used to be accustomed to the choice. However, I wanted to find out tips on how to crop every web page.

First, I wanted the scale of the picture:

$ determine fixed_0052.webp
WEBP 1128x2593 1128x2593+0+0 8-bit sRGB 114732B 0.020u 0:00.021

Knowing the scale, I used to be capable of make some estimations about what number of pixels I may stand to lose. After a number of trial runs, I got here up with this:

convert fix_0052.webp -gravity Center -crop 950x2450+0+0 crop_0052.webp

This is not an actual match, nevertheless it proved vital once I utilized it to different pictures within the booklet. The pages various in content material and scanner placement right here and there, so I used to be joyful to provide each a bit of respiration room.

Here’s the corrected and cropped picture:

(Seth Kenlon, CC BY-SA 4.0)

Batch picture modifying with open supply

The great thing about ImageMagick is that after you have found out the formulation for fixing your picture, you’ll be able to apply that repair to all pictures requiring the identical repair. I do that with GNU Parallel, which makes use of all my CPU cores to complete picture correction throughout lots of of pages. It would not take lengthy, and the outcomes converse for themselves. More importantly, I’ve received a digital archive of a enjoyable artifact of UNIX historical past.

Most Popular

To Top