Thanks for ur reply, But the thing is I was under the impression that in a ppm image the maximum or the min color value that any of the R,G and B components can take...i.e. if instead of 255 in the file below, if I give 16 then automatically it would imply my color space has changed to RGB 4:4:4 as all the color components now would be in the range 0-16 (i.e 4 bit binary...and let me clarify...i am repeatedly talking about binary from time to time as I use the color information in binary to clip/zero pad them to change the color space from rgb888 to rgb444 or rgb121212.That is it, not that i wish to save my files in binary format. After doing the above manipulation i convert them back to decimal nos. and save them to the ppm files.)
So here i am: 1. I took the image data of a ppm file in rgb888 color space..eg say-(255,255,255) 2. I converted this data to binary and did manipulations on it and then converted them back to decimal form...eg say-(15,15,15) 3. I have now a list full of such tuples of 3 member r,g,b information. The help now require is to create a ppm(p3) file using this list with the correct header and dimensions. Also, is it required to register this file as an image file or something, coz I have plenty of such ppm files in my system which pil doesnt recognise as image files. And the ones that it does recognize as image files have garbage data (apart from the header part) when opened in a text editor. Looking forward to your reply On 8/19/08, Lie Ryan <[EMAIL PROTECTED]> wrote: > On Tue, 2008-08-19 at 15:31 +0530, Ashish Sethi wrote: > > Hey frnds, > > thanks for replying to me earlier query. > > I have been able to solve most of my problems and have 1 more and much > > straightforward problem. I am working with PPM/PGM/PBM file formats > > which have the basic format :- > > > > P2 > > # feep.pgm > > 3 3 > > 255 > > 0 0 0 0 0 0 0 0 0 > > 0 0 0 9 24 15 10 27 18 > > 0 0 0 9 24 15 10 27 18 > > where line1= type of file(p1=PBM,p2=PGM,p3=PPM), line2=comment, > > line3=dimension of the image (x and y length),line4=maximum value of > > color( here 15 means 4 bit R,G,B data) > > NOW, uptil now, I have written the following code:- > > PNM (the collective name for PBM, PGM, and PPM is a relatively little > known format that is designed for simplicity in mind (but it obviously > not designed for space-saving), but you should know that there is also > the fourth-format PAM (Portable Arbitrary Map), which goes by P7 > (http://netpbm.sourceforge.net/doc/pam.html ). PBM is used for 2-bit > image, PGM for 8-bit image, and PPM for 24-bit image, that's quite > restrictive, but PAM is designed so the data can be any-bit image (in > fact it may be used not only for images, but any data that may be > represented as row and collumns). Since you want an image that is > represented in a quite unusual bit-depth (4-bit and 12-bit), P7 is the > perfect format for you. > > > import Image > > im="C:\\Documents and Settings\\ashishs trainee\\Desktop\\lena.ppm" > > i=Image.open(im) > > x=list(i.getdata()) > > z=len(x) > > xbar=[] > > count=0 > > > > while (count<z-1): > > q=[] > > co=0 > > while(co<3): > > y=x[count] > > q.append(convert_r8_2_r4(y[co])) > > co+=1 > > xbar.append(q) > > count+=1 > > > > A bit of advice: you should choose a better names instead of x, y, q, > co, z, xbar. And you should think in Python, i.e. using for-loop's > capability to loop over list directly. > > > > > Here, x is a list of all 3 member tuples of R,G,B data of each and > > every pixel. > > Here and a few lines after this may be used for your PAM header. > DEPTH 3 > > > The tuples in x have RGB values in the range 0-255..i.e > > 8 bit binary. > > # 4-bit/color > MAXVAL 16 > # 8-bit/color > MAXVAL 256 > # 12-bit/color > MAXVAL 4096 > > > Using the function convert_r8_2_r4() I was able to > > convert these values in the range 0-16...i.e 4 bit binary by clipping > > the least significant 4 bits. By doing this I was able to create xbar > > which is in the same format as x...i.e list of 3 member tuples of > > R,G,B data. Now my problem is how do i create another image of the > > same dimensions as the original in the same format as original using > > this xbar list. > > use something like this: > # untested > def listtopambody(image, depth, width, height, maxval): > def tobinary1(sample): > ''' Convert an integer to 1-byte binary ''' > assert sample < 256 > return chr(int(sample)) > def tobinary2(sample): > ''' Convert an integer to 2-byte binary (Big Endian) ''' > assert sample < 65536 > high, low = divmod(int(sample), 256) > return chr(high) + chr(low) > > assert maxval <= 65535 # PAM limitation > > # calculate number of bytes per sample > # and select the appropriate function > tobinary = tobinary1 if maxval < 256 else tobinary2 > > # Generate the body > pam_body = '' > for pixel in image: > for sample in pixel: > pam_body += tobinary(sample) > return pam_body > # w, h, maxval = ... > pam_body = listtopambody(im.getdata(), 3, w, h, maxval) > pam = pam_header + pam_body > > > > > > > Plz help > > > > > > On 8/18/08, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > > > > > > Ashish Sethi wrote: > > > > > > > > > > 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. > > > > 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." > > > > 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 > > > > Can any one please help in solving my problem?? > > > > > > > > > > What's "pixel data" here? Image.open expects the file to contain an > > > image in a known image interchange format (e.g. JPEG, PNG, etc). If you > > > have raw data, you might be able to use frombuffer or fromstring. > > > > > > </F> > > > > > > _______________________________________________ > > > Image-SIG maillist - Image-SIG@python.org > > > http://mail.python.org/mailman/listinfo/image-sig > > > > > _______________________________________________ Image-SIG maillist - Image-SIG@python.org http://mail.python.org/mailman/listinfo/image-sig