<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>transparent | LogikDevelopment</title>
	<atom:link href="https://www.logikdev.com/tag/transparent/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.logikdev.com</link>
	<description>&#34;Il n&#039;y a pas de problème, il n&#039;y a que des solutions. L&#039;esprit de l&#039;homme invente ensuite le problème.&#34; André Gide</description>
	<lastBuildDate>Tue, 25 Apr 2017 13:57:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>
	<item>
		<title>Make image backgrounds transparent with tolerance</title>
		<link>https://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/</link>
					<comments>https://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/#comments</comments>
		
		<dc:creator><![CDATA[smoreau]]></dc:creator>
		<pubDate>Wed, 05 Oct 2011 21:33:48 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[image background]]></category>
		<category><![CDATA[tolerance]]></category>
		<category><![CDATA[transparent]]></category>
		<guid isPermaLink="false">https://www.logikdev.com/?p=1435</guid>

					<description><![CDATA[<p>In one of the projects I am working on at the moment, I needed to convert the background colour of an image to be transparent so the image looks better on a non-white background. Looking on the Web, I found the following article from Dustin Marx: Making White Image Backgrounds Transparent with Java 2D/Groovy If [&#8230;]</p>
<p>The post <a href="https://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/">Make image backgrounds transparent with tolerance</a> first appeared on <a href="https://www.logikdev.com">LogikDevelopment</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In one of the projects I am working on at the moment, I needed to convert the background colour of an image to be transparent so the image looks better on a non-white background.</p>
<p>Looking on the Web, I found the following article from Dustin Marx:<br />
<a href="http://www.javaworld.com/community/node/7629" title="Making White Image Backgrounds Transparent with Java 2D/Groovy" target="_blank">Making White Image Backgrounds Transparent with Java 2D/Groovy</a><br />
If the link is broken, please download his code from the following link: <a href='/wp-content/uploads/2011/10/ImageTransparency.java_.zip'>ImageTransparency.java</a></p>
<p>The method which makes the background colour transparent is called <code>makeColorTransparent</code>. This method works pretty well, except in some cases as shown in the example below:</p>
<table style="width:100%;border:0px;">
<tr>
<td style="width:50%;border:0px;">
<img decoding="async" src="https://www.logikdev.com/wp-content/uploads/2011/10/geek-inside-original.jpg" title="Original image" width="100" height="100" class="aligncenter size-full wp-image-1454" />
</td>
<td style="width:50%;border:0px;">
<img decoding="async" src="https://www.logikdev.com/wp-content/uploads/2011/10/geek-inside-0.jpg" alt="" title="Converted image using Dustin's method" width="100" height="100" class="aligncenter size-full wp-image-1457" />
</td>
</tr>
<tr>
<td style="border:0px;text-align:center;vertical-align:top;">
<em>Original image</em>
</td>
<td style="border:0px;text-align:center;vertical-align:top;">
<em>Converted image<br />using Dustin&#8217;s method</em>
</td>
</tr>
</table>
<p>This is actually quite normal. Indeed, his code is converting a specific colour (#FFFFFF in our case) to be transparent. But what if the background is not homogeneous?</p>
<p>This is the reason why I had to modify his method to add a new parameter called <code>tolerance</code>:</p>
<pre class="brush: java; title: ; notranslate">
private Image makeColorTransparent(final BufferedImage im, final Color color, int tolerance) {
    int temp = 0;
    if (tolerance &lt; 0 || tolerance &gt; 100) {
        System.err.println(&quot;The tolerance is a percentage, so the value has to be between 0 and 100.&quot;);
        temp = 0;
    } else {
        temp = tolerance * (0xFF000000 | 0xFF000000) / 100;
    }
    final int toleranceRGB = Math.abs(temp);
		
    final ImageFilter filter = new RGBImageFilter() {
        // The color we are looking for (white)... Alpha bits are set to opaque
        public int markerRGBFrom = (color.getRGB() | 0xFF000000) - toleranceRGB;
        public int markerRGBTo = (color.getRGB() | 0xFF000000) + toleranceRGB;

        public final int filterRGB(final int x, final int y, final int rgb) {
            if ((rgb | 0xFF000000) &gt;= markerRGBFrom &amp;&amp; (rgb | 0xFF000000) &lt;= markerRGBTo) {
                // Mark the alpha bits as zero - transparent
                return 0x00FFFFFF &amp; rgb;
            } else {
                // Nothing to do
                return rgb;
            }
        }
    };

    final ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}
</pre>
<p>Such as Photoshop, the tolerance is a percentage value between 0 and 100. The higher the tolerance is, the bigger the range of colours will be.</p>
<p>Let&#8217;s take our previous example and apply a 50% tolerance:</p>
<p><img decoding="async" src="https://www.logikdev.com/wp-content/uploads/2011/10/geek-inside-50.jpg" alt="" title="geek-inside-50" width="100" height="100" class="aligncenter size-full wp-image-1472" /></p>
<p>That looks much better, isn&#8217;t it? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p><p>The post <a href="https://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/">Make image backgrounds transparent with tolerance</a> first appeared on <a href="https://www.logikdev.com">LogikDevelopment</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
