Multimedia Data Compression and Storage Lab Manual
The document outlines various programming exercises related to data compression and image processing, including implementations of Huffman coding, run-length encoding, Lempel-Ziv-Welch compression, arithmetic coding, and image conversion scripts. Each exercise includes an aim, algorithm, source code, and results demonstrating the effectiveness of the implemented algorithms. The exercises are designed for students in the Artificial Intelligence and Data Science department at Dhanalakshmi Srinivasan College of Engineering.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
88 views24 pages
Multimedia Data Compression and Storage Lab Manual
The document outlines various programming exercises related to data compression and image processing, including implementations of Huffman coding, run-length encoding, Lempel-Ziv-Welch compression, arithmetic coding, and image conversion scripts. Each exercise includes an aim, algorithm, source code, and results demonstrating the effectiveness of the implemented algorithms. The exercises are designed for students in the Artificial Intelligence and Data Science department at Dhanalakshmi Srinivasan College of Engineering.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 24
DHANALAKSHMI SRINIVASAN
COLLEGE OF ENGINEERING
(AUTONOMOUS) G&G
COIMBATORE - 641105 | www.dsce.ac.in
(Approved by AICTE, New Delhi & Affiliated to Anna University,
Chennai)
NH- 47, Palakkad Main Road, Navakkarai Po, Coimbatore — 641 105.
DEPARTMENT OF
ARTIFICIAL INTELLIGENCE AND DATA
SCIENCE
RECORD NOTEBOOK
Name:
Register No: ‘
Subject Code/Title:
Year/Semester:
Academic Year:DHANALAKSHMI SRINIVASAN
COLLEGE OF ENGINEERING
(AUTONOMOUS)
COIMBATORE - 641105 | www.dsce.ac.in
(Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai)
NH- 47, Palakkad Main Road, Navakkarai Po, Coimbatore — 641 105.
DEPARTMENT OF
ARTIFICIAL INTELLIGENCE AND DATA SCIENCE
CERTIFICATE
This is to certify that Mr./Ms.
Reg. No. a of, Semester
_Year B.Tech. (Artificial Intelligence and Data Science) has completed his/her practical
work in the.
laboratory during the academi¢ year 2024-25.
Faculty In-charge Head of the Department
Submitted for the University practical examination held on :
INTERNAL EXAMINER EXTERNAL
EXAMINERINDEX
S.No.
dimly
Name of the Experiment
No.
Mark
Staff
Sign.Ex.no:1 CONSTRUCT HUFFMAN CODES FOR GIVEN
Date: SYMBOL PROBABILITIES
Aim:
To implement the Huffman coding algorithm for a given set of symbol
probabilities and construct efficient prefix codes.
Algorithm:
1. Input: A list of symbols with their corresponding probabilities or
frequencies.
. Create a priority queue (min-heap) containing all symbols.
. While there is more than one node in the queue:
Remove two nodes with the lowest probabilities.
Create a new internal node with probability equal to the sum of two.
Assign 0 and 1 as edges to the two child nodes.
Insert the new node back into the queue.
4, Repeat until the queue contains only one node — the root of the Huffman
tree.
5.Traverse the tree to assign binary codes to cach symbol:
eee enn
Source Code:
import heapq
class Node:
def _init_(self, symbol, prob):
self.symbol = symbol
self.prob = prob
self.left = None
self.right = None
def _It_(self, other):
return self.prob < other.prob
def huffman_codes(symbols_probs):
heap = [Node(sym, prob) for sym, prob in symbols_probs.items()]
heapg.heapify(heap)
while len(heap) > 1:
left = heapg.heappop(heap)
right = heapg.heappop(heap)
new_node = Node(None, left.prob + right.prob)new_node.left = left
new_node.right = right
heapq.heappush(heap, new_node)
root = heap[0]
codes = {}
def generate_codes(node, code=""):
if node is not None:
if node.symbol is not None:
codes[node.symbol] = code
generate_codes(node.left, code + "0")
generate_codes(node.right, code + "1")
generate_codes(root)
return codes
Output:
Symbol Huffman Code
A 0
in
101
1001
1000
Ee oo ow
Result:
The Hufliman coding algorithm successfully generated variable-length, prefix-fiee
codes. Symbols with higher probabilities have shorter codes, minimizing the total
weighted path length.Ex.no:2 ENCODE RUN LENGTHS WITH FIXED-LENGTH
Date: CODE USING PYTHON
Aim:
To implement run-length encoding (RLE) using fixed-length codes to compress a
string by encoding repeated characters.
Algorithm:
1. Inputa string of characters.
2. Traverse the string while counting consecutive occurrences of each character.
3, Store each character followed by ifs run-length count,
4. Use a fixed-length binary representation for counts (e.g., 4-bit for max count 15).
5. Output the encoded string in character + binary count format.
Source Code:
def fixed_length_binary(n, bits=4):
return format(n, £0 {bits}b’)
def run_length_encode_fixed(s):
encoded = ""
i=0
while i /dev/null
then
echo "ImageMagick is not installed. Please install it using 'sudo apt install
imagemagick"
exit |
fi
# Loop through image files (add more extensions if needed)
for img in *.png *.bmp *.gif *.tiff; do
if [-f"Simg" J; then
filename="$ {img%.*}"
convert "Simg" "Sfilename.jpg”
echo "Simg converted to Sfilename.jpg"
fi
done
Output:
If the current directory contains the following files:
image L.pngphoto2.bmp
icon3.gif
After running the script:
image! jpg
photo2,jpg
icon3.jpg,
will be created in the same directory.
Result:
The shell script successfully converted all image files in the current
directory to JPEG format using ImageMagick.Ex.no:6 WRITE A PROGRAM TO SPLIT IMAGES FROM A
Date: VIDEO WITHOUT USING ANY PRIMITIVES
Aim:
To extract frames (images) from a video file manually, avoiding high-level
primitives, and save them as individual image files.
Algorithm:
1. Import necessary libraries (cv2, 0s).
2. Open the video file using v2. VideoCapture.
3. Retrieve the total frame count and frame rate.
4. Loop through the frames by manually setting the frame position using set()
and extracting each frame using read().
5. Save each frame using ev2.imwrite() into an output folder.
6. Close the video capture.
Source Code:
import ev2
import os
def split_video_to_images(video_path, output_folder):
# Create output folder if it doesn't exist
if not os.path.exists(output_folder):
os.makedirs(output_folder)
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("Error: Could not open video.")
return
total_frames = int(cap.get(ev2.CAP_PROP_FRAME_COUNT))
print(f'Total frames: {total_frames}")
for frame_num in range(total_frames):
cap.set(ev2.CAP_PROP_POS_FRAMES, frame_num) # Manually set
frame position
success, frame = cap.read() # Read frame
if not success:
print("Error reading frame {frame_num}")
continuefilename = os.path,join(output_folder, f"frame_{frame_num:04d} jpg")
cv2.imwrite(filename, frame)
cap.release()
print(""Frames successfully extracted.")
# Example usage
video_file = "sample_video.mp4" # Replace with your video file path
output_dir = "extracted_frames"
split_video_to_images(video_file, output_dir)
Output:
The program will output image files such as:
extracted_frames/frame_0000.jpg
extracted_frames/frame_0001.jpg
Each image corresponds to a frame from the video.
Result:
The program successfully extracts all frames from the video without using
high-level primitives like automatic looped reading. It manually navigates
frame-by-frame and saves them to disk, providing low-level control over
video processing.Ex.no:7 CREATE A PHOTO ALBUM OF A TRIP BY
Date: APPLYING APPROPRIATE IMAGE DIMENSIONS,
AND FORMAT
Aim:
To develop a photo album of a trip by resizing and formatting images
appropriately using Python, ensuring uniformity in display and compatibility
across digital platforms.
Algorithm:
1. Import Required Libraries: Use PIL (Python Imaging Library) for image
processing.
2. Set Target Dimensions and Format: Decide the final image size (e.g.,
800x600 pixels) and format (¢.g., JPEG or PNG).
3. Load Images: Access all images from a selected folder.
4, Resize and Format Images:
Open each image.
Resize it to the target dimensions.
Convert and save it in the desired format.
5. Create Album Layout:
Use a basic HTML template or Python script to generate a layout.
6. Display Album: View the album using a web browser or simple GUI.
Source Code:
from PIL import Image
import os
# Define input and output folders
input_folder = 'trip_images’
output_folder = 'album_images’
os.makedirs(output_folder, exist_ok=True)
# Desired image size and format
target_size = (800, 600)
target_format = 'JPEG'
# Process images
for filename in os.listdir(input_folder):
if filename.lower().endswith((',jpe’, ‘png’, jpeg’):
img_path = os,path,join(input_folder, filename)
img = Image.open(img_path)
img_resized = img resize(target_size)output_path = os.path,join(output_folder, os.path.splitext(filename)[0] +
ips)
img_resized.save(output_path, target_format)
print(""Images resized and formatted successfully."
Output:
All input images resized to 800x600 pixels.
Converted and saved in JPEG format inside the album_images folder.
These images are now consistent in size and format, suitable for a web or
desktop album,
Result:
The photo album was successfully created with resized and uniformly
formatted images, demonstrating how Python can be used for basic digital
image processing tasks. The output album is easy to view and share due to its
consistent format.Ex.no:8 IDENTIFYING THE POPULARITY OF CONTENT
Date: RETRIEVAL FROM A MEDIA SERVER
Aim:
To write a program that analyzes content request logs to identify the
popularity of each content item retrieved from a media server.
Algorithm:
1. Start
2. Create a log of content requests (¢.g., video names or content IDs).
3. Initialize a dictionary to store the count of requests for each content.
4. Loop through each request in the log:
If the content is already in the dictionary, increment its count.
Else, add it to the dictionary with count = 1.
5. Sort the dictionary based on the frequency of access in descending order.
6. Display the content along with their request counts.
7. Stop
Source Code:
# content_popularity.py.
def identify_popularity(request_logs):
popularity dict = {}
# Count the number of times each content is requested
for content in request_logs:
if content in popularity_dict:
popularity_dict[{content] += 1
else:
popularity _dict[content] = 1
# Sort contents based on number of requests (popularity)
sorted_popularity = sorted(popularity_dict.items(), key=lambda x: x[1],
reverse=True)
print("Content Popularity from Media Server:\n")
print(""{:<20} {}".format("Content", "Number of Requests"))
print("-" * 35)
for content, count in sorted_popularity:print(" {:<20} {}".format(content, count))
# Sample log data (you can replace this with actual logs)
media_requests = [
"movie1.mp4", "song].mp3", "moviel mp4", "movie2.mp4”, "song2.mp3",
"movie1.mp4", "song|.mp3", "movie3.mp4", "movie2.mp4", "song 1.mp3"
]
identify_popularity(media_requests)
Output:
Content Popularity from Media Server:
Content Number of Requests
moviel.mp4 3
song].mp3 3
movie2.mp4 2
song2.mp3 1
movie3.mp4 1
Result:
The Python program successfully identified the popularity of content
retrieved from the media server by counting the number of requests for each
content and displaying them in descending order of popularity.Ex.no:9 ENSURING DATA AVAILABILITY IN DISKS USING.
Date: STRIP-BASED METHOD
Aim:
To implement a striping method for data storage across multiple disks,
ensuring data availability and improved performance by distributing data in
strips across disks.
This method is commonly used in RAID (Redundant Array of Independent
Disks), especially RAID 0 (striping without parity) or RAID 5 (striping with
parity).
Algorithm:
1. Divide the data into blocks.
2. Distribute (strip) the blocks across multiple disks in a round-robin fashion.
3. Calculate parity for each set of blocks and store it on one of the disks in a
rotating pattern.
4, During failure, the missing data block can be reconstructed using parity
and the remaining data.
Source Code:
def calculate_parity(blocks):
# XOR all data blocks to get the parity
parity = 0
for block in blocks:
parity “= block
return parity
with_parity(data_blocks, num_disks):
for iin range(0, len(data_blocks), num_disks - 1):
stripe_data = data_blocks[i:i + (num_disks - 1)]
while len(stripe_data) <(num_disks - 1):
stripe_data.append(0) # Padding if not enough data
parity = calculate_parity(stripe_data)
stripe ‘stripe_data)
parity_position = (i // (num_disks - 1)) % num_disksstripe.insert(parity_position, parity)
stripes.append{(stripe)
return stripes
def display_stripes(stripes):
for i, stripe in enumerate(stripes):
print(f"Stripe {i+1}: {stripe}")
# Example usage
data_blocks = [10, 20, 30, 40, 50, 60, 70] # Simulated data
num_disks = 4
stripes = striping_with_parity(data_blocks, num_disks)
display_stripes(stripes)
Output:
Stripe 1: [60, 10, 20, 30]
Stripe 2: [40, 70, 50, 60]
Explanation:
In Stripe 1, the parity (60) is stored in disk 0.
In Stripe 2, the parity (40) is stored in disk 1, rotating parity disk.
Result:
The code demonstrates how striping with parity improves data availability
across multiple disks. Even if one disk fails, the lost data can be reconstructed
using parity and surviving data. This is foundational in RAID 5
implementations for fault tolerance in multimedia storage systems.Ex.no:10 PROGRAM FOR SHEDULING REQUESTS FOR
Date: DATA STREAMS
Aim:
To write a shell script that converts all image files in the current directory
to JPEG format.
Algorithm:
1. Start the script.
2. Loop through all image files in the current directory (e.
GIF).
3. Use the convert command (from ImageMagick) to convert each image to
JPEG format.
4, Save the output with a .jpg extension.
5. End the script.
PNG, BMP,
Source Code:
#\/bin/bash
# Check if ImageMagick is installed
if ! command -v convert &> /dev/null
then
echo "ImageMagick is not installed, Please install it using 'sudo apt install
imagemagick"
exit 1
fi
# Loop through image files (add more extensions if needed)
for img in *.png *.bmp *.gif *.tiff; do
if [-f"Simg" ]; then
filename="$ {img%.*}"
convert "Simg" "Sfilename,jpg"
echo "Simg converted to $filename.jpg"
fi
done
Output:
If the current directory contains the following files:
image |.pngphoto2.bmp
icon3.gif
After running the script:
imagel jpg
photo2.jpg
icon3.jpg
will be created in the same directory.
Result:
The shell script successfully converted all image files in the current directory
to JPEG format using ImageMagick.