Jed Rembold & Fred Agbo
March 8, 2024
What is the third element (index 2) in the below list?
[i * 4 for i in "Oct 21, 2022" if not i.isalpha() and not i.isspace()]
21
",,,,"
"tttt"
"2222"
[ [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)
Image data is commonly stored in two-dimensional arrays
Each element stores information about the pixel that exists at that location
The 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.
We could get the pixels from our example image using:
image = GImage("VLA_Moonset.jpg")
pixels = image.get_pixel_array()
The first index of the pixel array gets you the row, the second index gets you the column
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 |
The general approach for reading a text file is to first open the file and associate that file with a variable, commonly called its file handle
We will also use the with keyword to ensure that Python
cleans up after itself (closes the file) when we are done with it (Many
of us could use a with
irl)
with open(filename) as file_handle:
# Code to read the file using the file_handle
Python gives you several ways to actually read in the data
read
reads the entire file in as a
stringreadline
or
readlines
reads a single line or lines from
the fileread
alongside
splitlines
gets you a list of line
stringsThe read
method reads the entire file
into a string, with includes newline characters
(\n
) to mark the end of lines
Simple, but can be cumbersome to work with the newline characters, and, for large files, it can take a large amount of memory
As an example, the file:
One fish
two fish
red fish
blue fish
would get read as
"One fish\ntwo fish\nred fish\nblue fish"
Of the ways to read the file in a string at a time, using the file handler as an iterator and looping is probably best and certainly most flexible
Leads to code that looks like:
with open(filename) as f:
for line in f:
# Do something with the line
Note that most strategies preserve the newline character, which you very likely do not want, so be ready to strip them out before doing more processing
So long as your files are not gigantic, using
read
and then the
splitlines
method can be a good
option
This does remove the newline characters, since it splits the string at them
with open(filename) as f:
lines = f.read().splitlines()
# Then you can do whatever you want with the list of lines
import random
def name_mangler(filename):
"""
Reads from a roster of first names and then randomly chooses two to cut in half
and recombine with the other. Then prints off both combinations.
Inputs:
filename (string): The filename containing the names
Outputs:
None
"""
def get_names(filename):
"""Reads in the roster. """
with open(filename) as fh:
names = fh.read().splitlines()
return names
def choose_two(name_list):
""" Chooses two different names from the list. """
name1 = random.choice(name_list)
name2 = random.choice(name_list)
while name1 == name2:
name2 = random.choice(name_list)
return [name1, name2]
def slice_and_combine(name1, name2):
""" Slices and recombines both names, printing to the screen. """
name1_mid = len(name1)//2
name2_mid = len(name2)//2
print(name1[:name1_mid] + name2[name2_mid:])
print(name2[:name2_mid] + name1[name1_mid:])
names = get_names(filename)
chosen = choose_two(names)
slice_and_combine(chosen[0], chosen[1])
if __name__ == '__main__':
name_mangler('class_first_names.csv')