-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageCompression.py
More file actions
54 lines (46 loc) · 1.49 KB
/
imageCompression.py
File metadata and controls
54 lines (46 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
###############################################
## Python implementation for Image Compression
###############################################
'''
You are compressing a 255 bit map to 3 bit map
2^8=256 -> 2^3=8
2^5=32 times space saving
MApping of each bit value in image will be :
0-31 will be mapped as 0
32-63 will be mapped as 1
64-95 will be mapped as 2
96-127 will be mapped as 3
128-159 will be mapped as 4
160-191 will be mapped as 5
192-223 will be mapped as 6
224-255 will be mapped as 7
'''
import numpy as np
from PIL import image
im=Image.open("kanav.jpg")
pixelMap=im.load()
#Make a new copy for kanav.jpg carrying your changes
img=Image.new(im.mode,im.size)
pixelNew=img.load()
for i in range(img.size[0]):
for j in range(img.size[1]):
if (pixelMap[i,j]>=0 and pixelMap[i,j]<=31):
pixelNew[i,j]=0
elif (pixelMap[i,j]>=32 and pixelMap[i,j]<=63):
pixelNew[i,j]=1
elif (pixelMap[i,j]>=64 and pixelMap[i,j]<=95):
pixelNew[i,j]=2
elif (pixelMap[i,j]>=96 and pixelMap[i,j]<=127):
pixelNew[i,j]=3
elif (pixelMap[i,j]>=128 and pixelMap[i,j]<=159):
pixelNew[i,j]=4
elif (pixelMap[i,j]>=160 and pixelMap[i,j]<=191):
pixelNew[i,j]=5
elif (pixelMap[i,j]>=192 and pixelMap[i,j]<=223):
pixelNew[i,j]=6
elif (pixelMap[i,j]>=224 and pixelMap[i,j]<=255):
pixelNew[i,j]=7
img.save("kanav_modified.jpg)
#See the array value corresponding to kanav_modified.jpg
j=np.asanyarray(Image.open("kanav_modified.jpg"))
print(j)