Results 1 to 2 of 2

Thread: PIL convert 16bit grayscale to 8 bit

  1. #1
    Join Date
    Jun 2006
    Beans
    2,930

    PIL convert 16bit grayscale to 8 bit

    I made a utility that works with some scanned images. Everything was working until I started scanning grayscale images.

    It seems that the only issue is that the images cannot be displayed in the gui if they are 16bit, but it looks like the manipulations work fine. So as a fix I wanted to convert the image that is displayed in the gui to 8 bit, but I have not been successful with this. Any ideas?

    Code:
    #! /usr/bin/env python
    
    import Image
    import sys
    filename = sys.argv[1]
    
    im = Image.open(filename)
    print im.size
    print im.mode
    im2 = im.convert('L')  # this doesn't do what I was wanting
    print im2.mode
    
    im2.show()
    Code:
    python imgtest.py grayscale_example.png 
    (85, 63)
    I
    L
    Edit: Attached is an example image
    Edit2: realized convert creates a new object instead of replacing existing, but it still doesn't convert the pixel data to be visible. The displayed image is solid white.
    Attached Images Attached Images
    Last edited by lavinog; May 14th, 2010 at 06:45 PM.
    Support 7z in default installs!!!: Click Here

    How to use code blocks to post command output: Click Here
    Official Ubuntu Documentation

  2. #2
    Join Date
    Jun 2006
    Beans
    2,930

    Re: PIL convert 16bit grayscale to 8 bit

    Ok, I figured it out:
    Code:
    #! /usr/bin/env python
    
    import Image
    import sys
    
    filename = sys.argv[1]
    
    im = Image.open(filename)
    print im.size
    print im.mode
    
    table=[ i/256 for i in range(65536) ]
    
    im2 = im.point(table,'L')
    
    print im2.mode
    
    im2.show()
    Support 7z in default installs!!!: Click Here

    How to use code blocks to post command output: Click Here
    Official Ubuntu Documentation

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •