Example Code for Displaying Pictures--I'm Stumped

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Example Code for Displaying Pictures--I'm Stumped

Wayne Watson
A few hours ago someone posted some sample Python code somewhere in
several of the python lists pictures. I looked at it and thought that
looks pretty interesting. Now I can't find it. Does anyone know of that
post? Typical I use Image-SIG, Pmw-Toolkit Python Tutor, Tkinter,
VisPython lists but I don't see it.

My interest is that I would like to step through a video file of b/w
images, 640x480, and stop along the way to make histograms to determine
noise characteristics of the camera that took the video.

--
           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
              Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet  
               
                   350 350 350 350 350 350 350 350 350 350
                     Make the number famous. See 350.org
            The major event has passed, but keep the number alive.
 
                    Web Page: <www.speckledwithstars.net/>

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Example Code for Displaying Pictures--I'm Stumped

Wayne Watson
I thought I'd try executing your code under Win Python, but it objected
to indentation, tabs. I tried substituting 4 blanks for them, but that
didn't work out. Suggestions?

David Kirtley wrote:

> http://www.cs.panam.edu/~dkirtley/video/
>
> Should not need too much modification for what you want to do.
>
> David Kirtley
>
> On Tue, Nov 24, 2009 at 7:59 PM, Wayne Watson
> <[hidden email]> wrote:
>  
>> A few hours ago someone posted some sample Python code somewhere in several
>> of the python lists pictures. I looked at it and thought that looks pretty
>> interesting. Now I can't find it. Does anyone know of that post? Typical I
>> use Image-SIG, Pmw-Toolkit Python Tutor, Tkinter, VisPython lists but I
>> don't see it.
>>
>> My interest is that I would like to step through a video file of b/w images,
>> 640x480, and stop along the way to make histograms to determine noise
>> characteristics of the camera that took the video.
>>
>> --
>>          Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
>>
>>            (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
>>             Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet
>>                    350 350 350 350 350 350 350 350 350 350
>>                    Make the number famous. See 350.org
>>           The major event has passed, but keep the number alive.
>>
>>                   Web Page: <www.speckledwithstars.net/>
>>
>> _______________________________________________
>> Image-SIG maillist  -  [hidden email]
>> http://mail.python.org/mailman/listinfo/image-sig
>>
>>    
>
>  

--
           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
              Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet  
               
                   350 350 350 350 350 350 350 350 350 350
                     Make the number famous. See 350.org
            The major event has passed, but keep the number alive.
 
                    Web Page: <www.speckledwithstars.net/>

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Example Code for Displaying Pictures--I'm Stumped

Wayne Watson
David, I have many video files, but only want to process them one at a
time. The format is unique, but simple. Basically, 640x480 b/w bmp
images one right after the other. The program should read one image,
then allow the user to produce a histogram of the image displayed. Now
he moves to the next one of interest, and does the same. I started down
this path some months ago, but got side tracked. I did make some
progress, so I think I'll review that, and just see if I can finish it
off. I would think it really isn't a complicated program. In fact, I
would think such a common need, without the histograms, that it should
be available as a possible example of how to read simple video files.
I'll stick to Win Python. I've used C and Linux before, but don't see a
need to go back to it here.

David Kirtley wrote:

> Well, I really had intended it to be a guide and unless you have the
> gnuplot python stuff also, it would not run directly.
>
> What exactly are you trying to do? From what I understood, you have a
> bunch of image files and want to do histograms for each of them.
> (Unless you are trying to do them from video which is problematic. I
> personally saved the video I was working as a set of individual image
> files. Getting them from a video stream is a lot harder and has many
> difficulties- many man-made because of licensing and patents on video
> compression. I have not done it directly and would not want to mess
> with it. It is really nasty.)
>
> Also, that code is pretty old (I think 2.2 or maybe even older Python)
> the new glob stuff
> files = glob.glob(os.path.join('','*.out'))
> will make it easy to get a list of the files in the directory.
>
> So the basic strategy would be :
>
> Get the list of files to process with the glob -- Who chooses these names? ;)
>
> (Not perfect code, just rough idea.....)
>
> for file in file_list:
>    image = Image.open(file)
>    histogram = Image.chops.histogram -- don't remember where the
> histogram stuff is off the top of my head
>    make the comparison
>    save the results
>
>
>
> If you are going to be doing some in depth analysis of the data, you
> prob will want to use Numpy and deal with them as arrays.
>
> On Wed, Nov 25, 2009 at 9:44 AM, Wayne Watson
> <[hidden email]> wrote:
>  
>> I thought I'd try executing your code under Win Python, but it objected to
>> indentation, tabs. I tried substituting 4 blanks for them, but that didn't
>> work out. Suggestions?
>>
>> David Kirtley wrote:
>>    
>>> http://www.cs.panam.edu/~dkirtley/video/
>>>
>>> Should not need too much modification for what you want to do.
>>>
>>> David Kirtley
>>>
>>> On Tue, Nov 24, 2009 at 7:59 PM, Wayne Watson
>>> <[hidden email]> wrote:
>>>
>>>      
>>>> A few hours ago someone posted some sample Python code somewhere in
>>>> several
>>>> of the python lists pictures. I looked at it and thought that looks
>>>> pretty
>>>> interesting. Now I can't find it. Does anyone know of that post? Typical
>>>> I
>>>> use Image-SIG, Pmw-Toolkit Python Tutor, Tkinter, VisPython lists but I
>>>> don't see it.
>>>>
>>>> My interest is that I would like to step through a video file of b/w
>>>> images,
>>>> 640x480, and stop along the way to make histograms to determine noise
>>>> characteristics of the camera that took the video.
>>>>
>>>> --
>>>>         Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
>>>>
>>>>           (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
>>>>            Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet
>>>>                   350 350 350 350 350 350 350 350 350 350
>>>>                   Make the number famous. See 350.org
>>>>          The major event has passed, but keep the number alive.
>>>>
>>>>                  Web Page: <www.speckledwithstars.net/>
>>>>
>>>> _______________________________________________
>>>> Image-SIG maillist  -  [hidden email]
>>>> http://mail.python.org/mailman/listinfo/image-sig
>>>>
>>>>
>>>>        
>>>      
>> --
>>          Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
>>
>>            (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
>>             Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet
>>                    350 350 350 350 350 350 350 350 350 350
>>                    Make the number famous. See 350.org
>>           The major event has passed, but keep the number alive.
>>
>>                   Web Page: <www.speckledwithstars.net/>
>>
>> _______________________________________________
>> Image-SIG maillist  -  [hidden email]
>> http://mail.python.org/mailman/listinfo/image-sig
>>
>>    
>
>  

--
           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
              Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet  
               
                   350 350 350 350 350 350 350 350 350 350
                     Make the number famous. See 350.org
            The major event has passed, but keep the number alive.
 
                    Web Page: <www.speckledwithstars.net/>

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Example Code for Displaying Pictures--I'm Stumped

Chris Barker
Wayne Watson wrote:
> David, I have many video files, but only want to process them one at a
> time. The format is unique, but simple. Basically, 640x480 b/w bmp
> images one right after the other.

In here:

http://www.pythonware.com/library/pil/handbook/introduction.htm

Under "image sequences" there is enough info to tell you want to do if
PIL understands your format.


What happens when you point PIL at the file? Does it find the first
image? does it find more than one? If not, then you may need to break
them apart yourself before feeding them to PIL.


Also if it's as simple as a binary dump of a standard data type, then
you could probably use numpy's "fromfile" to read the data in, you could
then covert to a PIL image for the histogram, or just use numpy's
histogram functions to compute it.

You may want to post a (small) sample file here, and others can take a
look if you're still confused.

-Chris





--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Example Code for Displaying Pictures--I'm Stumped

Wayne Watson
I didn't know I could post, attach I guess, here. I'll do that later.

There is one puzzling format difficulty I have with the file that
requires some attention. I hope to have that cleared up in the next hour
or two. Actually, there's a twist in the format that should be
mentioned. The first image (frame) is 640x480, and I'm pretty sure one
byte per pixel. However, every other image (frame) after that is 128
pixels square. An auxiliary text file provides where, x and y pixel
position, the small image should be pasted to form a complete image with
the 640x480 frame. However, if one can get as far as simply displaying
the first frame and then the following smaller frames, I think that
would be quite sufficient for starters.

Yes, I've made some "touchdowns" before on the link you provided earlier
this year. I do need to fiddle with PIL again to get some semblance of
an idea how to use.

Christopher Barker wrote:

> Wayne Watson wrote:
>> David, I have many video files, but only want to process them one at
>> a time. The format is unique, but simple. Basically, 640x480 b/w bmp
>> images one right after the other.
>
> In here:
>
> http://www.pythonware.com/library/pil/handbook/introduction.htm
>
> Under "image sequences" there is enough info to tell you want to do if
> PIL understands your format.
>
>
> What happens when you point PIL at the file? Does it find the first
> image? does it find more than one? If not, then you may need to break
> them apart yourself before feeding them to PIL.
>
>
> Also if it's as simple as a binary dump of a standard data type, then
> you could probably use numpy's "fromfile" to read the data in, you
> could then covert to a PIL image for the histogram, or just use
> numpy's histogram functions to compute it.
>
> You may want to post a (small) sample file here, and others can take a
> look if you're still confused.
>
> -Chris
>
>
>
>
>

--
           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
              Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet  
               
                   350 350 350 350 350 350 350 350 350 350
                     Make the number famous. See 350.org
            The major event has passed, but keep the number alive.
 
                    Web Page: <www.speckledwithstars.net/>

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Example Code for Displaying Pictures--I'm Stumped

Chris Barker
Wayne Watson wrote:
> I didn't know I could post, attach I guess, here. I'll do that later.

  as long as it's a small file -- it can be much easier to help

> There is one puzzling format difficulty I have with the file that
> requires some attention. I hope to have that cleared up in the next hour
> or two. Actually, there's a twist in the format that should be
> mentioned. The first image (frame) is 640x480, and I'm pretty sure one
> byte per pixel. However, every other image (frame) after that is 128
> pixels square. An auxiliary text file provides where, x and y pixel
> position, the small image should be pasted to form a complete image with
> the 640x480 frame.

Maybe it's because I"m more familiar with numpy than PIL, but this is
how I'd do that (untested, of course...):

import numpy as np

infile = file('filename')

img = np.fromfile(infile, count=640*480, dtype=np.uint8)


for i in range(num_images):
     do_something
     small_image = np.fromfile(infile, count=128*128, dtype=np.uint8)
     img[x:x+128, y:y+128] = small_image
     ....

NOTE: np.histogram2d might do what you want for the histogram


I'm sure you can do somethign similar directly with PIL.

-Chris








--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Example Code for Displaying Pictures--I'm Stumped

jalopyuser
In reply to this post by Wayne Watson
Another good option is pyglet.  I use it for converting video to streams
of PIL images.

Here's the code for turning a pyglet image into a PIL image:

    def pyglet_to_pil_image (pyglet_image):
        image = pyglet_image.get_image_data()
        format = image.format
        if format != 'RGB':
            # Only save in RGB or RGBA formats.
            format = 'RGBA'
        pitch = -(image.width * len(format))

        # Note: Don't try and use frombuffer(..); different versions of
        # PIL will orient the image differently.
        pil_image = Image.fromstring(
            format, (image.width, image.height), image.get_data(format, pitch))
        # need to flip the image to accommodate Pyglet's transform space
        pil_image = pil_image.transpose(Image.FLIP_TOP_BOTTOM)
        return pil_image

This code is part of extensions/video.py, part of the UpLib source code
at http://uplib.parc.com/.

Bill
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Example Code for Displaying Pictures--I'm Stumped

Fredrik Lundh
On Thu, Nov 26, 2009 at 3:05 AM, Bill Janssen <[hidden email]> wrote:

>        # Note: Don't try and use frombuffer(..); different versions of
>        # PIL will orient the image differently.
>        pil_image = Image.fromstring(
>            format, (image.width, image.height), image.get_data(format, pitch))

frombuffer interprets the default decoder settings differently from
fromstring; this is being fixed long-term, but as mentioned in the
documentation, you can do it portably by spelling out the decoder
arguments:

    im = Image.frombuffer(mode, size, data, "raw", mode, 0, 1)

(1.1.7 will also warn you if you call it without the last four arguments).

</F>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Example Code for Displaying Pictures--I'm Stumped

Wayne Watson
In reply to this post by Chris Barker
Thanks. Unfortunately, the fellow that can answer my question left early
yesterday, so I won't know exactly the file's structure until Monday.

Christopher Barker wrote:

> Wayne Watson wrote:
>> I didn't know I could post, attach I guess, here. I'll do that later.
>
>  as long as it's a small file -- it can be much easier to help
>
>> There is one puzzling format difficulty I have with the file that
>> requires some attention. I hope to have that cleared up in the next
>> hour or two. Actually, there's a twist in the format that should be
>> mentioned. The first image (frame) is 640x480, and I'm pretty sure
>> one byte per pixel. However, every other image (frame) after that is
>> 128 pixels square. An auxiliary text file provides where, x and y
>> pixel position, the small image should be pasted to form a complete
>> image with the 640x480 frame.
>
> Maybe it's because I"m more familiar with numpy than PIL, but this is
> how I'd do that (untested, of course...):
>
> import numpy as np
>
> infile = file('filename')
>
> img = np.fromfile(infile, count=640*480, dtype=np.uint8)
>
>
> for i in range(num_images):
>     do_something
>     small_image = np.fromfile(infile, count=128*128, dtype=np.uint8)
>     img[x:x+128, y:y+128] = small_image
>     ....
>
> NOTE: np.histogram2d might do what you want for the histogram
>
>
> I'm sure you can do somethign similar directly with PIL.
>
> -Chris
>
>
>
>
>
>
>
>

--
           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
              Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

                   350 350 350 350 350 350 350 350 350 350
                     Make the number famous. See 350.org
            The major event has passed, but keep the number alive.

                    Web Page: <www.speckledwithstars.net/>


_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Example Code for Displaying Pictures--I'm Stumped

Wayne Watson
In reply to this post by jalopyuser
Thanks. I'll try to follow up on this soon.

Bill Janssen wrote:

> Another good option is pyglet.  I use it for converting video to streams
> of PIL images.
>
> Here's the code for turning a pyglet image into a PIL image:
>
>     def pyglet_to_pil_image (pyglet_image):
>         image = pyglet_image.get_image_data()
>         format = image.format
>         if format != 'RGB':
>             # Only save in RGB or RGBA formats.
>             format = 'RGBA'
>         pitch = -(image.width * len(format))
>
>         # Note: Don't try and use frombuffer(..); different versions of
>         # PIL will orient the image differently.
>         pil_image = Image.fromstring(
>             format, (image.width, image.height), image.get_data(format, pitch))
>         # need to flip the image to accommodate Pyglet's transform space
>         pil_image = pil_image.transpose(Image.FLIP_TOP_BOTTOM)
>         return pil_image
>
> This code is part of extensions/video.py, part of the UpLib source code
> at http://uplib.parc.com/.
>
> Bill
>
>  

--
           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
              Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

                   350 350 350 350 350 350 350 350 350 350
                     Make the number famous. See 350.org
            The major event has passed, but keep the number alive.

                    Web Page: <www.speckledwithstars.net/>


_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Example Code for Displaying Pictures--I'm Stumped

Wayne Watson
I now have a detailed description of the file, but the smallest file I
can find is 1.7M. I don't think I can go lower. It probably contains 90
128x128 frames plus the 640x480. I suppose if there's some tool in Win,
I could just cut it at some place to shorten it. Tomorrow I'm going out
of town, but should be on the web mail. I'll have my laptop, and many of
these files are on it, so I might be able to do some work on this.

Wayne Watson wrote:

> Thanks. I'll try to follow up on this soon.
>
> Bill Janssen wrote:
>> Another good option is pyglet.  I use it for converting video to streams
>> of PIL images.
>>
>> Here's the code for turning a pyglet image into a PIL image:
>>
>>     def pyglet_to_pil_image (pyglet_image):
>>         image = pyglet_image.get_image_data()
>>         format = image.format
>>         if format != 'RGB':
>>             # Only save in RGB or RGBA formats.
>>             format = 'RGBA'
>>         pitch = -(image.width * len(format))
>>
>>         # Note: Don't try and use frombuffer(..); different versions of
>>         # PIL will orient the image differently.
>>         pil_image = Image.fromstring(
>>             format, (image.width, image.height),
>> image.get_data(format, pitch))
>>         # need to flip the image to accommodate Pyglet's transform space
>>         pil_image = pil_image.transpose(Image.FLIP_TOP_BOTTOM)
>>         return pil_image
>>
>> This code is part of extensions/video.py, part of the UpLib source code
>> at http://uplib.parc.com/.
>>
>> Bill
>>
>>  
>

--
           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
              Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet  
               
          The popular press and many authorities believe the number
          of pedifiles that prowl the web is 50,00. There are no
          figures that support this. The number of children below
          18 years of age kidnapped by strangers is 1 in 600,00,
          or 115 per year. -- The Science of Fear by D. Gardner
 
                    Web Page: <www.speckledwithstars.net/>

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig