Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Setting an Image File Values

Reply
Thread Tools

Setting an Image File Values

 
 
W. Watson
Guest
Posts: n/a
 
      05-04-2004
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>
 
Reply With Quote
 
 
 
 
Mike C. Fletcher
Guest
Posts: n/a
 
      05-04-2004
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/



 
Reply With Quote
 
 
 
 
W. Watson
Guest
Posts: n/a
 
      05-04-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read 10 values at a time from a text file out of 50 values in VHDL shrutisunder VHDL 0 03-11-2009 06:10 AM
How to save High Quality JPG image by setting Image module from PIL Daniel Mark Python 1 09-25-2006 05:59 PM
Setting form values with external .js file dan_matthew Javascript 2 09-30-2005 08:56 PM
wx.Image: Couldn't add an image to the image list. Laszlo Zsolt Nagy Python 1 01-26-2005 09:55 PM
setting form values via external js file ... trying to use document.form.filed.value jason@cyberpine.com Javascript 3 11-08-2004 02:05 PM



Advertisments