{"id":156,"date":"2006-11-26T17:39:26","date_gmt":"2006-11-26T16:39:26","guid":{"rendered":"http:\/\/ospublish.constantvzw.org\/?p=156"},"modified":"2008-09-08T17:51:05","modified_gmt":"2008-09-08T16:51:05","slug":"convert-tiff-to-transparent-png","status":"publish","type":"post","link":"http:\/\/ospublish.constantvzw.org\/blog\/tools\/convert-tiff-to-transparent-png","title":{"rendered":"Convert tiff to transparent PNG"},"content":{"rendered":"
Since long, we wished to write about scripting for image creation and manipulation. There are many reason why you would spend some time to do it. To resize a lot of images by hand can be a tedious task, or your software misses a component to achieve a particular result. Or you want to turn a web application into an image editor, etc.<\/p>\n
<\/p>\n
We will start with a modest example taken from a real life situation. We, Femke and Nicolas, are working on an illustration in Inkscape. For this illustration, we have scanned a lot of notes we have written on paper. The scanned images have been saved in tiff. We have imported them in Inkscape and started making the composition. Half-way we realise that it should be a lot more easier to work with the same images but saved as PNG with a transparent background. As there is 165 images to transform, to do it one by one in Gimp sounds just frightening. This is where the wonderful Imagemagick<\/a> software enters into play. convert myfile.tiff -transparent white myfile.png<\/p><\/blockquote>\n To apply it to a whole directory of images and keep the filenames, we had to include it in a small shell script:<\/p>\n The fuzz<\/em> parameter makes it possible to give transparency to ‘nearly-white’ pixels.<\/p>\n","protected":false},"excerpt":{"rendered":" Since long, we wished to write about scripting for image creation and manipulation. There are many reason why you would spend some time to do it. To resize a lot of images by hand can be a tedious task, or your software misses a component to achieve a particular result. Or you want to turn […]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[38],"tags":[85,86,23],"_links":{"self":[{"href":"http:\/\/ospublish.constantvzw.org\/blog\/wp-json\/wp\/v2\/posts\/156"}],"collection":[{"href":"http:\/\/ospublish.constantvzw.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ospublish.constantvzw.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ospublish.constantvzw.org\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/ospublish.constantvzw.org\/blog\/wp-json\/wp\/v2\/comments?post=156"}],"version-history":[{"count":1,"href":"http:\/\/ospublish.constantvzw.org\/blog\/wp-json\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":747,"href":"http:\/\/ospublish.constantvzw.org\/blog\/wp-json\/wp\/v2\/posts\/156\/revisions\/747"}],"wp:attachment":[{"href":"http:\/\/ospublish.constantvzw.org\/blog\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ospublish.constantvzw.org\/blog\/wp-json\/wp\/v2\/categories?post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ospublish.constantvzw.org\/blog\/wp-json\/wp\/v2\/tags?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n
\nImagemagick is shipped with every major linux distribution or can easily be installed by the different package managers. It is also available on windows; and on MacosX via the Fink installer. Once there, Imagemagick gives you many tools to edit, resize, transform images. One of them is convert<\/em> that takes a file in input and converts it into (nearly) any format. In our case, a simple conversion was not enough since we wanted also to transform the white colour into a transparent background. The following command did the trick for one image:<\/p>\n\n
#!\/bin\/sh\r\nfor file in `ls | grep tiff`\r\ndo\r\n convert \"$file\" -fuzz 5% -transparent white \"${file}.png\"\r\n echo \"writing ${file}.png\"\r\ndone<\/pre>\n<\/blockquote>\n