|
11 | 11 | If that URL should fail, try contacting the author. |
12 | 12 | ''' |
13 | 13 |
|
14 | | -from __future__ import unicode_literals, print_function, division |
| 14 | +from __future__ import division, print_function, unicode_literals |
15 | 15 |
|
16 | 16 | __version__ = '1.0.0' |
17 | 17 | __author__ = "Mickaël 'Tiger-222' Schoentgen" |
@@ -190,31 +190,33 @@ def save_img(self, data, width, height, output): |
190 | 190 | http://inaps.org/journal/comment-fonctionne-le-png |
191 | 191 | ''' |
192 | 192 |
|
193 | | - zcrc32 = crc32 |
194 | | - zcompr = compress |
195 | | - len_sl = width * 3 |
| 193 | + len_sl = (width * 3 + 3) & -4 |
| 194 | + padding = 0 if len_sl % 8 == 0 else (len_sl % 8) // 2 |
196 | 195 | scanlines = b''.join( |
197 | | - [b'0' + data[y * len_sl:y * len_sl + len_sl] |
| 196 | + [b'0' + data[y * len_sl:y * len_sl + len_sl - padding] |
198 | 197 | for y in range(height)]) |
| 198 | + zcrc32 = crc32 |
| 199 | + zcompr = compress |
| 200 | + b = pack |
199 | 201 |
|
200 | | - magic = pack(b'>8B', 137, 80, 78, 71, 13, 10, 26, 10) |
| 202 | + magic = b(b'>8B', 137, 80, 78, 71, 13, 10, 26, 10) |
201 | 203 |
|
202 | 204 | # Header: size, marker, data, CRC32 |
203 | 205 | ihdr = [b'', b'IHDR', b'', b''] |
204 | | - ihdr[2] = pack(b'>2I5B', width, height, 8, 2, 0, 0, 0) |
205 | | - ihdr[3] = pack(b'>I', zcrc32(b''.join(ihdr[1:3])) & 0xffffffff) |
206 | | - ihdr[0] = pack(b'>I', len(ihdr[2])) |
| 206 | + ihdr[2] = b(b'>2I5B', width, height, 8, 2, 0, 0, 0) |
| 207 | + ihdr[3] = b(b'>I', zcrc32(b''.join(ihdr[1:3])) & 0xffffffff) |
| 208 | + ihdr[0] = b(b'>I', len(ihdr[2])) |
207 | 209 |
|
208 | 210 | # Data: size, marker, data, CRC32 |
209 | 211 | idat = [b'', b'IDAT', b'', b''] |
210 | 212 | idat[2] = zcompr(scanlines, 9) |
211 | | - idat[3] = pack(b'>I', zcrc32(b''.join(idat[1:3])) & 0xffffffff) |
212 | | - idat[0] = pack(b'>I', len(idat[2])) |
| 213 | + idat[3] = b(b'>I', zcrc32(b''.join(idat[1:3])) & 0xffffffff) |
| 214 | + idat[0] = b(b'>I', len(idat[2])) |
213 | 215 |
|
214 | 216 | # Footer: size, marker, None, CRC32 |
215 | 217 | iend = [b'', b'IEND', b'', b''] |
216 | | - iend[3] = pack(b'>I', zcrc32(iend[1]) & 0xffffffff) |
217 | | - iend[0] = pack(b'>I', len(iend[2])) |
| 218 | + iend[3] = b(b'>I', zcrc32(iend[1]) & 0xffffffff) |
| 219 | + iend[0] = b(b'>I', len(iend[2])) |
218 | 220 |
|
219 | 221 | with open(output, 'wb') as fileh: |
220 | 222 | fileh.write( |
|
0 commit comments