Files

Calvin (Deutschbein)

W9Wed: 23 Oct

Announcements

  • Time to start problem set 5.
  • Sections today and tomorrow.

Today

  • Files into PGL
  • Writing files

GImage

  • You can display an image from a file in PGL using GImage(filename, x, y)
  • `filename' should be something like:
  • Joint Photographic Experts Group (JPEG): fish.jpg
  • Portable Network Graphics (PNG): fish.png

GImage


Curl

System

  • These are free bonus slides.
  • We can use "curl" from inside Python, and in fact use any command, with Python's "system()", included in "os". >>> 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

System

  • I almost always work with files that are free and open source online, and I download them via curl.
  • So I wanted to show you too!
  • Here is code that downloads pgl, downloads an image, then displays the image in pgl. 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)

GImage

  • We only need to download once, so we remove the first three things after the first time we run the code... from pgl import * pix = 800 # number of pixels gw = GWindow(pix, pix) image = GImage("cat.png") gw.add(image)

Today

  • ✓ Files into PGL
  • Writing files

Files

  • Suppose we want to convert the image to text art, where some text character represents cat and some other character represents everything else.
  • We use a variety of pgl and list techniques to accomplish this, culminating in the new thing of writing to a file. pix = 800 # number of pixels gw = GWindow(pix, pix) image = GImage("cat.png") gw.add(image)

get_pixel_array()

  • What is a pixel array? Let's check. pix = 800 # number of pixels gw = GWindow(pix, pix) image = GImage("cat.png") gw.add(image) pixels = image.get_pixel_array() print(type(pixels))
  • We see the following: <class 'list'>
  • A list! A list of what? And how big?

get_pixel_array()

  • Let's look at the first thing in first list in pixels... pixels = image.get_pixel_array() print(len(pixels)) print(len(pixels[0])) print(type(pixels[0][0]))
  • We see the following: 1315 986 <class 'int'>
  • The image was taller than wide, so that 1315 should be the height.
  • And I guess each pixel is stored as an integer. Let's look at one.

Refresher


get_pixel_array()

  • And I guess each pixel is stored as an integer. Let's look at one. pixels = image.get_pixel_array() print(pixels[0][0])
  • We see the following: 4282337860
  • Okay I have no idea what that is.
  • Time to introduce something new!

GImage.get_red()

  • PGL stores pixels as integers, but provides a way to look at tbrightness of each color of a pixel.
  • The way to look at red brightness is 'GImage.get_red', and the other colors are similar. pixels = image.get_pixel_array() print(pixels[0][0]) print(GImage.get_red(pixels[0][0]))
  • We see the following: 4282337860 63
  • Ah! 63 is in the range from 0 to 255!
  • It's the red brightness!

GImage.get_red()

  • Juniper is orange/buff kitty likely to be brightest in red colors (I think).
  • She is sleeping on a stone/gray blanket that shouldn't have much red at all.
  • Let's print a dot '.' if brightness is below half (127)
  • Let's print zero '0' if brightness is above half (127)
  • Let's see what it looks like! for row in pixels: for pixel in row: if GImage.get_red(pixel) > 127: print('0') else: print('.')
  • Oh that prints everything on its own line. Yuck!

GImage.get_red()

  • We'll use a list comprehension for each row.
  • We'll only look at every ~20th pixel, to make it smaller.
  • Let's see what it looks like! 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)
  • Depending on how your VS Code is set up, I could finally see a cat here...

GImage.get_red()

  • Let's join each row to print strings instead of lists. 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)
  • This looks catlike to me, let me see if I can show you...

Text

    .................................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
    

Text

  • Changing row[::20] to row[::10] to make the image wider gives:
    .................................................................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
    
    

Today

  • ✓ Files into PGL
  • Writing files

File Write

  • Say I want to send a picture of my cat to someone with PGL.
  • I use PGL to make a text file and send the text file.
  • This is impractical but the best I can do on short notice.
  • The following is how W3Schools recommends writing a text file: 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())

File Write

  • Test it. >>> 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! >>>
  • The "30" is the number of letters written.
  • The "Whoops" is due to the fact this will delete any pre-existing content in the file.
    • Generally we want this - we write to a file, check if we like it, change the code, want to start over.

File Write

  • We'll open a file. We provide a filename and an opening type - "w" is for writing. f = open("cat.txt", "w")
  • Then we change all the "print" statements to 'f.write' 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)
  • This will do one annoying: print adds a newline, write does not.
  • This will write every '.' and '0' in the same row!

File Write

  • Add a '\n' to the end of each row. f = open("cat.txt", "w")
  • Then we change all the "print" statements to 'f.write' 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"
  • Let's look at it!

Announcements

  • Time to start problem set 5.
  • Sections today and tomorrow.