0% found this document useful (0 votes)
35 views10 pages

DCTimagecompress Copy1

The document discusses image compression using discrete cosine transform (DCT). It imports necessary libraries, defines functions to perform DCT compression on images, calculate image sizes, and get system specifications. It then loads test images, compresses them using DCT with 1000 coefficients, displays and compares the original and reconstructed images sizes. It also prints the CPU usage, GPU usage if available, memory usage, and total runtime.

Uploaded by

onlineclass net
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views10 pages

DCTimagecompress Copy1

The document discusses image compression using discrete cosine transform (DCT). It imports necessary libraries, defines functions to perform DCT compression on images, calculate image sizes, and get system specifications. It then loads test images, compresses them using DCT with 1000 coefficients, displays and compares the original and reconstructed images sizes. It also prints the CPU usage, GPU usage if available, memory usage, and total runtime.

Uploaded by

onlineclass net
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

6/19/23, 7:34 PM DCTimagecompress-Copy1

In [1]: import time


tik = time.time()

In [2]: import platform


import psutil
import subprocess
import sys

def get_ubuntu_version():
try:
result = subprocess.run(['lsb_release', '-r'], capture_output=True, text=Tr
if result.returncode == 0:
output = result.stdout.strip()
version = output.split('\t')[1]
return version
else:
return "Unknown"
except FileNotFoundError:
return "Unknown"

def get_system_specifications():
system = platform.uname()
os_version = platform.version()
processor = system.processor
memory = psutil.virtual_memory()
disk_usage = psutil.disk_usage('/')
ubuntu_ver = get_ubuntu_version()
python_ver = sys.version.split()[0]

print(f"Operating System: {system.system} {os_version}")


print(f"Ubuntu Version: {ubuntu_ver}")
print(f"Python Version: {python_ver}")
print(f"Processor: {processor}")
print(f"Memory - Total: {round(memory.total / (1024**3), 2)} GB")
print(f"Memory - Available: {round(memory.available / (1024**3), 2)} GB")
print(f"Disk Usage - Total: {round(disk_usage.total / (1024**3), 2)} GB")
print(f"Disk Usage - Used: {round(disk_usage.used / (1024**3), 2)} GB")
print(f"Disk Usage - Free: {round(disk_usage.free / (1024**3), 2)} GB")
print(sys.getrecursionlimit())
get_system_specifications()

Operating System: Windows 10.0.17763


Ubuntu Version: Unknown
Python Version: 3.10.9
Processor: AMD64 Family 23 Model 49 Stepping 0, AuthenticAMD
Memory - Total: 27.95 GB
Memory - Available: 23.01 GB
Disk Usage - Total: 126.45 GB
Disk Usage - Used: 19.23 GB
Disk Usage - Free: 107.22 GB
3000

In [3]: #import library


import os
os.sys.path
import matplotlib.pyplot as plt
import cv2
import numpy as np
import pywt
from keras.preprocessing.image import ImageDataGenerator
import psutil
import GPUtil

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/DCTimagecompress-Copy1.ipynb?download=false 1/10
6/19/23, 7:34 PM DCTimagecompress-Copy1

def calculate_image_size(image):
# Menghitung ukuran dalam bytes dari gambar
_, buffer = cv2.imencode('.jpg', image)
size_bytes = len(buffer.tobytes())
return size_bytes

In [4]: %%time

def dct_compression(image, num_coeffs):


# Konversi gambar ke skala keabuan
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Ubah gambar menjadi array float32


float_image = np.float32(gray_image)

# Lakukan DCT
dct_image = cv2.dct(float_image)

# Ambil koefisien teratas


sorted_indices = np.argsort(np.abs(dct_image.ravel()))[::-1][:num_coeffs]

# Set koefisien yang tidak terpilih menjadi nol


dct_image.ravel()[sorted_indices] = 0

# Lakukan rekonstruksi DCT


reconstructed_image = cv2.idct(dct_image)

# Kembalikan gambar yang direkonstruksi


return reconstructed_image

def get_cpu_usage():
cpu_percent = psutil.cpu_percent(interval=1)
return cpu_percent

def get_gpu_usage():
gpus = GPUtil.getGPUs()
gpu_percent = []
for gpu in gpus:
gpu_percent.append(gpu.load * 100)
return gpu_percent

def get_memory_usage():
memory = psutil.virtual_memory()
total_memory = memory.total
used_memory = memory.used
memory_percent = (used_memory / total_memory) * 100
return memory_percent
def get_file_size(file_path):
if os.path.isfile(file_path):
file_size = os.path.getsize(file_path)
return file_size
else:
return -1 # -1 menandakan file tidak ditemukan

# Daftar nama file gambar


image_files = ['jyothi-kumar.jpg','adrien-bruneau.jpg','bryan-garces.jpg','david-em

# Tentukan jumlah koefisien DCT yang ingin digunakan


num_coeffs = 1000

# Proses kompresi dan rekonstruksi untuk setiap gambar


for file in image_files:

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/DCTimagecompress-Copy1.ipynb?download=false 2/10
6/19/23, 7:34 PM DCTimagecompress-Copy1

# Load gambar
image_path = 'image_compress/image_compress/' + file
image = cv2.imread(image_path)
new_width=500
new_height=500
image=cv2.resize(image,(new_width,new_height))
# Lakukan kompresi DCT
compressed_image = dct_compression(image, num_coeffs)

# Konversi gambar yang direkonstruksi menjadi format uint8


reconstructed_image = np.uint8(compressed_image)

# Tampilkan gambar asli dan gambar yang direkonstruksi


print('nama file :',file)
cv2.imshow('Original Image', image)
plt.imshow(image)
plt.show()
size = calculate_image_size(image)
print(f"Ukuran gambar original: {size} bytes")
print(f'original image: {image.shape}')
cv2.imshow('Reconstructed Image', reconstructed_image)
plt.imshow(reconstructed_image)
plt.show()
size = calculate_image_size(reconstructed_image)
print(f"Ukuran gambar compress: {size} bytes")
print(f'compress image: {reconstructed_image.shape}')
tok = time.time()
print(f'time image {file} by notebook: {round(tok-tik, 2)} sec')
print("\n")
# cv2.waitKey(0)
# cv2.destroyAllWindows()

if __name__ == '__main__':
cpu_usage = get_cpu_usage()
gpu_usage = get_gpu_usage()
memory_usage = get_memory_usage()

print(f'CPU Usage: {cpu_usage}%')


if len(gpu_usage) > 0:
for i, gpu in enumerate(gpu_usage):
print(f'GPU {i} Usage: {gpu}%')
else:
print('No GPUs found.')
print(f'Memory Usage: {memory_usage}%')

tok = time.time()
print(f'total time DWT by notebook: {round(tok-tik, 2)} sec')

nama file : jyothi-kumar.jpg

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/DCTimagecompress-Copy1.ipynb?download=false 3/10
6/19/23, 7:34 PM DCTimagecompress-Copy1

Ukuran gambar original: 71053 bytes


original image: (500, 500, 3)

Ukuran gambar compress: 245890 bytes


compress image: (500, 500)
time image jyothi-kumar.jpg by notebook: 4.59 sec

nama file : adrien-bruneau.jpg

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/DCTimagecompress-Copy1.ipynb?download=false 4/10
6/19/23, 7:34 PM DCTimagecompress-Copy1

Ukuran gambar original: 92213 bytes


original image: (500, 500, 3)

Ukuran gambar compress: 240025 bytes


compress image: (500, 500)
time image adrien-bruneau.jpg by notebook: 5.07 sec

nama file : bryan-garces.jpg

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/DCTimagecompress-Copy1.ipynb?download=false 5/10
6/19/23, 7:34 PM DCTimagecompress-Copy1

Ukuran gambar original: 101163 bytes


original image: (500, 500, 3)

Ukuran gambar compress: 292206 bytes


compress image: (500, 500)
time image bryan-garces.jpg by notebook: 5.54 sec

nama file : david-emrich.jpg

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/DCTimagecompress-Copy1.ipynb?download=false 6/10
6/19/23, 7:34 PM DCTimagecompress-Copy1

Ukuran gambar original: 151922 bytes


original image: (500, 500, 3)

Ukuran gambar compress: 262751 bytes


compress image: (500, 500)
time image david-emrich.jpg by notebook: 6.0 sec

nama file : emmanuelle-magnenat.jpg

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/DCTimagecompress-Copy1.ipynb?download=false 7/10
6/19/23, 7:34 PM DCTimagecompress-Copy1

Ukuran gambar original: 97210 bytes


original image: (500, 500, 3)

Ukuran gambar compress: 235051 bytes


compress image: (500, 500)
time image emmanuelle-magnenat.jpg by notebook: 6.46 sec

nama file : keiron-crasktellanos.jpg

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/DCTimagecompress-Copy1.ipynb?download=false 8/10
6/19/23, 7:34 PM DCTimagecompress-Copy1

Ukuran gambar original: 78885 bytes


original image: (500, 500, 3)

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/DCTimagecompress-Copy1.ipynb?download=false 9/10
6/19/23, 7:34 PM DCTimagecompress-Copy1

Ukuran gambar compress: 231909 bytes


compress image: (500, 500)
time image keiron-crasktellanos.jpg by notebook: 6.94 sec

CPU Usage: 1.4%


No GPUs found.
Memory Usage: 18.73266242978817%
total time DWT by notebook: 7.94 sec
CPU times: total: 3.02 s
Wall time: 3.87 s

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/DCTimagecompress-Copy1.ipynb?download=false 10/10

You might also like