PyObjCを使用して画像にテキストをオーバーレイしようとしましたが、私の質問"Annotate images using tools built into OS X"に答えています。私はそれを実行すると、私はこれを取得PyObjCのNSImageでテキストを描画する際にエラーが発生しました。
#!/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)
:
drawAtPointのためのドキュメントで探し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
:withAttributes:CocoaMagic、RMagick用RubyObjCの交換を参照して、私はこれを作ってみましたNSViewに焦点が当てられている場合にのみ、このメソッドを呼び出すようにしてください。 NSImageはNSViewのサブクラスではありませんが、これはうまくいくと思いますが、Rubyの例では非常に似たようなことがあるようです。
この作品を制作するには、何を変更する必要がありますか?
コードを忠実に改行し、line for lineをObjective-C Foundationツールに書き換えました。問題なく動作します。 [そうする理由がある場合、私はここにいる場合を投稿させていただきます。]質問が続いなっ
、どのようん:
は[message_string drawAtPoint:point withAttributes:text_attributes];
は
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)
マイナータイプミス:ここ