<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: transparent code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 08:00:49 GMT</pubDate>
    <description>DZone Snippets: transparent code</description>
    <item>
      <title>PNG-24 Alpha support for IE</title>
      <link>http://snippets.dzone.com/posts/show/1836</link>
      <description>What so much webdesigners dream about!&lt;br /&gt;Get alpha channel on web, that's possible with PNG-24 images and this trick.&lt;br /&gt;BE CAREFUL: that seem to work only for 10 images per page.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  # Display PNG-24 images with alpha channel on IE&lt;br /&gt;  # BE CAREFUL: with this trick, only 10 PNGs seems to be supported by IE&lt;br /&gt;  # Don't forget to set the size of your div&lt;br /&gt;  def transpng(id, png)&lt;br /&gt;    '&lt;style type="text/css"&gt;&lt;br /&gt;      &lt;!--&lt;br /&gt;        ' + id + ' {&lt;br /&gt;          background-image: url(' + png + ');&lt;br /&gt;        }&lt;br /&gt;      --&gt;&lt;br /&gt;    &lt;/style&gt;&lt;br /&gt;    &lt;!--[if IE]&gt;&lt;br /&gt;    &lt;style&gt;&lt;br /&gt;      ' + id + ' {&lt;br /&gt;        background-image: none;&lt;br /&gt;        filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + png + ', sizingMethod=\'scale\');&lt;br /&gt;      }&lt;br /&gt;    &lt;/style&gt;&lt;br /&gt;    &lt;![endif]--&gt;'&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 04 Apr 2006 02:03:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1836</guid>
      <author>Seb (S&#233;bastien Grosjean)</author>
    </item>
    <item>
      <title>Using transparent gif/png with Image's blitting</title>
      <link>http://snippets.dzone.com/posts/show/1383</link>
      <description>pys60's Image class will load a transparent gif/png&lt;br /&gt;just like a non-transparent one.&lt;br /&gt;But its blit() method accept a mask paramenter.&lt;br /&gt;I have talked about this in a &lt;a href=http://bigbold.com/snippets/posts/show/926&gt;previous snippet&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The missing link is to create a mask automatically&lt;br /&gt;from the Image. It's typically the top-left pixel.&lt;br /&gt;You can use Image's getpixel() which is undocumented.&lt;br /&gt;I show typical use of getpixel &lt;a href=http://bigbold.com/snippets/posts/show/832&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Combine them all, here's the automask function.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def automask(im):&lt;br /&gt;    width, height = im.size&lt;br /&gt;    mask = Image.new(im.size, '1') # black and white&lt;br /&gt;    tran = im.getpixel((0,0))[0]   # transparent top-left&lt;br /&gt;    for y in range(height):&lt;br /&gt;        line = im.getpixel([(x, y) for x in range(width)])&lt;br /&gt;        for x in range(width):&lt;br /&gt;            if line[x] == tran:&lt;br /&gt;                mask.point((x,y), 0)  # mask on the point&lt;br /&gt;    return mask&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;An example usage&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from graphics import Image&lt;br /&gt;&lt;br /&gt;ship = Image.open('E:\\Images\\ship.gif')&lt;br /&gt;mask = automask(ship)&lt;br /&gt;&lt;br /&gt;canvas.blit(ship, mask=mask)  # don't forget to create canvas first&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;A Sprite class can be defined based on this too.</description>
      <pubDate>Thu, 09 Feb 2006 13:03:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1383</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
