Jed Rembold & Fred Agbo
March 10, 2025
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 |
from pgl import GWindow, GOval, GImage
gw =GWindow(600,400)
image = GImage("Moon.png", 0,0)
image.scale(gw.get_width()/image.get_width())
gw.add(image)
def imagetreshold(e):
TRESHOLD = 130
pixel = image.get_pixel_array()
#print(pixel)
for r in range(len(pixel)):
for c in range(len(pixel[0])):
value = pixel[r][c]
red =GImage.get_red(value)
if red< TRESHOLD:
pixel[r][c]= GImage.create_rgb_pixel(0,0,0)
else:
pixel[r][c] = GImage.create_rgb_pixel(255,255,255)
# You must create a new Gimage
new_image = GImage(pixel)
gw.add(new_image)
gw.add_event_listener("click", imagetreshold)