Re: [sugar] how to make a grayscale image?
On Nov 2, 2007, at 3:16 , Albert Cahalan wrote: Eben Eliason writes: Roughly speaking, you can calculate a colored pixel's effective luminance by: Y = 0.3*R+0.59*G+0.11*B To be clear on why this is rough: it performs an operation on non-linear data which is only valid on linear data. That is, it ignores gamma. From best to worst: a. convert to linear, Y = 0.3*R+0.59*G+0.11*B, convert back b. square, Y = 0.3*R+0.59*G+0.11*B, square root c. Y = 0.3*R+0.59*G+0.11*B d. Y = (R+G+G+B)2 e. Y = G FYI, most interesting image operations are only valid on linear data. This includes scaling and alpha blending. Lots of programmers degrade images by screwing this up. We should all try to do better, especially when the image is something that might be important to the user. Right. For example, am I the only one who is bothered by the huge change in perceived brightness of different colors when you switch the DCON to grayscale mode? This switch from swizzling to the per- pixel brightness calculation gives a huge difference. - Bert - ___ Sugar mailing list Sugar@lists.laptop.org http://lists.laptop.org/listinfo/sugar
Re: [sugar] how to make a grayscale image?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Erik Blankinship wrote: Anyone know of an easy technique for turning a pixbuf or cairo context into grayscale? I can't tell what you mean. Do you mean to take a snapshot of an existing pixbuf, convert that snapshot to grayscale, and display the snapshot over the existing pixbuf? If so, the easiest way I know of is gtk.gdk.Pixbuf.get_pixels_array and gtk.gdk.pixbuf_new_from_array [1]. These methods allow you to do math on the pixels as a Numeric Python array, and then make a new Pixbuf from the resulting array. 1. http://www.pygtk.org/docs/pygtk/class-gdkpixbuf.html#method-gdkpixbuf--get-pixels-array -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.7 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFHKmkCUJT6e6HFtqQRAgrjAJ47VHqmT9j7xVAyNzgKXdgDTxQWMQCffJ6k D26RDImuualfzAdlJNaP4k0= =Oc7S -END PGP SIGNATURE- ___ Sugar mailing list Sugar@lists.laptop.org http://lists.laptop.org/listinfo/sugar
Re: [sugar] how to make a grayscale image?
thanks everyone! import gtk def grayScalePixBuf( pb, copy ): arr = pb.get_pixels_array() if (copy): arr = arr.copy() for row in arr: for pxl in row: y = 0.3*pxl[0][0]+0.59*pxl[1][0]+0.11*pxl[2][0] pxl[0][0] = y pxl[1][0] = y pxl[2][0] = y return gtk.gdk.pixbuf_new_from_array(arr, pb.get_colorspace(), pb.get_bits_per_sample()) pb = gtk.gdk.pixbuf_new_from_file(cat.jpg) pb = grayScalePixBuf(pb, True) pb.save( grayCat.png, png, {} ) On 11/1/07, Benjamin M. Schwartz [EMAIL PROTECTED] wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Erik Blankinship wrote: Anyone know of an easy technique for turning a pixbuf or cairo context into grayscale? I can't tell what you mean. Do you mean to take a snapshot of an existing pixbuf, convert that snapshot to grayscale, and display the snapshot over the existing pixbuf? If so, the easiest way I know of is gtk.gdk.Pixbuf.get_pixels_array and gtk.gdk.pixbuf_new_from_array[1]. These methods allow you to do math on the pixels as a Numeric Python array, and then make a new Pixbuf from the resulting array. 1. http://www.pygtk.org/docs/pygtk/class-gdkpixbuf.html#method-gdkpixbuf--get-pixels-array -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.7 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFHKmkCUJT6e6HFtqQRAgrjAJ47VHqmT9j7xVAyNzgKXdgDTxQWMQCffJ6k D26RDImuualfzAdlJNaP4k0= =Oc7S -END PGP SIGNATURE- ___ Sugar mailing list Sugar@lists.laptop.org http://lists.laptop.org/listinfo/sugar
Re: [sugar] how to make a grayscale image?
Eben Eliason writes: Roughly speaking, you can calculate a colored pixel's effective luminance by: Y = 0.3*R+0.59*G+0.11*B To be clear on why this is rough: it performs an operation on non-linear data which is only valid on linear data. That is, it ignores gamma. From best to worst: a. convert to linear, Y = 0.3*R+0.59*G+0.11*B, convert back b. square, Y = 0.3*R+0.59*G+0.11*B, square root c. Y = 0.3*R+0.59*G+0.11*B d. Y = (R+G+G+B)2 e. Y = G FYI, most interesting image operations are only valid on linear data. This includes scaling and alpha blending. Lots of programmers degrade images by screwing this up. We should all try to do better, especially when the image is something that might be important to the user. ___ Sugar mailing list Sugar@lists.laptop.org http://lists.laptop.org/listinfo/sugar