On Mon, 2008-08-18 at 16:24 +0530, Ashish Sethi wrote: > Hey Lie, > Thanks for replying so promptly. > Here is my real problem which I posted as a new post:-
You should always Reply-to-all so everybody in the tutor-list could see the response. > I have a problem in converting between different RGB color spaces.I > have a jpeg file in rgb mode. > The rgb data by default is in RGB[8:8:8] mode...i.e, 8 bits(1 byte) > per band(r,g,b). > I need to convert it into RGB[4:4:4] format by getting the rgb > information of each pixel in the image > and converting the ascii values obtained for the R,G and B bands into > binary form and then clip the least significant 4 bits > from this binary no. to form a 4 bit binary value and then convert > this back to ascii and and save the new information back > to the image file. Untill now, I have been able obtain the pixel > information of my image file using : > > >>> im = Image.open("image.jpg") > > >>> pix = im.load() > > >>> pix[0, 0] > > (226, 162, 125) > > 1.Now, how do i convert this ascii data to binary? You mean you want to convert the string 'A' to its binary value 65? Use the built-in function ord('A'). But the problem you seems to be having, I think, isn't about that. pix[0, 0] returns a tuple that contains the three integers for each RGB. So you just need to do something like this (sort of): for r, g, b in pix: # do something with r, g ,b > 2.how do i mask last 4 bits ( least significant bits )? I think some it, bit-wise operation would do: http://docs.python.org/ref/bitwise.html How to bit-mask: http://en.wikipedia.org/wiki/Mask_(computing) >>> v1 = int('01000101', 2) >>> v2 = int('11110000', 2) >>> print v1, v2 69 240 >>> # 69 (base-10) is 01000101 (base-2) >>> # 240 (base-10) is 11110000 (base-2) >>> print v1 & v2 # bit-wise and 65 >>> # 65 (base-10) is 01000000 (base-2) >>> # 01000101 bitwise-and 11110000 is 01000000 > 3.also, I need to convert the same image to RGB[12:12:12] mode,for > which i need to zero pad with 4 zeroes the > binary RGB[8:8:8] data. How do I do this? > So, regarding my initial problem...once i solve this problem...i will > have the pixel wise data in > an array or list...so to write this data back to the image file...i > have to use fromarray command I'm not aware that PIL has a fromarray command, do you mean fromstring? > .But that unfortunately isnt giving back an image file... I think JPEG (at least PIL's JPEG) doesn't support 4-bit/color and 12-bit/color representation. JPEG supports 'L'uminance (8-bit/pixel), RGB (8-bit/color), CMYK (I'm not sure). You probably need another image format, one that support 12-bit/color format (I'm not aware if any popular image format supports that, but I'm not imaging expert) > I would also appreciate any help you could offer me regarding the new > problem. > > Looking forward to your reply > > > > > > On 8/18/08, Lie Ryan <[EMAIL PROTECTED]> wrote: > > Message: 3 > > Date: Mon, 18 Aug 2008 14:30:08 +0530 > > From: "Ashish Sethi" <[EMAIL PROTECTED]> > > Subject: [Tutor] problem in converting pixel data to image > file > > To: tutor@python.org > > Message-ID: > > > <[EMAIL PROTECTED]> > > Content-Type: text/plain; charset="utf-8" > > > > Hi all, > > I have a problem in converting the pixel data (read from a > string and > > written to a file using fromstring command in PIL ). > > The file handle of this file is called buffer. Now, when I > tried to > > open the > > file as an image file but it didnt work. > > What is the format of the image you want to open? PIL doesn't > support > all image format in the world (of course), but the most common > ones are > supported. fromstring, by default, assumes that the string > you're > passing it is in raw format (i.e. the definition of raw format > is > explained PIL's documentation) > > > Then I read the documentation of PIL and found this written > about > > fromstring > > function > > "Note that this function decodes pixel data, not entire > images. If you > > have an entire image in a string, wrap it in a *StringIO > *object, and > > use > > *open *to load it." > > StringIO is simply a wrapper around string to make it have a > file-like > interface (since Python uses Duck Typing principle, if an > object has the > same members a file-object have, like read(), write(), > readlines(), etc > python should not differentiate them) > > > so i wrote the following code.... > > > > file = StringIO.StringIO(buffer) > > img = Image.open(file) > > img.save(file, 'JPEG') > > > > *Error:* > > img = Image.open(file) > > File > "/home/rahhal/python/lib/python2.4/site-packages/PIL/Image.py", > > line > > 1745, in open > > raise IOError("cannot identify image file") > > IOError: cannot identify image file > > PIL is it cannot identify what format the original image is > in, either > you pass explicitly what format the original file is in, or > (at the > worst case) you create (or search whether one is available on > the > internet) your own decoder. > > PS: List of image format supported by PIL (you can also find > it in PIL's > documentation): > BMP > BUFR (identify only) > CUR (read only) > DCX (read only) > EPS (write-only) > FITS (identify only) > FLI, FLC (read only) > FPX (read only) > GBR (read only) > GD (read only) > GIF > GRIB (identify only) > HDF5 (identify only) > ICO (read only) > IM > IMT (read only) > IPTC/NAA (read only) > JPEG > MCIDAS (read only) > MIC (read only) > MPEG (identify only) > MSP > PALM (write only) > PCD (read only) > PCX > PDF (write only) > PIXAR (read only) > PNG > PPM > PSD (read only) > SGI (read only) > SPIDER > TGA (read only) > TIFF > WAL (read only) > WMF (identify only) > XBM > XPM (read only) > XV Thumbnails > > > Can any one please help in solving my problem?? > > -------------- next part -------------- > > An HTML attachment was scrubbed... > > URL: > > > > <http://mail.python.org/pipermail/tutor/attachments/20080818/e7a1c2bd/attachment-0001.htm> > > > > ------------------------------ > > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor