Wednesday, September 4, 2013

Removing the background of an image

While in the process of polishing up a paper, I had to draw figures using a well-known proprietary program for drawing chemical formula whose name I will not write down, out of decency (but you know which one it is anyway). I'm using the mac version (yes, no version for Linux) which has a neat glitch in that the "Save As" button does not work at all: no way to export your chemical formula as a SVG/PDF/PNG picture or whatever ! So I resorted to the neat Print as PDF feature of macintosh. I had to edit the resulting PDF files by hand as some of the colors were wrong (great !), and when I tried to include the PDF, I realized they had a white background... No way !

So I dug in ImageMagick's convert documentation, and came up with the following code that converts the PDF file into a PNG with a transparent background:

convert -density 600 figure.pdf -channel alpha \
  -fx '((r == 1 && g == 1 && b == 1) ? 0 : 1)' figure.png

There are still some few points that look white but are probably not that white (due to antialiased rendering of the PDF file ?). There are many more things that can be done using the -fx operator, this page was helpful to me !

Edit: while the alpha channel seems to be on by default for PDF files, it is not necessarily the case for all images. If the above doesn't work, try adding -alpha Set before the -fx bit.

26 comments:

Anonymous said...

Hi Vincent.
As a scientist myself, I understand perfectly:)
Anyhow, if the number of your PDFs is of reasonably small order, you could try to:
1. Import your PDF into GIMP
2. Use the function named, ehm, "transform color to alpha" (or something like that) in "colors" menu. Choose white.

Results are FAR better, I believe. Also, Elsevier totally digs EPS-es (and others) exported in GIMP, so it is one of my primary 'publication' tools:)

I wish your papers good reviews.
regards
w.

Vincent Fourmond said...

That's a good tip: the quality is indeed better: while with imagemagick there is still a bit of white lingering around the black lines of the chemical figure, gimp does the thing properly.

I have no clue what is the difference, though, since in principle that's the same rendering engine (ie ghostscript). They must be using different options (there are so many).

Downside is, it's not command-line enough for me ;-)!

Thanks for your kind words !

Vincent Fourmond said...

Got it, it's aliasing. If I convert to PPM beforehand using pdftoppm and disabling antialiasing or using the option +antialias, it looks better (though still not as good as gimp, I wonder how they do it !)

nurul said...

I visited your site.I liked your sites.You wrote about the background removal.It is very helpful for using photoshop.I will visite your site again as soon as possible.

Anonymous said...

Would tikz and its associated packages for chemical plotting not work for this particular use case?

chrysn said...

you could try opening the pdf in inkscape. that will allow you to ungroup the image, identify the background plane, and remove it. that way, your vector graphics will stay vector graphics. (worst case you'd have to run the pdf through pdf2ps/ps2pdf before.)

Nawsin said...

Resourceful Photoshop tutorial about Background remove of Image. This tutorial helps to grow up image editing technique.


Ghost Mannequin Editing Service
Photo Retouch Up
Knockout Service Provider

Rustam Ali said...

That is wonderful tutorial about background remove. thanks for sharing this useful post.
Clipping Creations India | ecommerce image editing service

Sonya said...

With adobe photoshop it seems easier to delete the background

clipping path with shadow said...

Really it was amazing post. Thank you for sharing with us.

Clipping Solutions said...
This comment has been removed by the author.
Clipping Solutions said...

Some truly interesting info, well written and broadly user-friendly.
clipping path service|Photo Retouching services|Vector Tracing

Anonymous said...

Enjoyed reading the article on top of, explains everything in detail, the article is extremely fascinating and effective to Clipping Path Lab. Thank you and smart luck for the approaching articles

Anonymous said...

Remove background from this app easily with out any designing skill.
Background Remover

abbasiMohamd said...

its so important to take your time with the pics and try to get the best out of it. great. thank you. abbasiMohamd

amanatullas said...

I have finally understood how to apply them to my own pictures! Thanks, amanatullas

abbasiMohamd said...

Thank you for this post, it’s really helpful, especially as my blog is pretty new, abbasiMohamd

cory brown said...

I’m just starting out and I find it really challenging to get the background right, it’s probably just as important as the outfit if you ask me! cory brown

Martin Jackson said...

Finally i learned how to remove background easily.
Clipping Path

Photo Retouching Services said...


This is very informative post. Thanks for sharing.




Photo Retouching Services

Batiste said...

This is an excellent blog you have shared. I have read and appreciated it guy. Clipping Path | Remove White Background | Product Photo Editing

Erin Davis said...

Remove background from this app easily without any designing skill.
You can use Ghost Mannequin Effect

Beverly Thompson said...

Photography is my passion, from my childhood life I am doing work in this sector. Love to write blogs on photography tips and tutorials also publish on various platforms. Last 5 years, I am working dedicatedly in this industry. Here I would like to share my Professional knowledge which has proper guidelines to enhance my newbie photography career. Thank you

Natalie Lanham said...

Are you a photographer looking to enhance your images and take your work to the next level? Check out this article on photo editing services for photographers. Discover how outsourcing your photo editing can save you time and help you deliver high-quality, professional results to your clients. Learn about the different types of photo editing services available, from basic retouching to advanced photo manipulation, and find out how to choose the right service provider for your needs. Read on to learn more and take your photography business to new heights!

Anna said...

Very good and more effective resources, thanks a lot

Kaylee Feeney said...

I’m always uplifted after reading your posts thank you for being such a reliable source of positivity

Visit Here: 3d modeling service