我試圖覆蓋使用PyObjC一些文本的圖像NSImage中繪製文本,同時努力回答我的問題,"Annotate images using tools built into OS X"。通過引用CocoaMagic,一個RubyObjC更換爲RMagick,我想出了這一點:錯誤PyObjC
#!/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)
當我運行它,我得到這個:
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
尋找在文檔的drawAtPoint:withAttributes:它說,「你應該只在NSView有焦點時調用這個方法。」 NSImage不是NSView的子類,但我希望這可以工作,並且在Ruby示例中似乎非常類似。
我需要做些什麼才能完成這項工作?
我重寫了代碼,將它轉換爲行,換行爲Objective-C基礎工具。它工作,沒有問題。 [我會很高興在這裏後,如果如果有一個這樣做的原因。]
的問題就變成了,如何:
[message_string drawAtPoint:point withAttributes:text_attributes];
從
message_string.drawAtPoint_withAttributes_(point, text_attributes)
有什麼不同?有沒有辦法告訴哪個「OC_PythonObject」引發NSInvalidArgumentException?