-1

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 internet) 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()
Mr. MAD
  • 1
  • 4
  • It's telling you it wants an integer, but `len(p[0])/3` produces `6.0`, a float. Try `len(p[0])//3` to get `6`, an integer. – Mark Apr 23 '22 at 16:40

1 Answers1

0

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)]
Michał
  • 124
  • 5
  • Good Thinking, but one tuple contains one row, and the programm simply reads three tuples at once and makes a rgb value out of it. I don't know how it is doing it precisely, but thats roughtly how it works – Mr. MAD Apr 23 '22 at 17:56