ThinkPython C02

From XPUB & Lens-Based wiki
Revision as of 20:33, 23 September 2010 by Migratebot (talk | contribs) (Created page with "== Exercise X2.1 == A common problem when working with time-based media (audio / movies) is to translate between various representations of "timecodes". For instance, subtitle...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Exercise X2.1

A common problem when working with time-based media (audio / movies) is to translate between various representations of "timecodes". For instance, subtitles for movies can be written as a text file in the "SubRip" format, where the starting time of a particular subtitle looks like this:

 01:12:08,000 --> 01:12:10,520
 We both know that the mind
 of a woman in love...


So, the format for a time code in a SubRip file is:

 HH:MM:SS,FFF


Where the "HH" means the hour, "MM" the minutes, "SS" the seconds, and "FFF" the fractional part of the time (always written with three numbers).

Use python as a calculator to convert the pair of time codes shown above into a pair of number of seconds representing the same times (e.g. the timecode 00:01:30,000 ==> 90 seconds). Show the calculations you make and try to write each one as a single line (so one line of calculation per timecode).

Exercise X2.2

The most common way to represent colors in the computer is as a triplet of 3 numbers (or "components") where each number represents the level of red, green, and blue that when mixed together would produce the desired color. This triplet is called the RGB value of a color. Often the range of numbers is used between 0 (no color) and 255 (full color). For instance, pure red would be the triplet (255, 0, 0) and full-saturation yellow (red and green with no blue) would be (255, 255, 0).

A simple way to "mix" two colors is to calculate the average of each color component. Calculate the color that would result from mixing yellow with pure blue and show your calculation as typed to Python. Based on the resulting numbers, can you already say anything about what the color might look like? You can use most image editing softwares like the GIMP or Photoshop to see the result.

1. A number between 0 and 255 takes exactly 8 bits of computer memory. An RGB value would then take 24 bits (8*3). Most graphics displays work with "24bit" color. In this system, more than a 16 million unique colors can be specified.
2. Python recognizes lists of things when written in parentheses and separated by commas, as in (1, 2, 3). These are formally called "tuples" and are dealt with later on in the textbook.
3. You can see the effect of this kind of transformation in an image editor by creating two layers with overlapping boxes in each color. If you then turn the opacity (or transparency) of one of the layers to 50%, you should see the resulting "mix" color.