prichunkpng 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!D:\TestPythonProject\TEST\venv\Scripts\python.exe
  2. # prichunkpng
  3. # Chunk editing tool.
  4. """
  5. Make a new PNG by adding, delete, or replacing particular chunks.
  6. """
  7. import argparse
  8. import collections
  9. # https://docs.python.org/2.7/library/io.html
  10. import io
  11. import re
  12. import string
  13. import struct
  14. import sys
  15. import zlib
  16. # Local module.
  17. import png
  18. Chunk = collections.namedtuple("Chunk", "type content")
  19. class ArgumentError(Exception):
  20. """A user problem with the command arguments."""
  21. def process(out, args):
  22. """Process the PNG file args.input to the output, chunk by chunk.
  23. Chunks can be inserted, removed, replaced, or sometimes edited.
  24. Chunks are specified by their 4 byte Chunk Type;
  25. see https://www.w3.org/TR/2003/REC-PNG-20031110/#5Chunk-layout .
  26. The chunks in args.delete will be removed from the stream.
  27. The chunks in args.chunk will be inserted into the stream
  28. with their contents taken from the named files.
  29. Other options on the args object will create particular
  30. ancillary chunks.
  31. .gamma -> gAMA chunk
  32. .sigbit -> sBIT chunk
  33. Chunk types need not be official PNG chunks at all.
  34. Non-standard chunks can be created.
  35. """
  36. # Convert options to chunks in the args.chunk list
  37. if args.gamma:
  38. v = int(round(1e5 * args.gamma))
  39. bs = io.BytesIO(struct.pack(">I", v))
  40. args.chunk.insert(0, Chunk(b"gAMA", bs))
  41. if args.sigbit:
  42. v = struct.pack("%dB" % len(args.sigbit), *args.sigbit)
  43. bs = io.BytesIO(v)
  44. args.chunk.insert(0, Chunk(b"sBIT", bs))
  45. if args.iccprofile:
  46. # http://www.w3.org/TR/PNG/#11iCCP
  47. v = b"a color profile\x00\x00" + zlib.compress(args.iccprofile.read())
  48. bs = io.BytesIO(v)
  49. args.chunk.insert(0, Chunk(b"iCCP", bs))
  50. if args.transparent:
  51. # https://www.w3.org/TR/2003/REC-PNG-20031110/#11tRNS
  52. v = struct.pack(">%dH" % len(args.transparent), *args.transparent)
  53. bs = io.BytesIO(v)
  54. args.chunk.insert(0, Chunk(b"tRNS", bs))
  55. if args.background:
  56. # https://www.w3.org/TR/2003/REC-PNG-20031110/#11bKGD
  57. v = struct.pack(">%dH" % len(args.background), *args.background)
  58. bs = io.BytesIO(v)
  59. args.chunk.insert(0, Chunk(b"bKGD", bs))
  60. if args.physical:
  61. # https://www.w3.org/TR/PNG/#11pHYs
  62. numbers = re.findall(r"(\d+\.?\d*)", args.physical)
  63. if len(numbers) not in {1, 2}:
  64. raise ArgumentError("One or two numbers are required for --physical")
  65. xppu = float(numbers[0])
  66. if len(numbers) == 1:
  67. yppu = xppu
  68. else:
  69. yppu = float(numbers[1])
  70. unit_spec = 0
  71. if args.physical.endswith("dpi"):
  72. # Convert from DPI to Pixels Per Metre
  73. # 1 inch is 0.0254 metres
  74. l = 0.0254
  75. xppu /= l
  76. yppu /= l
  77. unit_spec = 1
  78. elif args.physical.endswith("ppm"):
  79. unit_spec = 1
  80. v = struct.pack("!LLB", round(xppu), round(yppu), unit_spec)
  81. bs = io.BytesIO(v)
  82. args.chunk.insert(0, Chunk(b"pHYs", bs))
  83. # Create:
  84. # - a set of chunks to delete
  85. # - a dict of chunks to replace
  86. # - a list of chunk to add
  87. delete = set(args.delete)
  88. # The set of chunks to replace are those where the specification says
  89. # that there should be at most one of them.
  90. replacing = set([b"gAMA", b"pHYs", b"sBIT", b"PLTE", b"tRNS", b"sPLT", b"IHDR"])
  91. replace = dict()
  92. add = []
  93. for chunk in args.chunk:
  94. if chunk.type in replacing:
  95. replace[chunk.type] = chunk
  96. else:
  97. add.append(chunk)
  98. input = png.Reader(file=args.input)
  99. return png.write_chunks(out, edit_chunks(input.chunks(), delete, replace, add))
  100. def edit_chunks(chunks, delete, replace, add):
  101. """
  102. Iterate over chunks, yielding edited chunks.
  103. Subtle: the new chunks have to have their contents .read().
  104. """
  105. for type, v in chunks:
  106. if type in delete:
  107. continue
  108. if type in replace:
  109. yield type, replace[type].content.read()
  110. del replace[type]
  111. continue
  112. if b"IDAT" <= type <= b"IDAT" and replace:
  113. # If there are any chunks on the replace list by
  114. # the time we reach IDAT, add then all now.
  115. # put them all on the add list.
  116. for chunk in replace.values():
  117. yield chunk.type, chunk.content.read()
  118. replace = dict()
  119. if b"IDAT" <= type <= b"IDAT" and add:
  120. # We reached IDAT; add all remaining chunks now.
  121. for chunk in add:
  122. yield chunk.type, chunk.content.read()
  123. add = []
  124. yield type, v
  125. def chunk_name(s):
  126. """
  127. Type check a chunk name option value.
  128. """
  129. # See https://www.w3.org/TR/2003/REC-PNG-20031110/#table51
  130. valid = len(s) == 4 and set(s) <= set(string.ascii_letters)
  131. if not valid:
  132. raise ValueError("Chunk name must be 4 ASCII letters")
  133. return s.encode("ascii")
  134. def comma_list(s):
  135. """
  136. Convert s, a command separated list of whole numbers,
  137. into a sequence of int.
  138. """
  139. return tuple(int(v) for v in s.split(","))
  140. def hex_color(s):
  141. """
  142. Type check and convert a hex color.
  143. """
  144. if s.startswith("#"):
  145. s = s[1:]
  146. valid = len(s) in [1, 2, 3, 4, 6, 12] and set(s) <= set(string.hexdigits)
  147. if not valid:
  148. raise ValueError("colour must be 1,2,3,4,6, or 12 hex-digits")
  149. # For the 4-bit RGB, expand to 8-bit, by repeating digits.
  150. if len(s) == 3:
  151. s = "".join(c + c for c in s)
  152. if len(s) in [1, 2, 4]:
  153. # Single grey value.
  154. return (int(s, 16),)
  155. if len(s) in [6, 12]:
  156. w = len(s) // 3
  157. return tuple(int(s[i : i + w], 16) for i in range(0, len(s), w))
  158. def main(argv=None):
  159. if argv is None:
  160. argv = sys.argv
  161. argv = argv[1:]
  162. parser = argparse.ArgumentParser()
  163. parser.add_argument("--gamma", type=float, help="Gamma value for gAMA chunk")
  164. parser.add_argument(
  165. "--physical",
  166. type=str,
  167. metavar="x[,y][dpi|ppm]",
  168. help="specify intended pixel size or aspect ratio",
  169. )
  170. parser.add_argument(
  171. "--sigbit",
  172. type=comma_list,
  173. metavar="D[,D[,D[,D]]]",
  174. help="Number of significant bits in each channel",
  175. )
  176. parser.add_argument(
  177. "--iccprofile",
  178. metavar="file.iccp",
  179. type=argparse.FileType("rb"),
  180. help="add an ICC Profile from a file",
  181. )
  182. parser.add_argument(
  183. "--transparent",
  184. type=hex_color,
  185. metavar="#RRGGBB",
  186. help="Specify the colour that is transparent (tRNS chunk)",
  187. )
  188. parser.add_argument(
  189. "--background",
  190. type=hex_color,
  191. metavar="#RRGGBB",
  192. help="background colour for bKGD chunk",
  193. )
  194. parser.add_argument(
  195. "--delete",
  196. action="append",
  197. default=[],
  198. type=chunk_name,
  199. help="delete the chunk",
  200. )
  201. parser.add_argument(
  202. "--chunk",
  203. action="append",
  204. nargs=2,
  205. default=[],
  206. type=str,
  207. help="insert chunk, taking contents from file",
  208. )
  209. parser.add_argument(
  210. "input", nargs="?", default="-", type=png.cli_open, metavar="PNG"
  211. )
  212. args = parser.parse_args(argv)
  213. # Reprocess the chunk arguments, converting each pair into a Chunk.
  214. args.chunk = [
  215. Chunk(chunk_name(type), open(path, "rb")) for type, path in args.chunk
  216. ]
  217. return process(png.binary_stdout(), args)
  218. if __name__ == "__main__":
  219. main()