Frage

Ich versuche, ein Bild mit etwas Text zu überlagern PyObjC verwenden, während das Streben meine Frage zu beantworten, " Anmerken Bilder Werkzeuge eingebaut in OS x " verwenden. Durch Bezugnahme auf CocoaMagic , ein RubyObjC Ersatz für RMagick , ich habe mit dieser kommen:

#!/usr/bin/env python

from AppKit import *

source_image = "/Library/Desktop Pictures/Nature/Aurora.jpg"
final_image = "/Library/Desktop Pictures/.loginwindow.jpg"
font_name = "Arial"
font_size = 76
message = "My Message Here"

app = NSApplication.sharedApplication()  # remove some warnings

# read in an image
image = NSImage.alloc().initWithContentsOfFile_(source_image)
image.lockFocus()

# prepare some text attributes
text_attributes = NSMutableDictionary.alloc().init()
font = NSFont.fontWithName_size_(font_name, font_size)
text_attributes.setObject_forKey_(font, NSFontAttributeName)
text_attributes.setObject_forKey_(NSColor.blackColor, NSForegroundColorAttributeName)

# output our message
message_string = NSString.stringWithString_(message)
size = message_string.sizeWithAttributes_(text_attributes)
point = NSMakePoint(400, 400)
message_string.drawAtPoint_withAttributes_(point, text_attributes)

# write the file
image.unlockFocus()
bits = NSBitmapImageRep.alloc().initWithData_(image.TIFFRepresentation)
data = bits.representationUsingType_properties_(NSJPGFileType, nil)
data.writeToFile_atomically_(final_image, false)

Wenn ich es laufen, bekomme ich diese:

Traceback (most recent call last):
  File "/Users/clinton/Work/Problems/TellAtAGlance/ObviouslyTouched.py", line 24, in <module>
    message_string.drawAtPoint_withAttributes_(point, text_attributes)
ValueError: NSInvalidArgumentException - Class OC_PythonObject: no such selector: set

in der Dokumentation der Suche nach drawAtPoint: withAttributes :, es sagt: „Sie sollten diese Methode nur aufrufen, wenn ein NSView Fokus hat.“ NSImage ist keine Unterklasse von NSView, aber ich würde hoffen, dass dies funktionieren würde, und etwas sehr ähnlich scheint in dem Ruby-Beispiel zu arbeiten.

Was muss ich ändern, um diese Arbeit zu machen?


schrieb ich den Code, es für die Linie treu, Linie Umwandlung in eine Objective-C-Stiftung Werkzeug. Es funktioniert ohne Probleme. [Ich würde mich freuen, wenn hier posten, wenn es einen Grund gibt, dies zu tun.]

Die Frage ist dann, wie funktioniert:

[message_string drawAtPoint:point withAttributes:text_attributes];

unterscheiden sich von

message_string.drawAtPoint_withAttributes_(point, text_attributes)

? Gibt es eine Möglichkeit zu sagen, welche „OC_PythonObject“ hebt die NSInvalidArgumentException?

War es hilfreich?

Lösung

Hier sind die Probleme in dem obigen Code:

text_attributes.setObject_forKey_(NSColor.blackColor, NSForegroundColorAttributeName)
->
text_attributes.setObject_forKey_(NSColor.blackColor(), NSForegroundColorAttributeName)

bits = NSBitmapImageRep.alloc().initWithData_(image.TIFFRepresentation)
data = bits.representationUsingType_properties_(NSJPGFileType, nil)
->
bits = NSBitmapImageRep.imageRepWithData_(image.TIFFRepresentation())
data = bits.representationUsingType_properties_(NSJPEGFileType, None)

Minor Tippfehler in der Tat.

Beachten Sie, dass der mittlere Teil des Codes kann mit dieser mehr lesbaren Variante ersetzt werden:

# prepare some text attributes
text_attributes = { 
    NSFontAttributeName : NSFont.fontWithName_size_(font_name, font_size),
    NSForegroundColorAttributeName : NSColor.blackColor() 
}

# output our message 
NSString.drawAtPoint_withAttributes_(message, (400, 400), text_attributes)

Das erfuhr ich durch den Quellcode schauen, um NodeBox , die zwölf Zeilen von psyphography.py und cocoa.py , die besonders speichern und _getImageData Methoden.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top