PMXBOT Log file Viewer

Help | Karma | Search:

#pil logs for Monday the 4th of November, 2013

(Back to #pil overview) (Back to channel listing) (Animate logs)
[21:26:59] <pedro3005> hello all. I have a list of 10201 floats that describe the pixels of a 101x101 image. can I use PIL to convert it to image format? I've been trying to no avail
[21:44:30] <wiredfool> pedro3005: should be possible
[21:44:52] <wiredfool> easiest way would be with numpy
[21:45:28] <pedro3005> wiredfool, how?
[21:45:31] <wiredfool> but there should be a way without it
[21:47:17] <wiredfool> for numpy,
[21:47:43] <wiredfool> nparr = numpy.array(data, numpy.float)
[21:47:59] <wiredfool> img = Image.fromarray(nparr)
[21:48:33] <wiredfool> That will get you an image with a mode of F. You'd then need to do a conversion to I
[21:48:44] <wiredfool> or maybe L, and then save it.
[21:49:08] <wiredfool> alternately, you can do any float -> int conversions in numpy
[21:52:37] <wiredfool> pedro3005: have you looked at putdata?
[21:53:17] <pedro3005> wiredfool, yes, I tried that
[21:53:25] <pedro3005> but the image was ending up black regardless
[21:53:30] <pedro3005> how do I convert F to I?
[21:53:57] <wiredfool> should be im.convert('I')
[21:54:06] <wiredfool> but you might need I32, or I16
[21:54:17] <pedro3005> let me try
[21:54:19] <wiredfool> The conversion routines aren't necessarily smart about auto contrast
[21:54:25] <wiredfool> or limits or whatnot
[21:54:43] <wiredfool> so depending on what you wnat out, you may have to do that manually first
[21:55:51] <wiredfool> putdata should work, given that there arent any large warnings about it being only for 'normal' images
[21:56:48] <wiredfool> infact, putdata has supported F for 11 years
[21:58:32] <pedro3005> well, I did:
[21:58:54] <pedro3005> im = Image.new("F", (101, 101)) \\ im.putdata(array) \\ im.convert("I") \\ im.save("out.png")
[21:59:05] <pedro3005> should I have used the numpy thing first?
[21:59:13] <wiredfool> no.
[21:59:28] <pedro3005> oh
[21:59:30] <pedro3005> i mistyped
[21:59:32] <wiredfool> so what's probably happening is that everything is working till the save
[21:59:42] <pedro3005> i actually did im1 = im.convert("I") and then im1.save
[22:00:27] <wiredfool> and then the I converts down to L on a save
[22:00:35] <wiredfool> I == 32 bit integer
[22:01:02] <wiredfool> L == 8 bit integer, and it's a hard conversion to 0->255
[22:02:10] <pedro3005> i also tried im1.show() and it showed black too
[22:02:45] <wiredfool> try im.getdata[0:10]
[22:03:01] <wiredfool> it should show something. And probably not in the 0-255 range
[22:03:32] <pedro3005> TypeError: 'instancemethod' object has no attribute '__getitem__'
[22:04:49] <wiredfool> sorry, returns iterable
[22:04:56] <wiredfool> list(im.getdata())[0:10]
[22:05:26] <pedro3005> oh
[22:05:29] <wiredfool> also, for completness, same thing with im1
[22:05:32] <pedro3005> it returns the floats
[22:05:36] <wiredfool> good
[22:05:50] <pedro3005> im1 returns [0, 0, 1, 0, 0, 0, 0, 0, 1, 0]
[22:05:53] <pedro3005> looks wrong
[22:05:58] <wiredfool> yeah
[22:06:10] <wiredfool> so, the conversion isn't scaling well
[22:06:39] <wiredfool> internally, what that conversion does is: *out++ = (INT32) *in++;
[22:07:12] <wiredfool> so if your floats aren't distributed well as pixel values, they're going to be converted badly
[22:07:31] <wiredfool> what's max(data) and min(data)?
[22:07:49] <pedro3005> 1 and 0
[22:07:58] <pedro3005> the first 10 are [0.572549045085907, 0.9686274528503418, 1.0, 0.9882352948188782, 0.9882352948188782, 0.9882352948188782, 0.4745098054409027, 0.9098039269447327, 1.0, 0.9921568632125854]
[22:08:36] <wiredfool> ah
[22:09:17] <wiredfool> so when you do putdata, call it as putdata(data, scale=255,offset=0)
[22:09:30] <wiredfool> then convert to L instead of I
[22:11:07] <pedro3005> ok, I'll try
[22:13:27] <pedro3005> well, it got something non-black
[22:13:40] <pedro3005> let me look more a little
[22:24:23] <pedro3005> wiredfool, well, it seems to be converting correctly
[22:24:57] <pedro3005> thank you for your help, now the error is probably my fault
[22:25:19] <wiredfool> glad that it's working
[22:28:32] <pedro3005> oh, I figured out
[22:28:35] <pedro3005> now everything is working!
[22:34:00] <wiredfool> Sweet
[22:46:59] <pedro3005> wiredfool, one thing: isn't L black and white?
[22:56:02] <wiredfool> L is gray
[22:56:06] <wiredfool> 1 is b/w
[22:56:17] <wiredfool> (rather, L is 256 Gray
[22:56:29] <wiredfool> I is 16 or 32bit gray)
[22:58:51] <pedro3005> hm.. well can I convert it in such a way that the color isn't lost?
[22:59:31] <pedro3005> I tried converting to RGB mode instead
[22:59:54] <wiredfool> how is the color encoded in the float array?
[23:01:07] <pedro3005> good question..