{"action":"created","issue":{"url":"https://api.github.com/repos/nortikin/sverchok/issues/1586","repository_url":"https://api.github.com/repos/nortikin/sverchok","labels_url":"https://api.github.com/repos/nortikin/sverchok/issues/1586/labels{/name}","comments_url":"https://api.github.com/repos/nortikin/sverchok/issues/1586/comments","events_url":"https://api.github.com/repos/nortikin/sverchok/issues/1586/events","html_url":"https://github.com/nortikin/sverchok/issues/1586","id":224437175,"number":1586,"title":"Custom PNG saving","user":{"login":"kalwalt","id":1275858,"avatar_url":"https://avatars1.githubusercontent.com/u/1275858?v=3","gravatar_id":"","url":"https://api.github.com/users/kalwalt","html_url":"https://github.com/kalwalt","followers_url":"https://api.github.com/users/kalwalt/followers","following_url":"https://api.github.com/users/kalwalt/following{/other_user}","gists_url":"https://api.github.com/users/kalwalt/gists{/gist_id}","starred_url":"https://api.github.com/users/kalwalt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kalwalt/subscriptions","organizations_url":"https://api.github.com/users/kalwalt/orgs","repos_url":"https://api.github.com/users/kalwalt/repos","events_url":"https://api.github.com/users/kalwalt/events{/privacy}","received_events_url":"https://api.github.com/users/kalwalt/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-04-26T11:36:13Z","updated_at":"2017-04-26T11:58:53Z","closed_at":null,"body":"Discussion from https://github.com/nortikin/sverchok/issues/1566\r\nBefore to ask a pull request, i'm testing the code that @zeffii pointed out in this link http://stackoverflow.com/a/19174800/1243487\r\noriginal code from @ideasman42 and @Evgeni_Sergeev\r\n\r\n**PNG_test.py**\r\n\r\n```python\r\n'''\r\ncode taken from:\r\nhttp://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image/19174800#19174800\r\nthanks to @ideasman42 and @Evgeni Sergeev\r\n'''\r\n\"\"\"\r\nin buf s d=[[]] n=1\r\nout some s\r\n\"\"\"\r\n\r\ndef write_png(buf, width, height):\r\n    \"\"\" buf: must be bytes or a bytearray in Python3.x,\r\n        a regular string in Python2.x.\r\n    \"\"\"\r\n    import zlib, struct\r\n\r\n    # reverse the vertical line order and add null bytes at the start\r\n    width_byte_4 = width * 4\r\n    raw_data = b''.join(b'\\x00' + buf[span:span + width_byte_4]\r\n                        for span in range((height - 1) * width_byte_4, -1, - width_byte_4))\r\n\r\n    def png_pack(png_tag, data):\r\n        chunk_head = png_tag + data\r\n        return (struct.pack(\"!I\", len(data)) +\r\n                chunk_head +\r\n                struct.pack(\"!I\", 0xFFFFFFFF & zlib.crc32(chunk_head)))\r\n\r\n    return b''.join([\r\n        b'\\x89PNG\\r\\n\\x1a\\n',\r\n        png_pack(b'IHDR', struct.pack(\"!2I5B\", width, height, 8, 6, 0, 0, 0)),\r\n        png_pack(b'IDAT', zlib.compress(raw_data, 9)),\r\n        png_pack(b'IEND', b'')])\r\n        \r\n\r\ndef saveAsPNG(array, filename):\r\n    import struct\r\n    if any([len(row) != len(array[0]) for row in array]):\r\n        raise ValueError(\"Array should have elements of equal size\")\r\n\r\n                                #First row becomes top row of image.\r\n    flat = []; map(flat.extend, reversed(array))\r\n                                 #Big-endian, unsigned 32-byte integer.\r\n    buf = b''.join([struct.pack('>I', ((0xffFFff & i32)<<8)|(i32>>24) )\r\n                    for i32 in flat])   #Rotate from ARGB to RGBA.\r\n\r\n    data = write_png(buf, len(array[0]), len(array))\r\n    f = open(filename, 'wb')\r\n    f.write(data)\r\n    f.close()\r\n    \r\nsaveAsPNG(buf, 'test_grid.png')\r\n#saveAsPNG([[0xffFF0000, 0xffFFFF00],\r\n           #[0xff00aa77, 0xff333333]], 'test_EU_grid.png')\r\n\r\n```\r\n\r\nmy nodeTree:\r\n\r\n![savepng_custom](https://cloud.githubusercontent.com/assets/1275858/25432339/5913cca2-2a84-11e7-84df-581bd2ae6feb.png)\r\n\r\ngist of nodetree:\r\nhttps://gist.github.com/432eed6f4e3befe150bf037bc4d7e09f\r\n\r\nBu there is a difference from the imaged passed to blender image editor and png saved from the script:\r\n\r\n**texture_grid.png** --> image passed and saved from blender image editor\r\n![texture_grid](https://cloud.githubusercontent.com/assets/1275858/25432452/c7072420-2a84-11e7-834d-5b4b9aedb191.png)\r\n\r\n**test_grid.png** --> saved directly from the script\r\n![test_grid](https://cloud.githubusercontent.com/assets/1275858/25432493/f8967e46-2a84-11e7-8617-58045546526d.png)\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/nortikin/sverchok/issues/comments/297379900","html_url":"https://github.com/nortikin/sverchok/issues/1586#issuecomment-297379900","issue_url":"https://api.github.com/repos/nortikin/sverchok/issues/1586","id":297379900,"user":{"login":"zeffii","id":619340,"avatar_url":"https://avatars0.githubusercontent.com/u/619340?v=3","gravatar_id":"","url":"https://api.github.com/users/zeffii","html_url":"https://github.com/zeffii","followers_url":"https://api.github.com/users/zeffii/followers","following_url":"https://api.github.com/users/zeffii/following{/other_user}","gists_url":"https://api.github.com/users/zeffii/gists{/gist_id}","starred_url":"https://api.github.com/users/zeffii/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zeffii/subscriptions","organizations_url":"https://api.github.com/users/zeffii/orgs","repos_url":"https://api.github.com/users/zeffii/repos","events_url":"https://api.github.com/users/zeffii/events{/privacy}","received_events_url":"https://api.github.com/users/zeffii/received_events","type":"User","site_admin":false},"created_at":"2017-04-26T11:58:53Z","updated_at":"2017-04-26T11:58:53Z","body":"```\r\nlibpng error: Not enough image data\r\nDDS: trying to read beyond end of stream (corrupt file?)\r\nIMB_ibImageFromMemory: unknown fileformat (C:\\Users\\zeffi\\Desktop\\b278c\\test_grid.png)\r\n```"}}
{"actor":{"display_login":"zeffii"}}