Cocoa 在PyObjC中的NSImage上绘制文本时出错

Cocoa 在PyObjC中的NSImage上绘制文本时出错,cocoa,image,annotations,pyobjc,ruby-cocoa,Cocoa,Image,Annotations,Pyobjc,Ruby Cocoa,我试图用PyObjC将一个图像与一些文本叠加在一起,同时努力回答我的问题。通过引用RubyObjC的替代品,我得出以下结论: #!/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" fo

我试图用PyObjC将一个图像与一些文本叠加在一起,同时努力回答我的问题。通过引用RubyObjC的替代品,我得出以下结论:

#!/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
不同于

message_string.drawAtPoint_withAttributes_(point, text_attributes)

??有没有办法判断哪个“OC_PythonObject”正在引发NSInvalidArgumentException?

以上代码中有以下问题:

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)
确实有一些小的打字错误

请注意,代码的中间部分可以替换为更可读的变体:

# 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)

我通过查看到的源代码、and的12行,特别是save和_getImageData方法了解到了这一点。

以上代码中存在以下问题:

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)
确实有一些小的打字错误

请注意,代码的中间部分可以替换为更可读的变体:

# 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)
我通过查看到的源代码了解到这一点,包括and的12行代码,特别是save和_getImageData方法