Re: File read and writing in binary mode...

2006-06-17 Thread hdante
 Hi,

 I'm sorry, but you have a conceptual error there. Text files differ
from binary files because they are not considered raw. When you say
that this or that file is a text file, python and/or the operating
system takes the liberty to insert and or remove semantics from the
file, according to some formatting agreed/imposed by them. Text files
are formatted files. Binary files are raw files. You can't expect by
any means that python will respect your raw data when writing text
files.

 The solution to the question "how can I write a binary file into a
text file" requires that you convert the binary file to a format
suitable for textual access. For example, you can "uuencode" the binary
file inside your text file. In simple terms:

 mytext = serialize(binary_file.read())
 text_file.write(mytext)
 ...
 mydata = deserialize(text_file.read())

 The functions "serialize" and "deserialize" are responsible for
converting the binary data to/from some textual representation.

 HOWEVER, why would you want to put binary data into a text file ? Is
this some information that will be used by your application ? Or will
you transfer it to some other person in a portable way ? Maybe you
should leave those files alone and not try to merge them. If it is a
complex structure you should put it into a database instead of doing
those strange things. In the worst case, you could just write a text
file, write a binary file and concatenate them later. See if this
really is a requirement for your project.



[EMAIL PROTECTED] wrote:
> Hi,
>
> I'm trying to open a file (any file) in binary mode and save it inside
> a new text file.
> After that I want to read the source from the text file and save it
> back to the disk with its original form. The problem is tha the binary
> source that I extract from the text file seems to be diferent from the
> source I saved. Here is my code:
> 1)
> handle=file('image.gif','rb')
> source=handle.read()
> handle.close()
>
> if I save the file directly everything is well :
> 2A)
> handle=file('imageDuplicated.gif','wb')
> handle.write(source)
> handle.close()
>
> the file imageDuplicated.gif will be exactly the same as the original
> image.gif.
> But if I save the source to a text file I have porblem :
> 2B)
> handle=file('text.txt','w')
> handle.write(source)
> handle.close()
>
> handle=file('text.txt','r')
> source2=handle.read()
> handle.close()
>
> handle=file('imageDuplicated.gif','wb')
> handle.write(source2)
> handle.close()
>
> the files are completly different and I even cant display the image
> from the imageDuplicated.gif .
>
> something changes when I save the source in the text file because in
> 2B) source == source2 returns a False .
> I suspect that maybe the encoding is making a conflict but I don't know
> how to manipulate it...
> Every help is welcome, thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple script to make .png thumbnails from .zip archive...

2006-06-18 Thread hdante
 Hi,

 I don't know zipfile by heart, but python official documentation is
always good ( docs.python.org ). You need a loop in the file list like
this:

  for file in zip:
process(file)

 Unfortunatelly, there are too many ways to create a thumbnail from an
image. I'll cite one, using the "python image" external module, that
I've found to be very easy:

import Image
def process(file):
  try:
image = Image.open(file)
image.thumbnail ((128,128), Image.ANTIALIAS)
image.save (file + '.thumb.png')
  except:
print 'Skipping file', file

 Links:
 http://docs.python.org/lib/lib.html - Python Library Reference
 http://www.pythonware.com/library/pil/handbook/image.htm - The Image
Module

K P S wrote:
> Hi.
>   I'm looking for a small script that will take a .zip archive and pull
> the first .jpg from the archive and convert it to a .png.
>
> The reason for this is I want to have tuhmbnails for these archives in
> nautilus under gnome.  I would like something similar to the following
> code, which will pull a thumbnail from an openoffice.org (oasis)
> document.  What I want is a little more involved, I guess, since I
> don't know the name of the file (for the zip.read command), and I need
> to convert the file from .jpg to .png once I get it.  Any help would be
> appreciated.  Including a pointer to a web page of a manual with
> examples. :-)
>
> #!/usr/bin/python
>
> import zipfile
> import sys
> import gnomevfs
>
> inURL=gnomevfs.get_local_path_from_uri(sys.argv[1])
> outURL=sys.argv[2]
>
> zip=zipfile.ZipFile(inURL,mode="r")
> picture=zip.read("Thumbnails/thumbnail.png")
> thumbnail=open(outURL,"w")
> thumbnail.write(picture)
> thumbnail.write("/n")
> zip.close()
> thumbnail.close()

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Passing data to system command

2006-06-18 Thread hdante
 Should be like this:

 from subprocess import Popen, PIPE

 my_output = file('output1.ps', 'w')
 p1 = Popen(["psxy"], stdin = PIPE, stdout=my_output)
 p1.stdin.write(my_format(array))
 p1.communicate()
 my_output.close()

 I've never used that, though, please tell us if it worked.

Chris Hieronymus wrote:
> Hi,
>
> I have a bunch of x-y data contained in an array.  I would like to
> plot the data using an
> external program (psxy in GMT).  The plotting program takes x-y
> couples as standard
> input.  How do I get the data into the system call?  I used to do
> things in csh and awk,
> i.e., something like
>
> awk '{; print $1, $2}' filename | psxy  options> >! output.ps
>
> The reason I'm trying to use python is because the manipulations are
> getting too cumbersome
> in awk.  Now I have all the manipulations done in python, but I'm
> missing that last step.
>
> I've tried various things with os.system, popen, and subprocess, but
> so far without success.
> Does anyone know how to do this?
>
> chris
>
>
> 
> ---
> Christoph
> Hieronymus
> [EMAIL PROTECTED]
> Associate
> Professor
> phone: (+46) 18-471 2383
> Uppsala
> University
>fax:   (+46) 18-501   110
> Dept. of Earth Sciences (Geophysics)
> Villavägen 16
> SE-752 36 Uppsala,  Sweden

-- 
http://mail.python.org/mailman/listinfo/python-list