Difference between revisions of "CSC111 Why 255?"
(→Octal Dump) |
(→Octal Dump) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 16:14, 13 March 2014 (EDT) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 16:14, 13 March 2014 (EDT) | ||
---- | ---- | ||
+ | =Sample JES Program= | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # doNotChangeColor( image ). | ||
+ | # Changes the amount of red, green and blue that is in an image | ||
+ | def makeSquares( image ): | ||
+ | for x in range(0,getWidth(image)): | ||
+ | for y in range(0,getHeight(image)): | ||
+ | pixel = getPixel (image, x, y) | ||
+ | red = getRed(pixel) | ||
+ | green = getGreen(pixel) | ||
+ | blue = getBlue(pixel) | ||
+ | if x < getWidth(image)/2 and y < getHeight(image)/2: | ||
+ | newColor = makeColor( 255, 0, 0 ) | ||
+ | elif x > getWidth(image)/2 and y < getHeight(image)/2: | ||
+ | newColor = makeColor( 0, 250, 0 ) | ||
+ | elif x < getWidth( image )/2 and y > getHeight( image)/2: | ||
+ | newColor = makeColor( 0, 0, 240 ) | ||
+ | else: | ||
+ | newColor = makeColor( 100, 100, 100 ) | ||
+ | |||
+ | # the line below replaces the pixel with its original color. Change | ||
+ | # the amount of red, green and blue to see some change in the colors | ||
+ | setColor( pixel, newColor ) | ||
+ | return image | ||
+ | # ================================================================== | ||
+ | # MAIN PROGRAM | ||
+ | # ================================================================== | ||
+ | file = pickAFile() | ||
+ | image = makePicture( file ) | ||
+ | show( image ) | ||
+ | |||
+ | image = makeSquares( image ) | ||
+ | repaint( image ) | ||
+ | writePictureTo( image, file ) | ||
+ | </source> | ||
+ | <br /> | ||
=Sample Image= | =Sample Image= | ||
− | |||
<br /> | <br /> | ||
[[Media:square.bmp | square.bmp]] | [[Media:square.bmp | square.bmp]] | ||
Line 16: | Line 52: | ||
<br /> | <br /> | ||
'''od -t u1 square.bmp''' | '''od -t u1 square.bmp''' | ||
− | <source lang=" | + | <source lang="text"> |
0000000 66 77 230 4 0 0 0 0 0 0 54 0 0 0 40 0 | 0000000 66 77 230 4 0 0 0 0 0 0 54 0 0 0 40 0 | ||
0000020 0 0 20 0 0 0 20 0 0 0 1 0 24 0 0 0 | 0000020 0 0 20 0 0 0 20 0 0 0 1 0 24 0 0 0 | ||
Line 34: | Line 70: | ||
0002340 0 250 0 0 250 0 | 0002340 0 250 0 0 250 0 | ||
</source> | </source> | ||
+ | <br /> | ||
+ | =Bits= | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | def toBin( n, noBits ): | ||
+ | s = bin( n ) | ||
+ | s = "0" * noBits + s[2:] | ||
+ | return s[-noBits:] | ||
+ | |||
+ | noBits = int( input( "Number of bits? " ) ) | ||
+ | |||
+ | noCombinations = 2**noBits | ||
+ | print( "Number of combinations =", noCombinations ) | ||
+ | for i in range( noCombinations ): | ||
+ | print( "%3d %s" % ( i, toBin( i, noBits ) ) ) | ||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:Python]][[Category:CSC111]] |
Latest revision as of 21:15, 13 March 2014
--D. Thiebaut (talk) 16:14, 13 March 2014 (EDT)
Sample JES Program
# doNotChangeColor( image ).
# Changes the amount of red, green and blue that is in an image
def makeSquares( image ):
for x in range(0,getWidth(image)):
for y in range(0,getHeight(image)):
pixel = getPixel (image, x, y)
red = getRed(pixel)
green = getGreen(pixel)
blue = getBlue(pixel)
if x < getWidth(image)/2 and y < getHeight(image)/2:
newColor = makeColor( 255, 0, 0 )
elif x > getWidth(image)/2 and y < getHeight(image)/2:
newColor = makeColor( 0, 250, 0 )
elif x < getWidth( image )/2 and y > getHeight( image)/2:
newColor = makeColor( 0, 0, 240 )
else:
newColor = makeColor( 100, 100, 100 )
# the line below replaces the pixel with its original color. Change
# the amount of red, green and blue to see some change in the colors
setColor( pixel, newColor )
return image
# ==================================================================
# MAIN PROGRAM
# ==================================================================
file = pickAFile()
image = makePicture( file )
show( image )
image = makeSquares( image )
repaint( image )
writePictureTo( image, file )
Sample Image
identify square.bmp square.bmp BMP 20x20 20x20+0+0 8-bit sRGB 1.25KB 0.000u 0:00.000
ls -l square.bmp -rw-r--r-- 1 thiebaut wheel 1254 Mar 13 20:48 square.bmp
Octal Dump
od -t u1 square.bmp
0000000 66 77 230 4 0 0 0 0 0 0 54 0 0 0 40 0
0000020 0 0 20 0 0 0 20 0 0 0 1 0 24 0 0 0
0000040 0 0 176 4 0 0 0 0 0 0 0 0 0 0 0 0
0000060 0 0 0 0 0 0 240 0 0 240 0 0 240 0 0 240
0000100 0 0 240 0 0 240 0 0 240 0 0 240 0 0 240 0
0000120 0 240 0 0 100 100 100 100 100 100 100 100 100 100 100 100
0000140 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
0000160 100 100 240 0 0 240 0 0 240 0 0 240 0 0 240 0
...
0002200 0 0 255 0 0 255 0 0 255 0 0 255 100 100 100 0
0002220 250 0 0 250 0 0 250 0 0 250 0 0 250 0 0 250
0002240 0 0 250 0 0 250 0 0 250 0 0 0 255 0 0 255
0002260 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0
0002300 0 255 0 0 255 0 0 255 100 100 100 0 250 0 0 250
0002320 0 0 250 0 0 250 0 0 250 0 0 250 0 0 250 0
0002340 0 250 0 0 250 0
Bits
def toBin( n, noBits ):
s = bin( n )
s = "0" * noBits + s[2:]
return s[-noBits:]
noBits = int( input( "Number of bits? " ) )
noCombinations = 2**noBits
print( "Number of combinations =", noCombinations )
for i in range( noCombinations ):
print( "%3d %s" % ( i, toBin( i, noBits ) ) )