wie benutzt man einen binary string in system commands?

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
drtebi
User
Beiträge: 7
Registriert: Donnerstag 10. April 2003, 17:09
Wohnort: Madrid

Hallo,
ich versuche einen binary string in einem command zu benutzen. Die script ist ungefaehr so:

Code: Alles auswählen

import commands
import Image

# open the image
img = Image.open('1883.jpg')

# create thumbnail
img.thumbnail((128,128))

# now I create a raw string for the image data
thumb = img.tostring()

# now I want to use the raw string in a command.
# this could be any command, in my case I want
# to use setfattr, which sets an extended attribute
# on an XFS file system (I know this works from the
# command line, so this cannot be my problem...)
commands.getoutput('setfattr -n user.some -v ' + thumb + ' "1883.jpg"')

So, whatever I have tried, I seem to have a problem to get the binary string to work in the last function call above.

Any suggestions, please???
Voges
User
Beiträge: 564
Registriert: Dienstag 6. August 2002, 14:52
Wohnort: Region Hannover

Hi!
drtebi hat geschrieben: commands.getoutput('setfattr -n user.some -v ' + thumb + ' "1883.jpg"')
Was gibt denn commands.getoutput() zurück?
Jan
Milan
User
Beiträge: 1078
Registriert: Mittwoch 16. Oktober 2002, 20:52

Hi.

Sorry, but I cannot help you very much, I just don't know what your command do. Maybee you succed without using this module if you try to use os.popen /os.popen2 /os.popen3 /os.popen4.
This is nearly the same way to execute a command. Hope I could help you.

@Voges:
getstatusoutput(cmd)
Execute the string cmd in a shell with os.popen() and return a 2-tuple (status, output). cmd is actually run as { cmd ; } 2>&1, so that the returned output will contain output or error messages. A trailing newline is stripped from the output. The exit status for the command can be interpreted according to the rules for the C function wait().

getoutput(cmd)
Like getstatusoutput(), except the exit status is ignored and the return value is a string containing the command's output.
drtebi
User
Beiträge: 7
Registriert: Donnerstag 10. April 2003, 17:09
Wohnort: Madrid

was die commands.getoutput function hergibt ist einfach nur ein mix von zeichen... also irgendwas geht da shief, und es wird dann die binary data von dem thumb geprinted.

Also wie gesagt, es ist glaube ich wirklich egal ob welche funktion ich verwende um den system call zu machen, das problem ist nur dass ich die binary data irgendwie escapen muss oderso, damit es nicht zu errors kommt.
Gibt es nicht so eine funtion und einen string zu escapen fuer binary data?

man dankt...
Voges
User
Beiträge: 564
Registriert: Dienstag 6. August 2002, 14:52
Wohnort: Region Hannover

Milan hat geschrieben:@Voges:
Danke. Ich weiß, was commands.getoutput() macht ;-). Ich wollte nur wissen, was die Funktion in diesem konkreten Falle zurückgibt. Müsste ja eine, vielleicht aufschlussreiche Fehlermeldung sein.
Jan
Voges
User
Beiträge: 564
Registriert: Dienstag 6. August 2002, 14:52
Wohnort: Region Hannover

drtebi hat geschrieben:Gibt es nicht so eine funtion und einen string zu escapen fuer binary data?
Ist die Frage, ob dann setfattr() das dann noch versteht.

Ich bekomme beim Versuch, Deinen Code auszuführen, eine Exception, weil die Daten Nullwerte enthalten (nicht verwunderlich). Du schreibst, dass es direkt in der Shell, funktioniert. Kannst Du da mal ein Beispiel geben? Wie gibst Du dort nicht darstellbare Werte an?
Jan
drtebi
User
Beiträge: 7
Registriert: Donnerstag 10. April 2003, 17:09
Wohnort: Madrid

OK, es war etwas falsch beschrieben...
Ich habe hier zunaechst den thumbnail erzeugt, was sehr leicht auch mit python's Image library zu machen ist, dann mache ich das im shell mit einem redirect, und garnicht mit setfattr, sondern mit attr:

Code: Alles auswählen

attr -q -s thumbnail 1883.jpg < 1883t.jpg
Ich werde mal versuchen die python script mit dieser command line umzuschreiben, mal sehen ob es klappt ;)
Benutzeravatar
strogon14
User
Beiträge: 58
Registriert: Sonntag 23. Februar 2003, 19:34
Wohnort: Köln
Kontaktdaten:

Dann machst Du es am besten so:

chout, chin, cherr = popen2.popen3('attr -q -s thumbnail 1883.jpg')
chin.write(thumb)
chin.close()

Dann brauchst Du 'thumb' nicht vorher in eine temporäre Datei schreiben um es dannn in den Shell-Befehl zu pipen, sondern schreibst es in das File-Obkekt, das popen3() zurück gibt.

HTH, Chris
drtebi
User
Beiträge: 7
Registriert: Donnerstag 10. April 2003, 17:09
Wohnort: Madrid

Vielen Dank, das war auf jeden Fall schon mal eine guter Tip.

Es scheint jedoch etwas anderes nicht so ganz zu klappen, und zwar muss da irgendwas mit der tostring() function aus dem Image module nicht klappen, bzw, habe ich falsch verstanden.

Um das etwas deutlicher zu machen sind hier zwei Beispiele.

Das erste funktioniert nicht:

Code: Alles auswählen

import Image

img = Image.open('1882.jpg')
img.thumbnail((128,128))

thumb = img.tostring()

print """Content-type: image/jpeg\r\n"""
print thumb
Das zweite funktioniert:

Code: Alles auswählen

import Image
import sys

img = Image.open('1882.jpg')
img.thumbnail((128,128))

print """Content-type: image/jpeg\r\n"""
img.save(sys.stdout, "JPEG")
Dabei heisst es in der Python Image Library Reference (PIL):

tostring

tostring()

Returns a string containing pixel data, using the standard "raw" encoder.

(http://www.pythonware.com/library/pil/h ... /image.htm)

Eigenartig, was verstehe ich falsch?
Benutzeravatar
strogon14
User
Beiträge: 58
Registriert: Sonntag 23. Februar 2003, 19:34
Wohnort: Köln
Kontaktdaten:

drtebi hat geschrieben:
tostring()

Returns a string containing pixel data, using the standard "raw" encoder.

(http://www.pythonware.com/library/pil/h ... /image.htm)

Eigenartig, was verstehe ich falsch?
Ganz, einfach: das "raw" encoding ist kein JPEG-Format!

Du kannst also nicht den MIME-Typ image/jpeg angeben und dann ein anderes Format ausgeben.

Die save() Methode des Image-Objekts schreibt dagegen das Bild in der angegebenen Codierung in das angegebene Datei-Objekt.
drtebi
User
Beiträge: 7
Registriert: Donnerstag 10. April 2003, 17:09
Wohnort: Madrid

Ja,
ich hatte mir schon gedacht dass da irgendwas mit dem Format nicht richtig sein konnte...
Wie auch immer, ich habe jetzt eine Lösung gefunden, die folgende script macht aus dem original image "princesa.jpg" einen thumbnail, und speichert diesen als extended attribute im original image:

Code: Alles auswählen

import Image
from popen2 import popen2
import StringIO

original = 'princesa.jpg'

img = Image.open(original)
img.thumbnail((200,200))

file = StringIO.StringIO()
img.save(file, "JPEG")
thumb = file.getvalue()

# this now works ;)
pout, pin = popen2('attr -q -s thumbnail ' + original)
pin.write(thumb)
pin.close()
Vielen Dank für all Eure Hilfe!
Antworten