摘要:本文将带你了解IOS开发之教你还原iOS图片,希望本文对大家学IOS有所帮助。
python脚本恢复iOS程序中的png图片。
使用方法:
1、把ipin.py放到要恢复的.png图片一个目录里 2、打开终端,cd到此目录。 3、输入 python ipin.py 4、根据提示信息输入 Y,回车。这样就能把图片还原到可以查看了。 [python] view plain copy 1. #--- 2. # iPIN - iPhone PNG Images Normalizer v1.0 3. # Copyright (C) 2007 4. # 5. # Author: 6. # Axel E. Brzostowski 7. # //www.axelbrz.com.ar/ 8. # axelbrz@gmail.com 9. # 10. # References: 11. # //iphone.fiveforty.net/wiki/index.php/PNG_Images 12. # //www.libpng.org/pub/png/spec/1.2/PNG-Contents.html 13. # 14. # This program is free software: you can redistribute it and/or modify 15. # it under the terms of the GNU General Public License as published by 16. # the Free Software Foundation, either version 3 of the License. 17. # 18. # This program is distributed in the hope that it will be useful, 19. # but WITHOUT ANY WARRANTY; without even the implied warranty of 20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21. # GNU General Public License for more details. 22. # 23. #--- 24. 25. from struct import * 26. from zlib import * 27. import stat 28. import sys 29. import os 30. 31. def getNormalizedPNG(filename): 32. pngheader = "\x89PNG\r\n\x1a\n" 33. 34. file = open(filename, "rb") 35. oldPNG = file.read() 36. file.close() 37. 38. if oldPNG[:8] != pngheader: 39. return None 40. 41. newPNG = oldPNG[:8] 42. 43. chunkPos = len(newPNG) 44. 45. # For each chunk in the PNG file 46. while chunkPos < len(oldPNG): 47. 48. # Reading chunk 49. chunkLength = oldPNG[chunkPos:chunkPos+4] 50. chunkLength = unpack(">L", chunkLength)[0] 51. chunkType = oldPNG[chunkPos+4 : chunkPos+8] 52. chunkData = oldPNG[chunkPos+8:chunkPos+8+chunkLength] 53. chunkCRC = oldPNG[chunkPos+chunkLength+8:chunkPos+chunkLength+12] 54. chunkCRC = unpack(">L", chunkCRC)[0] 55. chunkPos += chunkLength + 12 56. 57. # Parsing the header chunk 58. if chunkType == "IHDR": 59. width = unpack(">L", chunkData[0:4])[0] 60. height = unpack(">L", chunkData[4:8])[0] 61. 62. # Parsing the image chunk 63. if chunkType == "IDAT": 64. try: 65. # Uncompressing the image chunk 66. bufSize = width * height * 4 + height 67. chunkData = decompress( chunkData, -8, bufSize) 68. 69. except Exception, e: 70. # The PNG image is normalized 71. return None 72. 73. # Swapping red & blue bytes for each pixel 74. newdata = "" 75. for y in xrange(height): 76. i = len(newdata) 77. newdata += chunkData[i] 78. for x in xrange(width): 79. i = len(newdata) 80. newdata += chunkData[i+2] 81. newdata += chunkData[i+1] 82. newdata += chunkData[i+0] 83. newdata += chunkData[i+3] 84. 85. # Compressing the image chunk 86. chunkData = newdata 87. chunkData = compress( chunkData ) 88. chunkLength = len( chunkData ) 89. chunkCRC = crc32(chunkType) 90. chunkCRC = crc32(chunkData, chunkCRC) 91. chunkCRC = (chunkCRC + 0x100000000) % 0x100000000 92. 93. # Removing CgBI chunk 94. if chunkType != "CgBI": 95. newPNG += pack(">L", chunkLength) 96. newPNG += chunkType 97. if chunkLength > 0: 98. newPNG += chunkData 99. newPNG += pack(">L", chunkCRC) 100. 101. # Stopping the PNG file parsing 102. if chunkType == "IEND": 103. break 104. 105. return newPNG 106. 107. def updatePNG(filename): 108. data = getNormalizedPNG(filename) 109. if data != None: 110. file = open(filename, "wb") 111. file.write(data) 112. file.close() 113. return True 114. return data 115. 116. def getFiles(base): 117. global _dirs 118. global _pngs 119. if base == ".": 120. _dirs = [] 121. _pngs = [] 122. 123. if base in _dirs: 124. return 125. 126. files = os.listdir(base) 127. for file in files: 128. filepath = os.path.join(base, file) 129. try: 130. st = os.lstat(filepath) 131. except os.error: 132. continue 133. 134. if stat.S_ISDIR(st.st_mode): 135. if not filepath in _dirs: 136. getFiles(filepath) 137. _dirs.append( filepath ) 138. 139. elif file[-4:].lower() == ".png": 140. if not filepath in _pngs: 141. _pngs.append( filepath ) 142. 143. if base == ".": 144. return _dirs, _pngs 145. 146. print "-----------------------------------" 147. print " iPhone PNG Images Normalizer v1.0" 148. print "-----------------------------------" 149. print " " 150. print "[+] Searching PNG files...", 151. dirs, pngs = getFiles(".") 152. print "ok" 153. 154. if len(pngs) == 0: 155. print " " 156. print "[!] Alert: There are no PNG files found. Move this python file to the folder that contains the PNG files to normalize." 157. exit() 158. 159. print " " 160. print " - %d PNG files were found at this folder (and subfolders)." % len(pngs) 161. print " " 162. while True: 163. normalize = raw_input("[?] Do you want to normalize all images (Y/N)? ").lower() 164. if len(normalize) > 0 and (normalize[0] == "y" or normalize[0] == "n"): 165. break 166. 167. normalized = 0 168. if normalize[0] == "y": 169. for ipng in xrange(len(pngs)): 170. perc = (float(ipng) / len(pngs)) * 100.0 171. print "%.2f%% %s" % (perc, pngs[ipng]) 172. if updatePNG(pngs[ipng]): 173. normalized += 1 174. print " " 175. print "[+] %d PNG files were normalized." % normalized
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号