diff --git a/jpegObj/__init__.py b/jpegObj/__init__.py index fda3eef..8e5ecab 100644 --- a/jpegObj/__init__.py +++ b/jpegObj/__init__.py @@ -53,7 +53,7 @@ class Jpeg(Jsteg): Jsteg.__init__(self, file, **kw) self.verbosity = verbosity if verbosity > 0: - print "[jpeg.__init__] Image size %ix%i" % (self.coef_arrays[0].shape) + print "[Jpeg.__init__] Image size %ix%i" % (self.coef_arrays[0].shape) if key != None: self.key = key elif rndkey: @@ -187,13 +187,40 @@ class Jpeg(Jsteg): cID = self.getCompID(channel) return self.coef_arrays[cID] + def setCoefMatrix(self, matrix, channel="Y"): + v, h = self.getCoefMatrix(channel).shape + assert matrix.shape == (v, h), "matrix is expected of size (%d,%d)" % (v, h) + + cID = self.getCompID(channel) + self.coef_arrays[cID] = matrix + + def getCoefBlocks(self, channel="Y"): """ This method returns the coefficient matrix for the given colour channel (as a 4-D tensor: (v,h,row,col)). """ + if channel == "All": + return [ + np.array([np.split(arr, arr.shape[1] / 8, axis=1) for arr in np.split(compMat, compMat.shape[0] / 8)]) + for compMat in self.coef_arrays] + compMat = self.getCoefMatrix(channel="Y") - return np.array([np.split(arr,arr.shape[1]/8, axis=1) for arr in np.split(compMat,compMat.shape[0]/8)]) + return np.array([np.split(arr, arr.shape[1] / 8, axis=1) for arr in np.split(compMat, compMat.shape[0] / 8)]) + + def getCoefBlock(self, channel="Y", loc=(0, 0)): + """ + This method returns the coefficient matrix for the given + colour channel (as a 4-D tensor: (v,h,row,col)). + """ + return self.getCoefBlocks(channel)[loc] + + + def setCoefBlock(self, block, channel="Y", loc=(0, 0)): + assert block.shape == (8, 8), "block is expected of size (8,8)" + cID = self.getCompID(channel) + v, h = loc[0] * 8, loc[1] * 8 + self.coef_arrays[cID][v:v + 8, h:h + 8] = block # Decompression # ------------- diff --git a/jpegObj/__init__.pyc b/jpegObj/__init__.pyc index b8044a9..b39a44c 100644 Binary files a/jpegObj/__init__.pyc and b/jpegObj/__init__.pyc differ diff --git a/yaj.py b/yaj.py index 76b487a..5a10101 100644 --- a/yaj.py +++ b/yaj.py @@ -1,41 +1,28 @@ __author__ = 'chunk' -# import mjsteg import numpy as np - -# a = mjsteg.Jsteg("res/test2.jpg") -# compY,compCr,compCb = a.coef_arrays -# -# res = np.array([np.split(arr,arr.shape[1]/8, axis=1) for arr in np.split(compY,compY.shape[0]/8)]) -# -# print res.shape -# -# print res[0,0:20] - import jpegObj b = jpegObj.Jpeg("res/test2.jpg") -c = b.getCoefBlocks(channel='Y') - -print c -print c.shape - - - - - - - - - - - +# c = b.getCoefBlocks(channel='Y') +# c1,c2,c3 = b.getCoefBlocks(channel='All') +# print c2 +print b.setCoefBlock(np.array([[0] * 8 for i in range(8)])) +c = b.getCoefBlock(channel='Y', loc=(0, 0)) +print c +c = b.getCoefBlock(channel='Y', loc=(-1, 1)) +print c +b.setCoefMatrix(np.array([[0] * 800 for i in range(600)]),channel='Y') +c = b.getCoefBlock(channel='Y', loc=(-1, 1)) +print c +c = b.getCoefBlock(channel='Y', loc=(0, 0)) +print c -- libgit2 0.21.2