forked from freddyb/appendTextToPNG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveTextPNG.py
More file actions
executable file
·65 lines (51 loc) · 1.37 KB
/
removeTextPNG.py
File metadata and controls
executable file
·65 lines (51 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python2
"""
Length Chunk type Chunk data CRC
4 bytes 4 bytes Length bytes 4 bytes
89 50 4E 47 0D 0A 1A 0A
"""
import struct
import zlib
import posixpath
from sys import argv,stdout
if len(argv) == 2:
if posixpath.exists(argv[1]):
key = None
pname = argv[1]
else:
print "file does not exist"
raise SystemExit
elif len(argv) == 3:
if posixpath.exists(argv[2]):
key = argv[1]
pname = argv[2]
else:
print "file does not exist"
raise SystemExit
else: # usage
print "Usage: %s <png>" % argv[0]
print "\tRemove all text-chunks (tEXt) in the <png> file\n"
# print "Usage: %s <key> <png>" % argv[0]
# print "\tRemove text-chunks (tEXt) with <key> in the <png> file\n"
raise SystemExit
f = open(pname, 'rb')
img_data = f.read()
#size = f.tell()
f.seek(0)
header = f.read(8)
try:
assert header == "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"
except:
print pname, "is not a valid PNG"
raise SystemExit
f.close()
text_index = img_data.find("tEXt")
img_data_post = img_data
while text_index != -1:
text_chunk = text_index-4
length = struct.unpack(">I",img_data_post[text_chunk:text_index])[0]
img_data_pre = img_data_post[:text_chunk]
stdout.write(img_data_pre)
img_data_post = img_data_post[text_index+length+8:]
text_index = img_data_post.find("tEXt")
stdout.write(img_data_post)