diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..a1459cf --- /dev/null +++ b/__init__.py @@ -0,0 +1 @@ +__author__ = 'chunk' diff --git a/common.py b/common.py new file mode 100644 index 0000000..13bbc4b --- /dev/null +++ b/common.py @@ -0,0 +1,77 @@ +""" +Common utils. + +@author: chunk +chunkplus@gmail.com +2014 Dec +""" +__author__ = 'hadoop' + +import os, sys +import time + + +class Timer(): + def __init__(self): + self.__newtime = time.time() + self.__oldtime = self.__newtime + + def mark(self): + self.__oldtime = self.__newtime + self.__newtime = time.time() + return self.__newtime - self.__oldtime + + def report(self): + print "%-24s%fs" % ("time elapsed:", self.mark()) + + +def ttimer(): + newtime = time.time() + while True: + oldtime = newtime + newtime = time.time() + yield newtime - oldtime + + +def ctimer(): + newtime = time.clock() + while True: + oldtime = newtime + newtime = time.clock() + yield newtime - oldtime + + +def ski2cv(img): + if img.ndim >= 3 and img.shape[2] >= 3: + img[:, :, [0, 2]] = img[:, :, [2, 0]] + return img + + +if __name__ == '__main__': + timer = Timer() + + timer.mark() + timer.report() + + timer.mark() + time.sleep(1) + # for i in range(1000000): + # print i + timer.report() + + + + + + + + + + + + + + + + + diff --git a/common.pyc b/common.pyc new file mode 100644 index 0000000..3f11497 Binary files /dev/null and b/common.pyc differ diff --git a/flip.py b/flip.py index 1917bab..fac432b 100644 --- a/flip.py +++ b/flip.py @@ -3,7 +3,7 @@ import sys import struct from PIL import _binary import numpy as np - +from array import array i8 = _binary.i8 o8 = _binary.o8 @@ -13,21 +13,27 @@ i32 = _binary.i32be a = mjpeg.Jpeg("res/test2.jpg") # print a.component_count def flipblock(block): - tmp = str(block) - for i in range(0, len(block), 4): - short, = struct.unpack_from("h", tmp, i+2) - struct.pack_into("h", block, i+2, -short) + tmp = str(block) + for i in range(0, len(block), 4): + short, = struct.unpack_from("h", tmp, i + 2) + struct.pack_into("h", block, i + 2, -short) + for comp in range(a.component_count): - xmax, ymax = a.getcomponentdimensions(comp) - for y in range(ymax): - for x in range((xmax+1)/2): - block = a.getblock(x,y,comp) - block_to_show = np.frombuffer(block, dtype=np.int16, count=-1, offset=0).reshape(8,8) - print block_to_show - block2 = a.getblock(xmax-1 - x, y, comp) - flipblock(block) - flipblock(block2) - a.setblock(xmax-1-x, y, comp, block) - a.setblock(x,y,comp,block2) + xmax, ymax = a.getcomponentdimensions(comp) + for y in range(ymax): + for x in range((xmax + 1) / 2): + block = a.getblock(x, y, comp) + # block_to_show = np.frombuffer(block, dtype=np.int16, count=-1, offset=0).reshape(8,8) + # print block_to_show + block2 = a.getblock(xmax - 1 - x, y, comp) + + block_to_show = np.frombuffer(block, dtype=np.int16, count=-1, offset=0).reshape(8, 8) + block = bytearray(block_to_show) + + flipblock(block) + flipblock(block2) + a.setblock(xmax - 1 - x, y, comp, block) + a.setblock(x, y, comp, block2) +a.write("test2ooout.jpg") \ No newline at end of file diff --git a/jpegObj/__init__.py b/jpegObj/__init__.py index 8e5ecab..1571149 100644 --- a/jpegObj/__init__.py +++ b/jpegObj/__init__.py @@ -4,7 +4,7 @@ print "[pysteg.jpeg] $Id: __init__.py 2204 2011-04-05 11:43:38Z georg $" from mjsteg import Jsteg -__all__ = ["Jpeg"] +__all__ = ['Jpeg'] # We need standard components from :mod:`numpy`, and some auxiliary # functions from submodules. @@ -194,6 +194,12 @@ class Jpeg(Jsteg): cID = self.getCompID(channel) self.coef_arrays[cID] = matrix + blocks = self.getCoefBlocks(channel) + xmax, ymax = self.Jgetcompdim(cID) + for y in range(ymax): + for x in range(xmax): + block = blocks[y, x] + self.Jsetblock(x, y, cID, bytearray(block.astype(np.int16))) def getCoefBlocks(self, channel="Y"): """ @@ -222,6 +228,8 @@ class Jpeg(Jsteg): v, h = loc[0] * 8, loc[1] * 8 self.coef_arrays[cID][v:v + 8, h:h + 8] = block + self.Jsetblock(loc[1], loc[0], cID, bytearray(block.astype(np.int16))) + # Decompression # ------------- @@ -311,3 +319,5 @@ class Jpeg(Jsteg): S = np.round(ibdct(D) + 128) return S.astype(np.uint8) + + diff --git a/jpegObj/__init__.pyc b/jpegObj/__init__.pyc index b39a44c..f1aac38 100644 Binary files a/jpegObj/__init__.pyc and b/jpegObj/__init__.pyc differ diff --git a/res/test4.jpg b/res/test4.jpg new file mode 100644 index 0000000..98bafec Binary files /dev/null and b/res/test4.jpg differ diff --git a/tests.py b/tests.py index 5a79bbd..80cf70b 100644 --- a/tests.py +++ b/tests.py @@ -4,7 +4,7 @@ def quitwith(s): raise Exception print("importing jpeg") -import jpeg +import mjpeg as jpeg print("loading a jpeg with a number") try: a = jpeg.Jpeg(0) diff --git a/testyaj.py b/testyaj.py new file mode 100644 index 0000000..eaaa2ac --- /dev/null +++ b/testyaj.py @@ -0,0 +1,92 @@ +__author__ = 'chunk' + +import numpy as np +import mjsteg +import jpegObj +from common import * + +timer = Timer() + +sample = [[7, 12, 14, -12, 1, 0, -1, 0], + [6, 5, -10, 0, 6, 0, 0, 0], + [0, 6, -5, 4, 0, -1, 0, 0], + [0, -3, 0, 1, -1, 0, 0, 0], + [-3, 5, 0, 0, 0, 0, 0, 0], + [2, -1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0]] + + +def diffblock(c1, c2): + diff = False + if np.array_equal(c1, c2): + print("blocks match") + else: + print("blocks not match") + diff = True + + return diff + + +def diffblocks(a, b): + diff = False + for comp in range(a.image_components): + xmax, ymax = a.Jgetcompdim(comp) + for y in range(ymax): + for x in range(xmax): + if a.Jgetblock(x, y, comp) != b.Jgetblock(x, y, comp): + print("blocks({},{}) in component {} not match".format(y, x, comp)) + diff = True + return diff + + +def test_setblocks(): + """ + wholewise + """ + imb = jpegObj.Jpeg("res/test4.jpg") + block = imb.getCoefBlock(channel='Y', loc=(-1, 2)) + print block + + imb.setCoefMatrix(np.array([[0] * 800 for i in range(600)]), channel='Y') + + block = imb.getCoefBlock(channel='Y', loc=(-1, 2)) + print block + + imb.Jwrite("res/test4.jpg") + + ima = jpegObj.Jpeg("res/test3.jpg") + imb = jpegObj.Jpeg("res/test4.jpg") + diffblocks(ima, imb) + + +def test_setblock(): + """ + blockwise + """ + imb = jpegObj.Jpeg("res/test4.jpg") + block = imb.getCoefBlock(channel='Y', loc=(0, 1)) + print block + + # print b.setCoefBlock(np.array([[27] + [0] * 7] + [[0] * 8 for i in range(7)]), loc=(0, 1)) + imb.setCoefBlock(np.array(sample), loc=(0, 1)) + blocks1 = imb.getCoefBlock(channel='Y', loc=(0, 1)) + print blocks1 + blocks2 = imb.Jgetblock(1, 0, 0) + block_to_show = np.frombuffer(blocks2, dtype=np.int16, count=-1, offset=0).reshape(8, 8) + print block_to_show + diffblock(blocks1, block_to_show) + + +if __name__ == '__main__': + timer.mark() + test_setblock() + timer.report() + + timer.mark() + test_setblocks() + timer.report() + + + + diff --git a/yaj.py b/yaj.py deleted file mode 100644 index 5a10101..0000000 --- a/yaj.py +++ /dev/null @@ -1,28 +0,0 @@ -__author__ = 'chunk' - -import numpy as np -import jpegObj - -b = jpegObj.Jpeg("res/test2.jpg") -# 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