def read_q_table(file_name): jpg = JpegImagePlugin.JpegImageFile(file_name) qtable = JpegImagePlugin.convert_dict_qtables(jpg.quantization) Y_qtable = qtable[0] Y_qtable_2d = np.zeros((8, 8)) qtable_idx = 0 for i in range(0, 8): for j in range(0, 8): Y_qtable_2d[i, j] = Y_qtable[qtable_idx] qtable_idx = qtable_idx + 1 return Y_qtable_2d
def test_pil_palette(): # img = cv2.imread('./img2.jpg') img = Image.open('./featuremaps.png') arr = np.array([ 7, 24, 86, 236, 255, 255, 255, 255, 57, 60, 112, 88, 255, 255, 255, 255, 146, 176, 199, 173, 255, 255, 255, 255, 247, 171, 199, 185, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 ]) arr2 = np.array([ 16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56, 14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56, 68, 109, 103, 77, 24, 35, 55, 64, 81, 104, 113, 92, 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99 ]) arr3 = np.array([ 1, 1, 255, 255, 255, 255, 255, 255, 1, 1, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 ]) arr = (arr).astype(np.int) qt = {0: arr3} from PIL import JpegImagePlugin qt = JpegImagePlugin.convert_dict_qtables(qt) file = io.BytesIO() img_qt = img.save(file, 'JPEG', qtables=qt) file.seek(0) img_data = file.read() img__ = cv2.imdecode(np.frombuffer(img_data, dtype=np.uint8), 1) cv2.imshow('tmp', img__) cv2.waitKey(0) cv2.destroyAllWindows() qt_n = img__.quantization print(0)
def test_convert_dict_qtables_deprecation(self): with pytest.warns(DeprecationWarning): qtable = {0: [1, 2, 3, 4]} qtable2 = JpegImagePlugin.convert_dict_qtables(qtable) assert qtable == qtable2