简体   繁体   中英

PyPng is giving me a very strange error, what can i do?

I'm using PyPng to draw Png-Files. I'm new to it (new as in 20 minues) and I use the following Code to generate a 6 by 6 pixel image, but it doesn't work. I've tried the same code (maybe or maybe not copied from the inte.net) on a 3 by 6 image and it worked fine, but every time I get this error message now:

Traceback (most recent call last): File "C:\Users\FakeUserTotallyMadeUp\Desktop\Python\imgGen\imgGen.py", line 21, in w.write(f, p)

File "C:\Users\FakeUserTotallyMadeUp\AppData\Roaming\Python\Python37\site-packages\png.py", line 668, in write nrows = self.write_passes(outfile, check_rows(rows))

File "C:\Users\FakeUserTotallyMadeUp\AppData\Roaming\Python\Python37\site-packages\png.py", line 703, in write_passes return self.write_packed(outfile, rows)

File "C:\Users\FakeUserTotallyMadeUp\AppData\Roaming\Python\Python37\site-packages\png.py", line 723, in write_packed self.write_preamble(outfile)

File "C:\Users\FakeUserTotallyMadeUp\AppData\Roaming\Python\Python37\site-packages\png.py", line 770, in write_preamble 0, 0, self.interlace))

struct.error: required argument is not an integer

I've tried many things to repair it, but nothing worked.

Here's the code:

import png

rowofrgb = ()
newrowofrgb = ()

p = [(255,0,0, 255,0,0, 0,255,0, 0,255,0, 0,0,255, 0,0,255)]

for i in range(5):
    rowofrgb = p[i]

    for b in rowofrgb:
        addtonewrow = (round(b/2),)
        newrowofrgb += addtonewrow
    
    rowofrgb = newrowofrgb
    newrowofrgb = ()
    p.append(rowofrgb)

f = open('swatch.png', 'wb')
w = png.Writer(len(p[0])/3, len(p), greyscale=False)
w.write(f, p)
f.close()

First thing I see is that p is a list containing 1 element, a single tuple, and that tuple consists of 18 integers. Perhaps you meant a list of 6 tuples, 3 elements each:

p = [(255,0,0), (255,0,0), (0,255,0), (0,255,0), (0,0,255), (0,0,255)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM