Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an image to 1bit/px binary bitmap with ImageMagick or RMagick?

I'm looking for the command and options to convert images (png, jpeg, whatever…) to 1bit/px bitmap image (similar to what happen when in photoshop when you convert it to bitmap). See example below

I'd like to do it programatically from within a rails app (e.g. RMagick or through ImageMagick)

enter image description here

The following works :

convert yo.jpg -remap pattern:gray30 mono.gif

but I' like to understand better what's happening, compared to what phosotsohp would do in that case; considering the many possibilities explained here (@roger_rowland thx for the link again)

In the end what I want to do is to have this "effect", and to enlarge the image without ending up with a blurry effect

like image 461
Ben Avatar asked Apr 07 '13 09:04

Ben


People also ask

What can I do with ImageMagick?

ImageMagick can resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.

Can ImageMagick convert HEIC to JPG?

If you have the free command-line tool ImageMagick installed on your computer, you can convert an HEIC file to any other format using the convert tool.

Is ImageMagick fast?

ImageMagick provides a fast, simple way to automate image resizing.

What does ImageMagick do in Linux?

ImageMagick is a free and open source, feature-rich, text-based, and cross-platform image manipulation tool used to create, edit, compose or convert bitmap images. It runs on Linux, Windows, Mac Os X, iOS, Android OS, and many other operating systems.


1 Answers

-monochrome

This option uses some smart dithering and generates very visible output:

convert -monochrome in.png out.png

Documentation: http://www.imagemagick.org/Usage/quantize/#monochrome

Compare that to a simpler -threshold 50 transform:

convert -threshold 50 in.png out.png

which loses most of the image. We could likely have improved that with a better -threshold value, but the beauty of -monochrome is that it is smarter and often works without us having to guess anything.

Concrete example from: https://www.nasa.gov/mission_pages/galex/pia15416.html

wget -O orig.jpg http://www.nasa.gov/images/content/650137main_pia15416b-43_full.jpg
# Downsize to 400 height to have a reasonable file size for upload here.
convert orig.jpg -resize x400 in.jpg
convert -monochrome in.jpg out.jpg
convert -threshold 50 in.jpg threshold-50.jpg

in.jpg

enter image description here

out.jpg

enter image description here

threshold-50.jpg

enter image description here

Related questions:

  • https://askubuntu.com/questions/9868/convert-an-image-from-grayscale-to-binary
  • https://superuser.com/questions/75373/convert-color-photos-of-documents-to-good-black-and-white-bitonal-images
  • https://unix.stackexchange.com/questions/108613/how-do-you-binarize-a-colored-image

Tested in Ubuntu 19.10, ImageMagick 6.9.10.