-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpngFile.py
More file actions
90 lines (68 loc) · 2.42 KB
/
pngFile.py
File metadata and controls
90 lines (68 loc) · 2.42 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import logging
import numpy as np
import cv2
import matplotlib.pyplot as plt;
from pngChunk import PngChunk
class PngFile(object):
HEADER = b'\x89PNG\r\n\x1a\n'
def __init__(self, file_path) -> None:
self.path_to_file = file_path
self.file = open(file_path, "br")
self._chunks = []
self.__check_header()
self.__load_chunks()
# self._decode_picture()
# self.get_fft()
def _check_color(self):
res = next((chunk for chunk in self.chunks if chunk.type == "IHDR"), None)
if res is not None:
return res.color_type
else:
raise RuntimeError("IHDR chunk not found")
def _decode_picture(self, data_dict: dict):
color = self._check_color()
for chunk in self.chunks:
if chunk.type == "IDAT":
chunk.decode(color, dict)
if chunk.type == "bKGD":
chunk.decode(color, dict)
def __check_header(self):
header = self.file.read(8)
logging.debug("Header: %s",header)
if header != self.HEADER:
raise RuntimeError(f"Invalid header {header}")
return header
def __load_chunks(self):
self._chunks.append(PngChunk(self.file))
while self._chunks[-1].type != "IEND":
self._chunks.append(PngChunk(self.file))
def isgray(self):
img = cv2.imread(self.path_to_file)
if len(img.shape) < 3: return True
if img.shape[2] == 1: return True
b,g,r = img[:,:,0], img[:,:,1], img[:,:,2]
if (b==g).all() and (b==r).all(): return True
return False
def get_fft(self):
fft_log = True
if self.isgray():
fft_log = False
# image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.imread(self.path_to_file, 0)
fft = np.fft.fft2(image)
fft_shifted = np.fft.fftshift(fft)
if fft_log:
fft_mag = np.ma.log10(abs(fft_shifted.transpose()))
else:
fft_mag = abs(fft_shifted.transpose())
fft_phase = np.angle(fft_shifted.transpose())
return (fft_mag, fft_phase)
def get_chunk(self, name: str):
res = next((chunk for chunk in self._chunks if chunk.type == name), None)
return res
@property
def chunks(self):
return self._chunks
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
test = PngFile("png/histo15.png")