<?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>Create Blog &#187; Python</title>
	<atom:link href="http://create.tpsitulsa.com/blog/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://create.tpsitulsa.com/blog</link>
	<description>the create framework blog</description>
	<lastBuildDate>Sat, 30 Jan 2010 20:08:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Sprouting—Automated Spriting for SproutCore</title>
		<link>http://create.tpsitulsa.com/blog/2009/10/17/sprouting%e2%80%94automated-spriting-for-sproutcore/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/10/17/sprouting%e2%80%94automated-spriting-for-sproutcore/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 23:09:09 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[SproutCore]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[spriting]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=203</guid>
		<description><![CDATA[Well. Spriting. Do you know what it is? While I&#8217;ve included a very brief summary, you may want to just skip that part if you already know about it, or find a better description of the technique. Also, while this article is specifically aimed at SproutCore, the concepts (and the script download) can easily be [...]]]></description>
			<content:encoded><![CDATA[<p>Well. Spriting. Do you know what it is? While I&#8217;ve included a <em>very</em> brief summary, you may want to <a href="#what-is-it">just skip that part</a> if you already know about it, or find <a href="http://www.alistapart.com/articles/sprites">a better description</a> of the technique. Also, while this article is specifically aimed at SproutCore, the concepts (and the script download) can easily be applied elsewhere.</p>

<h3>Well, here are your options:</h3>

<ol>
    <li><strong>Read the whole article.</strong> Could be amusing, if I’m having a bad writing day.</li>
    <li><a href="#skip-to-it">Skip to the download and “how to use.”</a> No, I’m not going to explain the code. Well, not here, at any rate. I’ll explain it in the code itself. Yes, of course there are comments.</li>
</ol>

<h3 id = "what-is-it">￼What is Spriting?</h3>

<p><div id="attachment_211" class="wp-caption alignleft" style="width: 110px"><img src="http://create.tpsitulsa.com/blog/wp-content/uploads/2009/10/sprites-100x300.png" alt="Example Sprited Images" title="Sprites" width="100" height="300" class="size-medium wp-image-211" /><p class="wp-caption-text">Example Sprited Images</p></div>Spriting is a technique which combines multiple small images into one large image. This can be very beneficial, because it means that, instead of downloading, say, <em>100</em> small images for all the commonly used icons in your web application, the web browser downloads <em>one</em> bigger image (or, perhaps, two or three, but you get the picture).</p>

<p>The goal of spriting is to reduce the effect of latency on your web app’s performance.</p>

<p>To your left is an example set of sprited images.</p>

<h3>Using a Script</h3>

<p>You may thing that combining several images into one larger image might be a bit time-consuming and, overall, a tad challenging, especially if you are often updating the individual images.</p>

<p>You are not alone. I feel the same way.</p>

<p>However, I do know a little about writing scripts—even if I’d like to know a lot more; I’m still only learning Python. So, there is a simple answer:</p>

<p>Write a darn script!</p>

<h3 id = "skip-to-it">The Product</h3>

<p>If you just want the script and how to use it, you’re in luck. Because there are, in fact, some comments in the script, I’ll leave the explanation of how the code works for there.</p>

<p>You can <a href="http://github.com/ialexi/Spriter">download the code</a> from GitHub.</p>

<p>The script is written in Python. It requires:
Either: Python 2.6+ or Python 2.5 with simplejson installed
PIL (Python Imaging Library)</p>

<p>Very briefly, here is what it does:</p>

<ul>
    <li>Takes a “sets” folder, which should have sub-folder <em>sets</em> of icons to be sprited.</li>
    <li>Takes an output folder, under which it will create new set folders containing CSS and image files.</li>
    <li>Combines images under a maximum size (128&#215;128 by default—see Configuration) into a sprites.png file in the output set folder.</li>
    <li>Creates CSS referencing these images using a URL template (SproutCore oriented at first—see URL)</li>
</ul>

<h3>Sets</h3>

<p>You often might have more than one set of icons. For instance, you could have a main set, and then an alternate set that you only want to load on demand. You still want that additional set to be sprited.</p>

<p>Or, perhaps, you may have images meant for repeating backgrounds, but that you still want to sprite.</p>

<p>To facilitate this, the spriting script takes, as its first argument, the location to a sets folder. You put your sets as subfolders into this folder. For instance, this could be your folder layout:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">my-sets
	common
		delete-32.png
		delete-64.png
		refresh-32.png
		refresh-64.png
		refresh-512.png
	repeat-x
		config.js       — see Configuration
		toolbar.png
		footer.png
		header.png
	odd-feature
		some-icon.png
		other-icon.png</pre></div></div>


<h3>URLs</h3>

<p>The CSS has to reference the images. Only problem: it doesn’t know where those images are.</p>

<p>Usually, you’d just be able to use paths relative to the StyleSheet. However, if you are using SproutCore, this won’t work because it moves around all the CSS and JavaScript.</p>

<p>With SproutCore, you need something more like:</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">static_url<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'images/set/sprites.png'</span><span style="color: #00AA00;">&#41;</span></pre></div></div>


<p>This is what the script does by default. Just like above. It uses a URL template of</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">static_url<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'images/{set}/{image}'</span><span style="color: #00AA00;">&#41;</span></pre></div></div>


<p>It is a string which will be passed through the Python <tt>format</tt> function, with <em>set</em> and <em>image</em> as arguments.</p>

<h3>Configuration</h3>

<p>There are a few things that are configurable. Configuration is done per-set.</p>

<p>Configuration files are JSON files, and are very simple. They are optional, and take at most four parameters:</p>

<ul>
    <li><strong>max-size</strong>: Default: 128.<br />The maximum size for sprited images. Images above this size will be put as separate images. Useful if you have some really high-res icons you don’t want to sprite, but want to keep with the set. </li>
    <li><strong>repeat</strong>: Default: “none”; can also be “x” or “y”.<br /> Determines the repeat mode. If repeat is “x,” the sprites will be laid out in rows; if “y,” the sprites will be laid out in columns. The generated CSS will include repeat-x or repeat-y, respectively.  Note that all images in a repeat-x set must be the same width, and all images in a repeat-y set must be the same height. The width or height of these images should be set using repeat-width and repeat height, which are discussed below. </li>
    <li><strong>repeat-width</strong>: Default: 32.<br />If repeat=“x,” the width of the repeat images. If your repetition does not require a pattern, you could set this to 1. </li>
    <li><strong>repeat-height</strong>: See that lovely summary of repeat-width above? Well, this is the same, but for y/height.</li>
</ul>

<h3>The CSS</h3>

<p>As you may know, SproutCore automatically includes any CSS files. So, since this generates CSS files (and, if you provide the right argument, puts them directly into a SproutCore-watched folder), the CSS is automatically added. So, how do you use it?</p>

<p>Very simply. The script generates CSS rules in this format:</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.set-name</span> <span style="color: #6666ff;">.icon-name</span><span style="color: #6666ff;">.icon</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.set-name</span> <span style="color: #6666ff;">.icon-name</span><span style="color: #6666ff;">.icon</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/* CSS */</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>


<p>For example, to show &#8220;refresh-64&#8243; icon in set &#8220;common&#8221;, you&#8217;d do something like this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">ImageView.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">64</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">64</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  value<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;common refresh-64 icon&quot;</span> <span style="color: #006600; font-style: italic;">// using SproutCore's built-in className support for spriting</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>Or, as you may have noticed from the rule above, you can accomplish some basic theming very easily by treating set names as themes:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">View.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>left<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span><span style="color: #CC0000;">256</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">256</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;common&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// the theme</span>
  childViews<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;styledView&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  styledView<span style="color: #339933;">:</span> ImageView.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">64</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">64</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    value<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;refresh-64 icon&quot;</span> <span style="color: #006600; font-style: italic;">// using SproutCore's built-in className support for spriting</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>


<p>Nice and simple, eh?</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/10/17/sprouting%e2%80%94automated-spriting-for-sproutcore/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
