Setting an Image File Values

Discussion in 'Python' started by W. Watson, May 4, 2004.

  1. W. Watson

    W. Watson Guest

    I have a 640x480 b/w bmp image file that can be converted to a dat file. I would like
    to convert the value of each pixel that is below say 120 units to exactly 40 units.
    I've never written a python program in my life. However, the manual for the image
    application I'm looking at show the following line to create a mask.dat file of size
    640x480 bytes with every value set to 40:
    python -c "open('mask.dat','w').write(chr(40)*640*480)"
    I suspect a program to do what I need is not much more complicated. Can someone
    construct a program for me to do the job? It'll probably be the only python program I
    ever need.

    --
    Wayne T. Watson (121.015 Deg. W, 39.262 Deg. N, 2,701 feet, Nevada City, CA)
    -- GMT-8 hr std. time, RJ Rcvr 39° 8' 0" N, 121° 1' 0" W

    Two laws Newton and Einstein didn't discover:
    1. Time is money.
    2. Matter will be damaged in direct proportion to its value.

    Web Page: <home.earthlink.net/~mtnviews>
    sierra_mtnview -at- earthlink -dot- net
    Imaginarium Museum: <home.earthlink.net/~mtnviews/imaginarium.html>
    W. Watson, May 4, 2004
    #1
    1. Advertising

  2. W. Watson wrote:

    > I have a 640x480 b/w bmp image file that can be converted to a dat
    > file. I would like to convert the value of each pixel that is below
    > say 120 units to exactly 40 units.


    ....

    > python -c "open('mask.dat','w').write(chr(40)*640*480)"


    Well, if you're going to take this as the baseline, then we can do some
    pretty darn simple stuff indeed:

    >>> import string
    >>> def createMask( threshold=120, target=40 ):

    .... """Create 256-char mapping of destination characters"""
    .... return string.maketrans( "".join( [chr(x) for x in
    range(threshold)]), chr(target) * threshold )
    ....
    >>> data = open( "p:\\drawcurve.py", 'rb' ).read() # mode 'rb' is

    important! use 'wb' to write
    >>> mask = createMask()
    >>> data

    "'''Test of the glVertex function\r\n\r\nDrawing TT glyphs as Cubic
    splines...\r\n\thttp://support.microsoft.com/support/kb/articles/q243/2/85.asp\r\n\r\n'''\r\nfrom
    OpenGLContext import testingcontext\r\nBaseContext"
    >>> data.translate( mask )

    '((((((((((((((((((((((x((((((((((((((((((((((((((y(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((x(((((((((((((((((((((x((((((((((((x('
    >>> ord('x') # just to see why x and y show up...

    120

    Of course, most Python programmers, when faced with a problem like this
    would turn to either PIL (Python Imaging Library) or Numpy (Numeric
    Python), but if all you need is quick-and-dirty, there you go.

    Have fun,
    Mike

    _______________________________________
    Mike C. Fletcher
    Designer, VR Plumber, Coder
    http://members.rogers.com/mcfletch/
    Mike C. Fletcher, May 4, 2004
    #2
    1. Advertising

  3. W. Watson

    W. Watson Guest

    Mike C. Fletcher wrote:

    > W. Watson wrote:
    >
    >> I have a 640x480 b/w bmp image file that can be converted to a dat
    >> file. I would like to convert the value of each pixel that is below
    >> say 120 units to exactly 40 units.

    >
    >
    > ...
    >
    >> python -c "open('mask.dat','w').write(chr(40)*640*480)"

    >
    >
    > Well, if you're going to take this as the baseline, then we can do some
    > pretty darn simple stuff indeed:
    >
    > >>> import string
    > >>> def createMask( threshold=120, target=40 ):

    > ... """Create 256-char mapping of destination characters"""
    > ... return string.maketrans( "".join( [chr(x) for x in
    > range(threshold)]), chr(target) * threshold )
    > ...
    > >>> data = open( "p:\\drawcurve.py", 'rb' ).read() # mode 'rb' is

    > important! use 'wb' to write
    > >>> mask = createMask()
    > >>> data

    > "'''Test of the glVertex function\r\n\r\nDrawing TT glyphs as Cubic
    > splines...\r\n\thttp://support.microsoft.com/support/kb/articles/q243/2/85.asp\r\n\r\n'''\r\nfrom
    > OpenGLContext import testingcontext\r\nBaseContext"
    > >>> data.translate( mask )

    > '((((((((((((((((((((((x((((((((((((((((((((((((((y(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((x(((((((((((((((((((((x((((((((((((x('
    >
    > >>> ord('x') # just to see why x and y show up...

    > 120
    >
    > Of course, most Python programmers, when faced with a problem like this
    > would turn to either PIL (Python Imaging Library) or Numpy (Numeric
    > Python), but if all you need is quick-and-dirty, there you go.
    >
    > Have fun,
    > Mike

    Having only seen the example snipet I gave as an example of python coding, what you
    wrote comes as a real surprise. This reminds me of a programming language called APL.
    It really had odd syntax. What are >>>, ..., and all those {{{{{? Not to mention the
    120 at the end. The snipet suggests that a python program can be run as a command
    line (python -c) like Perl. I would have thought a multi-line program to be executed
    would look something like
    python -c {line 1, line 2, line 3, etc.} It looks like I need to look at some
    python examples to get a feel for what this is about.
    W. Watson, May 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Zsolt Nagy
    Replies:
    1
    Views:
    1,287
    Kartic
    Jan 26, 2005
  2. Daniel Mark
    Replies:
    1
    Views:
    717
    Fredrik Lundh
    Sep 25, 2006
  3. shrutisunder
    Replies:
    0
    Views:
    476
    shrutisunder
    Mar 11, 2009
  4. Replies:
    3
    Views:
    161
    Michael Winter
    Nov 8, 2004
  5. dan_matthew

    Setting form values with external .js file

    dan_matthew, Sep 30, 2005, in forum: Javascript
    Replies:
    2
    Views:
    185
    dan_matthew
    Sep 30, 2005
Loading...

Share This Page