Calvin (Deutschbein)
Slides by Jed Rembold
11 March 2022
What would be the contents of the file Test.txt
after running the code to the right?
M = ord("A")
L = "".join([chr(M+i) for i in range(26)])
with open("Test.txt", "w") as f:
for i in range(len(L)):
if L[i] in "aeiou":
f.write("\n")
elif i % 2:
f.write(L[i].upper())
else:
f.write(L[i].lower())
aBcDeFgHiJkLmNoPqRsTuVwXyZ
AbCdEfGhIjKlMnOpQrStUvWxYz
BcD
FgH
JkLmN
PqRsT
VwXyZ
bCd
fGh
jKlMn
pQrSt
vWxYz
magic = [ [2, 9, 4], [7, 5, 3], [6, 1, 8] ]
magic[1][1] = 5
magic[-1][0] = 6
[ [2, 9, 4], [7, 5, 3], [6, 1, 8] ]
GImage
ClassGImage
class. GImage(filename, x, y)
filename
is the string containing the name of the file which contains the imagex
and y
are the coordinates of the upper left corner of the imagefish.gif
fish.jpg
fish.png
www.nasa.gov
can be freely used as long as you add an attribution linefrom pgl import GImage, GWindow, GLabel
def image_example():
gw = GWindow(800, 550)
image = GImage("VLA_Moonset.jpg")
image.scale(gw.get_width() / image.get_width())
gw.add(image)
citation = GLabel("Image Credit: Jeff Hellermann, NRAO / AUI / NSF")
citation.set_font("15px 'Sans-Serif'")
x = gw.get_width() - citation.get_width() - 10
y = image.get_height() + citation.get_ascent()
gw.add(citation, x, y)
GImage
class lets you convert between the image itself and the array representing the image contents by using the get_pixel_array
method, which returns a two-dimensional array of integers.image = GImage("VLA_Moonset.jpg")
pixels = image.get_pixel_array()
10010101
→ 0x95
00111001
→ 0x39
01100011
→ 0x63
#953963
or Function | Description |
---|---|
GImage.get_red(pixel) |
Returns the integer (0-255) corresponding to the red portion of the pixel |
GImage.get_green(pixel) |
Returns the integer (0-255) corresponding to the green portion of the pixel |
GImage.get_blue(pixel) |
Returns the integer (0-255) corresponding to the blue portion of the pixel |
GImage.get_alpha(pixel) |
Returns the integer (0-255) corresponding to the alpha portion of the pixel |
GImage.create_rgb_pixel(r,g,b) |
Returns a 32-bit integer corresponding to the desired color |