Created
April 4, 2021 13:07
-
-
Save ryank231231/2cbbc1adc7ba930159dd0eca8d67ace0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from apng import PNG, make_chunk, Chunk | |
import struct, sys, argparse | |
parser = argparse.ArgumentParser(description='PNG offset update') | |
parser.add_argument('infile', help='input file') | |
parser.add_argument('x', type=int, help='x') | |
parser.add_argument('y', type=int, help='y') | |
parser.add_argument('-o', '--out', help='output file. default is input file') | |
parser.add_argument('-t', '--type', help='offset type, caNv or oFFs', choices=['oFFs', 'caNv'], default='oFFs') | |
args = parser.parse_args() | |
infile = args.infile | |
x = args.x | |
y = args.y | |
offset_type = args.type | |
if args.out != None: | |
outfile = args.out | |
else: | |
outfile = infile | |
a = -1 | |
d = -1 | |
im = PNG.open(infile) | |
for i in range(len(im.chunks)): | |
if a < 0 and im.chunks[i].type == 'IDAT': | |
a = i | |
else: | |
if d < 0: | |
if im.chunks[i].type == offset_type: | |
d = i | |
if d >= 0: | |
if d > a: | |
im.chunks[d:d + 1] = [] | |
d = -1 | |
if a >= 0: | |
if offset_type == 'oFFs': | |
im.chunks[a:a] = [ | |
Chunk('oFFs', make_chunk('oFFs', struct.pack('!iib', x, y, 0)))] | |
else: | |
if offset_type == 'caNv': | |
im.chunks[a:a] = [ | |
Chunk('caNv', make_chunk('caNv', struct.pack('!IIii', im.width, im.height, x, y)))] | |
if d >= 0: | |
im.chunks[d:d + 1] = [] | |
im.save(outfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment