flip.py 1.11 KB
import mjpeg
import sys
import struct
from PIL import _binary
import numpy as np
from array import array

i8 = _binary.i8
o8 = _binary.o8
i16 = _binary.i16be
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)


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)

            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")