2

I wish I could ask this question in context, but I don't have enough rep to comment and ask my question there, so I have to make a new post. I am trying to use the code from this comment:

https://stackoverflow.com/a/21034111/432509

I am working in Python in Houdini at school, so I am limited on what libraries I can utilize to what they have installed, so I was going to use the pure python implementation to write some data out as a PNG map, but I am running into an error in this line:

def saveAsPNG(array, f):
    import struct
    if any([len(row) != len(array[0]) for row in array]):
        raise ValueError, "Array should have elements of equal size"

                            #First row becomes top row of image.
    flat = []; map(flat.extend, reversed(array))
                             #Big-endian, unsigned 32-byte integer.

    buf = b''.join([struct.pack('>I', ((0xffFFff & i32)<<8)|(i32>>24) )
               for i32 in flat])   #Rotate from ARGB to RGBA.

    data = write_png(buf, len(array[0]), len(array))
    #f = open(filename, 'wb')
    f.write(data)
    f.close()

I am getting the following error:

TypeError: unsupported operand type(s) for &: 'int' and 'str'

Unfortunately I am not familiar with all of this syntax and as such I am not able to repair the error myself. Any clues?

Community
  • 1
  • 1
Adalast
  • 151
  • 1
  • 1
  • 8
  • 1
    It looks like `i32` is a `str`, not a number... which means that your `flat` iterable has some strings it in... – mgilson May 02 '16 at 22:12
  • 1
    What does `flat` look like? – Natecat May 02 '16 at 22:12
  • @mgilson ok, now that I am looking over the code, yes, they are being formatted as strings, but how do I convert them into the right format? Can I just use hex()? – Adalast May 02 '16 at 22:16
  • 1
    @adalast -- `hex` will give you a string. You probably want `int`. – mgilson May 02 '16 at 22:17
  • Does your `array` contain numerical values? – Jongware May 02 '16 at 22:18
  • @mgilson the format that he gave is in the format 0x########, which i currently have as a string. can I just use int() on that? – Adalast May 02 '16 at 22:18
  • 2
    @Adalast -- I think you can (setting the base to 16). e.g. `int('0x555', 16)` – mgilson May 02 '16 at 22:20
  • @mgilson thank you thank you thank you thank you... that worked for fixing that error, but it doesn't seem to be writing anything to the file. I am getting a blank map file, even though I know that the data is going into my array right. – Adalast May 02 '16 at 22:35

0 Answers0