Calvin (Deutschbein)
W9Wed: 23 Oct
GImage
GImage(filename, x, y)
fish.jpg
fish.png
GImage
C:\Users\cd-desk> python
>>> print("hi")
hi
>>> exit()
C:\Users\cd-desk> curl https://raw.githubusercontent.com/cd-public/cd-public.github.io/refs/heads/main/courses/cs1f24/slides/imgs/cat.png -o cat.png
C:\Users\cd-desk>
>>> from os import *
>>> system("curl https://raw.githubusercontent.com/cd-public/cd-public.github.io/refs/heads/main/courses/cs1f24/slides/imgs/cat.png -o cat.png")
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2362k 100 2362k 0 0 8004k 0 --:--:-- --:--:-- --:--:-- 8064k
0
from os import system
system("curl https://raw.githubusercontent.com/rembold-cs151-master/HW03/refs/heads/master/pgl.py -o pgl.py")
system("curl https://raw.githubusercontent.com/cd-public/cd-public.github.io/refs/heads/main/courses/cs1f24/slides/imgs/cat.png -o cat.png")
from pgl import *
pix = 800 # number of pixels
gw = GWindow(pix, pix)
image = GImage("cat.png")
gw.add(image)
from pgl import *
pix = 800 # number of pixels
gw = GWindow(pix, pix)
image = GImage("cat.png")
gw.add(image)
pix = 800 # number of pixels
gw = GWindow(pix, pix)
image = GImage("cat.png")
gw.add(image)
get_pixel_array()
pix = 800 # number of pixels
gw = GWindow(pix, pix)
image = GImage("cat.png")
gw.add(image)
pixels = image.get_pixel_array()
print(type(pixels))
<class 'list'>
get_pixel_array()
pixels = image.get_pixel_array()
print(len(pixels))
print(len(pixels[0]))
print(type(pixels[0][0]))
1315
986
<class 'int'>
get_pixel_array()
pixels = image.get_pixel_array()
print(pixels[0][0])
4282337860
GImage.get_red()
pixels = image.get_pixel_array()
print(pixels[0][0])
print(GImage.get_red(pixels[0][0]))
4282337860
63
GImage.get_red()
for row in pixels:
for pixel in row:
if GImage.get_red(pixel) > 127:
print('0')
else:
print('.')
GImage.get_red()
def pixel_to_letter(pixel):
if GImage.get_red(pixel) > 127:
return '0'
return '.'
for row in pixels[::20]:
s = [pixel_to_letter(pixel) for pixel in row[::20]]
print(s)
GImage.get_red()
def pixel_to_letter(pixel):
if GImage.get_red(pixel) > 127:
return '0'
return '.'
for row in pixels[::20]:
row_list = [pixel_to_letter(pixel) for pixel in row[::20]]
s = "".join(row_list)
print(s)
.................................00............... ................................000..........0.... ...............................000.0.............. ................................0..0.............. .................................................. ..............................................0... ...............................................0.. .................................................. .................................................. ...............................................0.. ...............................................0.. .................................................. .........................0.00000.................. ...............0000000000000000.00..............0. .............0000000000000000000000.............0. ...........000000000.0000000000000000...........0. ..........00000000000000000000000000000.........0. .........0000000000000000000000000000000.......... ........000000000000000000000000000000000........0 ........0000000000000000000000000000000000........ .......000000000000000000000000000000000000....... ......00000000000000000000000000000000000000...... ......000000000000000000000000000000000000000..... .....0000000000000000000000000000000000000000..... ...0000000000000000000000000000000000000000000.... ..00000000000000000000000000000000000000000000.... ..000000000000000000000000000000000000000000000... ...00.00000000000000000000000000000000000000000... ........000000000000000000000000000000000000000... .........000000000000000000000000000000000000000.. .........000000000000000000000000000000000000000.. ..........00000000000000000000000000000000000000.. ..............0000000000000000000000000000000000.. ..0............00000000000000000000000000000000... .0000.........000000000000000000000000000000000... ..0000000....0000000000000000000000000000000000... ..000000000000000000000000000000000000000000000... ...0000000000000000000000000000000000000000000.... ...0000000000000000000000000000000000000000000.... .....00000000000000000000000000000000000000000.... ....00000000000000000000000000000000000000000..... .....0000000000000000000000000000000000000000..... ........000000000000000000000000000000000000...... ........000000000000000000000000000000000000...... .......000000....00000000000000000000000000....... .......000000000..0.00000000000000000000000....... .......0000000000000000000000000000000000......... ............0000000000000000000000000000.......... .............00000000000000000000000000........... ...............00000000000000000000000............ ..................0000000000000000000............. .......................0000000000000.............. ..............................000.0..........0.... 00000............................................. 000000000......................................... 00000000000....................................... 000000000000...................................... 00000000000000.................................... .00000000000000................................... 0000000000000000...............................00. .0000000000000000................................. ..000000000000000................................0 0.0000000000000000...............................0 0.0000000000000000................................ 00..000000000000000..............................0 0..00000000000000000............................00 |
.................................................................0000.............................. ...............................................................0000.00.0..................0........ ..............................................................0.000...0............................ ................................................................00.0.00....................0....... ...........................................................0....................................... ............................................................................................0...... ..............................................................................................00... .............................................................................................0..... .............................................................................................0..... ..............................................................................................0.... ..............................................................................................0.... ................................................................................................... ..................................................0..00.00000.0................................0... .............................0000000000000000000000000000.0000.00000...........................00.. ..........................0000000000000000000000000000000000000000000...........................0.. ......................000000000000000000.00000000000000000000000000000000.......................0.. ...................0000000000000000000000000000000000000000000000000000000000...................00. ..................0000000000000000000000000000000000000000000000000000000000000.................... ................000000000000000000000000000000000000000000000000000000000000000000................0 ................0000000000000000000000000000000000000000000000000000000000000000000................ .............0000000000000000000000000000000000000000000000000000000000000000000000000...........0. ............000000000000000000000000000000000000000000000000000000000000000000000000000............ ............00000000000000000000000000000000000000000000000000000000000000000000000000000.......... .........000000000000000000000000000000000000000000000000000000000000000000000000000000000......... ......0000000000000000000000000000000000000000000000000000000000000000000000000000000000000........ ...00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000....... ...000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...... ......0000.00000000000000000000000000000000000000000000000000000000000000000000000000000000000..... ...............0000000000000000000000000000000000000000000000000000000000000000000000000000000..... .................000000000000000000000000000000000000000000000000000000000000000000000000000000.... ..................00000000000000000000000000000000000000000000000000000000000000000000000000000.... ....................000000000000000000000000000000000000000000000000000000000000000000000000000.... ...........................00000000000000000000000000000000000000000000000000000000000000000000.... ...00........................00000000000000000000000000000000000000000000000000000000000000000..... ..0000000...................00000000000000000000000000000000000000000000000000000000000000000...... ....0000000000000........00000000000000000000000000000000000000000000000000000000000000000000...... ....00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...... ......00000000000000000000000000000000000000000000000000000000000000000000000000000000000000....... ......00000000000000000000000000000000000000000000000000000000000000000000000000000000000000....... ..........000000000000000000000000000000000000000000000000000000000000000000000000000000000........ ........0000000000000000000000000000000000000000000000000000000000000000000000000000000000......... ..........0000000000000000000000000000000000000000000000000000000000000000000000000000000.......... ...............0000000000000000000000000000000000000000000000000000000000000000000000000........... ...............000000000000000000000000000000000000000000000000000000000000000000000000............ ..............000000000000.......00000000000000000000000000000000000000000000000000000............. .............000000000000000000....00..00000000000000000000000000000000000000000000000............. ..............0000000.000000000000000000000000000000000000000000000000000000000000................. .......................000000000000000000000000000000000000000000000000000000000................... .........................00000000000000000000000000000000000000000000000000000..................... ..............................0000000000000000000000000000000000000000000000....................... ....................................0000000000000000000000000000000000000.......................... ..............................................00000000000000000000000.0............................ ............................................................00000...0.....................0........ 0000000000.................................................0....................................... 00000000000000000.................................................................................. 000000000000000000000.............................................................................. 000000000000000000000000........................................................................... 000000000000000000000000000........................................................................ .00000000000000000000000000000..................................................................... 0000000000000000000000000000000...............................................................0.0.. .00000000000000000000000000000000................................................................0. ...0000000000000000000000000000000................................................................0 00..0000000000000000000000000000000...............................................................0 0...0.000000000000000000000000000000............................................................... 0.00...0000000000000000000000000000000.......................................................0...00 00....000000000000000000000000000000000......................................................0.0000 |
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
#open and read the file after the overwriting:
f = open("demofile3.txt", "r")
print(f.read())
>>> f = open("demofile3.txt", "w")
>>> f.write("Woops! I have deleted the content!")
34
>>> f.close()
>>>
>>> #open and read the file after the overwriting:
>>> f = open("demofile3.txt", "r")
>>> print(f.read())
Woops! I have deleted the content!
>>>
f = open("cat.txt", "w")
for row in pixels[::10]:
# was 20, now 10
row_list = [pixel_to_letter(pixel) for pixel in row[::20]]
s = "".join(row_list)
f.write(s)
f = open("cat.txt", "w")
for row in pixels[::20]:
row_list = [pixel_to_letter(pixel) for pixel in row[::10]]
s = "".join(row_list)
f.write(s + '\n') # adding an "enter"