Home API Notes

Notes/Answers to questions

  • What are commands you use in terminal to access files?
  1. cd (change directory) to navigate to a specific directory
  2. ls to list the contents of the current directory
  3. rm to remove a file
  4. mv to move or rename a file
  5. cat to display the contents of a file
  • What are the command you use in Windows terminal to access files?
  1. cd to navigate to a specific directory
  2. dir to list the contents of the current directory
  3. type to display the contents of a file
  4. mkdir to create a new directory
  5. del to remove a file
  • What are some of the major differences?

The commands to access files are different between Terminal and Windows Terminal. Terminal is a command-line interface that is native to Unix-based systems such as Linux and macOS, while Windows Terminal is a command-line interface that is native to Windows. As a result, the commands used in Terminal are based on Unix commands, while the commands used in Windows Terminal are based on Windows commands.

Provide what you observed, struggled with, or leaned while playing with this code.

  • Why is path a big deal when working with images?

The path to an image file is critical when working with images because it determines where the image is stored on the system and how it is accessed by software applications. If the path is incorrect or the image cannot be found, this can lead to errors or issues with loading or processing the image.

  • How does the meta data source and label relate to Unit 5 topics?

In Unit 5, the concept of metadata is introduced as data that describes other data. In the context of images, metadata can provide additional information about the image file, such as the date it was created, the camera used to take the photo, or the location where the photo was taken. This can be useful in data analysis or machine learning tasks, as the metadata can provide additional context for the image data.

  • Look up IPython, describe why this is interesting in Jupyter Notebooks for both Pandas and Images?

IPython is an interactive shell for Python that provides a number of advanced features, such as tab completion, syntax highlighting, and the ability to execute code from the command line. In Jupyter Notebooks, IPython can be used to create rich, interactive data visualizations using libraries such as Pandas and Matplotlib. This allows users to explore and analyze data in a more interactive and dynamic way than is possible with traditional coding environments.

  • Where have you been a consumer of buffering?

As a consumer, I have experienced buffering in a variety of contexts, such as when I am streaming videos or music online, downloading large files, or transferring data between devices. I am also aware that buffering is commonly used in computer programming, where it can be used to optimize I/O operations.

  • From your consumer experience, what effects have you experienced from buffering?

From my consumer experience, I have noticed that when buffering is working properly, I may not notice any significant effects. However, if the buffering is not optimized or there is a large amount of data being buffered, I have experienced lag or delays in loading content. This can be frustrating and can result in a poor user experience.

  • How do these effects apply to images?

When working with images, buffering can be used to optimize I/O operations and improve performance. However, if the image file is very large or there are multiple images being loaded at once, buffering can result in lag or delays in displaying the images. This can impact the user experience, particularly if the images are being used for visual or artistic purposes. To mitigate the impact of buffering on image display, it is important to optimize the file size and resolution of the images and to use efficient buffering techniques.

  • Does this code seem like a series of steps are being performed?

Yes, the code performs a series of steps to prepare a list of images, set properties of the image, scale them, convert them to Base64 and create a grey scale representation of the image.

  • Describe Grey Scale algorithm in English or Pseudo code?

The Grey Scale algorithm converts each pixel of the image from RGB to grey scale by averaging the pixel values of red, green, and blue channels and then assigning this average value to all three channels.

  • Describe scale image? What is before and after on pixels in three images?

The scale_image function scales the image to a base width of 320 pixels while maintaining the aspect ratio of the image. The before image is the original image, and the after image is the scaled image.

  • Is scale image a type of compression? If so, line it up with College Board terms described?

Scale image can be considered a type of lossy compression, as it reduces the number of pixels in an image by resizing it, which can lead to a loss of detail and information.

2.2 College Board Quiz

I scored a 3/3 on this quiz, here are some of my takeaways...

  • Lossless compression algorithms guarantee the reconstruction of original data, while lossy compression algorithms do not. This is because lossless compression algorithms reduce the size of data without losing any information, while lossy compression algorithms remove some information that is considered less important, resulting in some loss of information and quality.

  • Lossy compression algorithms are typically more effective than lossless compression algorithms at reducing the size of data, resulting in faster transmission times. However, lossless compression algorithms are better for situations where it is necessary to reconstruct the original data without any loss of information.

  • Lossless compression of video files is typically less effective than lossy compression because video files contain a lot of information, so it can result in larger file sizes.

Questions

(1) Which of the following is an advantage of a lossless compression algorithm over a lossy compression algorithm?

Answer choices:(A) A lossless compression algorithm can guarantee that compressed information is kept secure, while a lossy compression algorithm cannot. (B) A lossless compression algorithm can guarantee reconstruction of original data, while a lossy compression algorithm cannot.

(C) A lossless compression algorithm typically allows for faster transmission speeds than does a lossy compression algorithm.

(D) A lossless compression algorithm typically provides a greater reduction in the number of bits stored or transmitted than does a lossy compression algorithm.

Correct answer:B> Why? A lossless compression algorithm can guarantee reconstruction of original data, while a lossy compression algorithm cannot. Lossless compression algorithms reduce the size of data without losing any information, so the original data can be reconstructed exactly. Lossy compression algorithms, on the other hand, reduce the size of data by removing some information that is considered less important, which can result in some loss of information and quality.

(2) A user wants to save a data file on an online storage site. The user wants to reduce the size of the file, if possible, and wants to be able to completely restore the file to its original version. Which of the following actions best supports the user’s needs?

Answer choices:(A) Compressing the file using a lossless compression algorithm before uploading it (B) Compressing the file using a lossy compression algorithm before uploading it

(C) Compressing the file using both lossy and lossless compression algorithms before uploading it

(D) Uploading the original file without using any compression algorithm

Correct answer:A> Why? Compressing the file using a lossless compression algorithm before uploading it. This will reduce the size of the file without losing any information, so the user can restore the original file exactly as it was before compression.

(3) A programmer is developing software for a social media platform. The programmer is planning to use compression when users send attachments to other users. Which of the following is a true statement about the use of compression?

Answer choices:(A) Lossless compression of video files will generally save more space than lossy compression of video files. (B) Lossless compression of an image file will generally result in a file that is equal in size to the original file.

(C) Lossy compression of an image file generally provides a greater reduction in transmission time than lossless compression does.

(D) Sound clips compressed with lossy compression for storage on the platform can be restored to their original quality when they are played.

Correct answer:C Lossy compression of an image file generally provides a greater reduction in transmission time than lossless compression does. Lossy compression algorithms are typically more effective than lossless compression algorithms at reducing the size of data, so they can result in faster transmission times. Lossless compression of video files is typically less effective than lossy compression because video files contain a lot of information, so it can result in larger file sizes.

Numpy Grey & RGB Scale Treatment

from IPython.display import HTML, display
from pathlib import Path  # https://medium.com/@ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f
from PIL import Image as pilImage # as pilImage is used to avoid conflicts
from io import BytesIO
import base64
import numpy as np

# prepares a series of images
def image_data(path=Path("images/"), images=None):  # path of static images is defaulted
    if images is None:  # default image
        images = [
            {'source': "Unknown", 'label': "Odd Smile", 'file': "odd_smile.png"}

        ]
    for image in images:
        # File to open
        image['filename'] = path / image['file']  # file with path
    return images

# Large image scaled to baseWidth of 320
def scale_image(img):
    baseWidth = 320
    scalePercent = (baseWidth/float(img.size[0]))
    scaleHeight = int((float(img.size[1])*float(scalePercent)))
    scale = (baseWidth, scaleHeight)
    return img.resize(scale)

# PIL image converted to base64
def image_to_base64(img, format):
    with BytesIO() as buffer:
        img.save(buffer, format)
        return base64.b64encode(buffer.getvalue()).decode()

# Set Properties of Image, Scale, and convert to Base64
def image_management(image):  # path of static images is defaulted        
    # Image open return PIL image object
    img = pilImage.open(image['filename'])
    
    # Python Image Library operations
    image['format'] = img.format
    image['mode'] = img.mode
    image['size'] = img.size
    # Scale the Image
    img = scale_image(img)
    image['pil'] = img
    image['scaled_size'] = img.size
    # Scaled HTML
    image['html'] = '<img src="data:image/png;base64,%s">' % image_to_base64(image['pil'], image['format'])
    
# Create Grey Scale and Color Base64 representations of Image
def image_management_add_html_color(image):
    # Image open return PIL image object
    img = image['pil']
    format = image['format']
    
    img_data = img.getdata()  # Reference https://www.geeksforgeeks.org/python-pil-image-getdata/
    image['data'] = np.array(img_data) # PIL image to numpy array
    image['gray_data'] = [] # key/value for data converted to gray scale
    image['red_data'] = []
    image['green_data'] = []
    image['blue_data'] = []

    # 'data' is a list of RGB data, the list is traversed and hex and binary lists are calculated and formatted
    for pixel in image['data']:
        # create gray scale of image, ref: https://www.geeksforgeeks.org/convert-a-numpy-array-to-an-image/
        average = (pixel[0] + pixel[1] + pixel[2]) // 3  # average pixel values and use // for integer division
        if len(pixel) > 3:
            gray_pixel = (average, average, average, pixel[3]) # PNG format
        else:
            gray_pixel = (average, average, average)
        image['gray_data'].append(gray_pixel)
        image['red_data'].append((pixel[0], 0, 0))
        image['green_data'].append((0, pixel[1], 0))
        image['blue_data'].append((0, 0, pixel[2]))
        
    img.putdata(image['gray_data'])
    image['html_grey'] = '<img src="data:image/png;base64,%s">' % image_to_base64(img, format)

    img.putdata(image['red_data'])
    image['html_red'] = '<img src="data:image/png;base64,%s">' % image_to_base64(img, format)

    img.putdata(image['green_data'])
    image['html_green'] = '<img src="data:image/png;base64,%s">' % image_to_base64(img, format)

    img.putdata(image['blue_data'])
    image['html_blue'] = '<img src="data:image/png;base64,%s">' % image_to_base64(img, format)


# Jupyter Notebook Visualization of Images
if __name__ == "__main__":
    # Use numpy to concatenate two arrays
    images = image_data()
    
    # Display meta data, scaled view, and grey scale for each image
    for image in images:
        image_management(image)
        print("---- meta data -----")
        print(image['label'])
        print(image['source'])
        print(image['format'])
        print(image['mode'])
        print("Original size: ", image['size'])
        print("Scaled size: ", image['scaled_size'])
        
        print("-- original image --")
        display(HTML(image['html'])) 
        
        print("--- grey image ----")
        image_management_add_html_grey(image)
        display(HTML(image['html_grey']))
        print("--- red image ---")
        image_management_add_html_color(image)
        display(HTML(image['html_red']))
        print("--- green image ---")
        display(HTML(image['html_green']))
        print("--- blue image ---")
        display(HTML(image['html_blue']))
    print()
---- meta data -----
Odd Smile
Unknown
PNG
RGBA
Original size:  (651, 621)
Scaled size:  (320, 305)
-- original image --
--- grey image ----
--- red image ---
--- green image ---
--- blue image ---

from PIL import Image
from IPython.display import display

def scale_image(img):
    baseWidth = 150
    scalePercent = (baseWidth/float(img.size[0]))
    scaleHeight = int((float(img.size[1])*float(scalePercent)))
    scale = (baseWidth, scaleHeight)
    return img.resize(scale)

red = Image.open('/home/etran/vscode/fastpages_EthanT/_notebooks/images/redcube.png')
red = scale_image(red)

cano = Image.open('/home/etran/vscode/fastpages_EthanT/_notebooks/images/oahu_volcano.png')
cano = scale_image(cano)

display(red)
display(cano)

Data Compression Hacks

When it comes to data compression, the image of a pure red cube is typically more suited for lossless compression, while the image of a volcano is more likely to be compressed using lossy compression. ## Red Cube The red cube image is less complex, with minimal variation in color and texture. As a result, it can be compressed without any loss of data. Lossless compression algorithms can efficiently identify and remove any redundant data without compromising the quality of the image. Consequently, a lossless compression algorithm is more likely to be used for compressing the red cube image. ## Volcano On the other hand, the image of a volcano contains much more complexity, such as variations in color, texture, and detail. When compressing this image using lossy compression algorithms, some of this complexity will be lost in order to achieve a smaller file size. This loss of information can result in a lower quality image compared to the original. However, lossy compression algorithms are often used for photographic images because they can achieve much smaller file sizes than lossless compression algorithms, while still maintaining a high enough quality for most purposes. Therefore, the image of the volcano is more likely to be compressed using a lossy compression algorithm.