Error drawing text on NSImage in PyObjC - cocoa

I'm trying to overlay an image with some text using PyObjC, while striving to answer my question, "Annotate images using tools built into OS X". By referencing CocoaMagic, a RubyObjC replacement for RMagick, I've come up with this:
#!/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)
When I run it, I get this:
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
Looking in the docs for drawAtPoint:withAttributes:, it says, "You should only invoke this method when an NSView has focus." NSImage is not a subclass of NSView, but I would hope this would work, and something very similar seems to work in the Ruby example.
What do I need to change to make this work?
I rewrote the code, converting it faithfully, line for line, into an Objective-C Foundation tool. It works, without problems. [I would be happy to post if here if there is a reason to do so.]
The question then becomes, how does:
[message_string drawAtPoint:point withAttributes:text_attributes];
differ from
message_string.drawAtPoint_withAttributes_(point, text_attributes)
? Is there a way to tell which "OC_PythonObject" is raising the NSInvalidArgumentException?

Here are the problems in the above 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 typos indeed.
Note that the middle portion of the code can be replaced with this more-readable variant:
# 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)
I learned this by looking at the source code to NodeBox, the twelve lines of psyphography.py and cocoa.py, particularly the save and _getImageData methods.

Related

Python3 tkinter: showing images from the Internet on a label (urllib) does not work

I would like to show Images from the Internet in a Label. I can do it with Images from the harddisk. Then I looked for a way to do it with Photos from the Internet and I wrote the following code:
self.res = requests.get('https://dog.ceo/api/breeds/image/random')
print(self.res)
self.jason = json.loads(self.res.content)
print (self.jason)
bild = self.jason['message']
print (bild)
u = urllib.request.urlopen(bild)
raw_data = u.read()
u.close()
b64_data = base64.encodestring(raw_data)
image = tk.PhotoImage(data = b64_data)
self.pic_label["image"] = image
But I get the following error message:
*Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/home/wolfgang/Eigene Programme/python_buch/api.py", line 42, in api
image = tk.PhotoImage(data = b64_data)
File "/usr/lib/python3.6/tkinter/__init__.py", line 3545, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/usr/lib/python3.6/tkinter/__init__.py", line 3501, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize image data*
What am I doing wrong? Thanks in advance
P.S.: I am using Linux, if that should matter.
Update:
In the meanwhile I updated my function. But it still doesn't show images. Does it maybe have to do with the api? Any ideas what I have to change?
def api(self):
self.res = requests.get('https://dog.ceo/api/breeds/image/random')
print(self.res)
self.jason = json.loads(self.res.content)
print (self.jason)
bild = self.jason['message']
image_bytes = urllib.request.urlopen(bild).read()
data_stream = io.BytesIO(image_bytes)
pil_image = Image.open(data_stream)
tk_image = ImageTk.PhotoImage(pil_image)
self.anzeige = tk.Label(self.root,image=tk_image)
self.anzeige.pack()
Thanks in advance
When I ran your code it returned a jpg image. The tkinter.PhotoImage class doesn't directly support jpg. You'll need to use PIL/Pillow to create the image and hand it off to tkinter, or use some other library to convert it to png or gif before creating a PhotoImage.
For how to display jpeg images in tkinter see How do I insert a JPEG image into a python Tkinter window?

My toplevel window in tkinter is no longer being destroyed. It was working fine until I tried changing other aspects of my function

I'm trying to get a popup window to display random text and a picture every time a button is pressed in tkinter. My original code was going to use an if/elif statement to do this. It worked as intended but I thought it might be easier to pair the data in a dictionary since there would be 50 elif statements otherwise (is it frowned upon to use so many? I actually found it easier to read).I was able to get this working but now the toplevel window in tkinter is not being destroyed like it was in the original function. A new Label is just being created on top of it and I can't figure out why. The function code is below. Thanks in advance, any help would be appreciated!
def Add_Gemstone2():
global Addstone
#destroy the previous window if there is one.
try:
AddStone.destroy()
except(AttributeError, NameError):
pass
#create the window.
AddStone=Toplevel()
AddStone.configure(bg='White', height=200, width=325)
AddStone.geometry('325x180+10+100')
# add gemstones to list from file.
gem_stones = open('gemstones.txt')
all_gem_stones = gem_stones.readlines()
gemstones = []
for i in all_gem_stones:
gemstones.append(i.rstrip())
# Add pictures to list.
path = r'C:\Users\Slack\Desktop\PYTHON WORKS\PYTHON GUI PROJECT\gems'
gempictures = []
# r=root, d=directories, f = files
for r,d,f in os.walk(path):
for file in f:
if '.gif' in file:
gempictures.append(os.path.join(r, file))
#create dictionary from lists.
gemdiction = dict(zip(gemstones, gempictures))
key, val = random.choice(list(gemdiction.items()))
# create the labels.
glbl1 = Label(AddStone, text=key, bg='gold', wraplength=300)
glbl1.pack()
image = ImageTk.PhotoImage(Image.open(val))
glbl2 = Label(AddStone, image=image)
glbl2.image = image
glbl2.pack()

Link a tkinter button to seperate script

I have a tkinter interface with a few entry widgets as inputs. Upon clicking a button I would like those inputs to be sent to a separate script to be processed and a value printed and potentially returned back to the button (I am looking at this for a dual accuracy assessment statistic)
This is a lower scale example of what I have so far and am looking to accomplish
Example Secondary Script: GUI_ConnectorScript
def calculate():
global result
result = int(entry.get())
result += 1
print result
Primary Script: GUI_ConnectorScript
from Tkinter import *
import GUI_ConnectorScript
background = "#A8A8A8"
master = Tk()
screen_width = master.winfo_screenwidth()
screen_height = master.winfo_screenheight()
width = int(screen_width*0.7)
height = int(screen_height*0.7)
size = "%sx%s"%(width,height)
master.geometry(size)
master.title("GIS Display")
text = Text(master, width = 80, height = 40, background = background)
text.pack(expand = TRUE, fill = BOTH)
entry = Entry(master, width=5).place(x=100,y=100)
button = Button(master, text="Calculate", command=GUI_ConnectorScript).place(x=500,y=500)
mainloop()
I have been trying to figure this out for awhile and have look around a lot for an answer. I have found examples similar but I am having an issue getting it to work for my application.
I agree with Parviz, whenever GUI programs get too complicated you should use Object-Oriented Programming.
I can further advice that you use kivy (if possible) instead of tkinter, its much better for bigger projects

kivy: possible to use buffer as image source?

I've got code along the lines of the following which generates a new image out of some existing images.
from PIL import Image as pyImage
def create_compound_image(back_image_path, fore_image_path, fore_x_position):
back_image_size = get_image_size(back_image_path)
fore_image_size = get_image_size(fore_image_path)
new_image_width = (fore_image_size[0] / 2) + back_image_size[0]
new_image_height = fore_image_size[1] + back_image_size[1]
new_image = create_new_image_canvas(new_image_width, new_image_height)
back_image = pyImage.open(back_image_path)
fore_image = pyImage.open(fore_image_path)
new_image.paste(back_image, (0, 0), mask = None)
new_image.paste(fore_image, (fore_x_position, back_image_size[1]), mask = None)
return new_image
Later in the code, I've got something like this:
from kivy.uix.image import Image
img = Image(source = create_compound_image(...))
If I do the above, I get the message that Image.source only accepts string/unicode.
If I create a StringIO.StringIO() object from the new image, and try to use that as the source, the error message is the same as above. If I use the output of the StringIO object's getvalue() method as the source, the message is that the source must be encoded string without NULL bytes, not str.
What is the proper way to use the output of the create_compound_image() function as the source when creating a kivy Image object?
It seems you want to just combine two images into one, you can actually just create a texture using Texture.create and blit the data to a particular pos using Texture.blit_buffer .
from kivy.core.image import Image
from kivy.graphics import Texture
bkimg = Image(bk_img_path)
frimg = Image(fr_img_path)
new_size = ((frimg.texture.size[0]/2) + bkimg.texture.size[0],
frimg.texture.size[1] + bkimg.texture.size[1])
tex = Texture.create(size=new_size)
tex.blit_buffer(pbuffer=bkimg.texture.pixels, pos=(0, 0), size=bkimg.texture.size)
tex.blit_buffer(pbuffer=frimg.texture.pixels, pos=(fore_x_position, bkimg.texture.size[1]), size=frimg.texture.size)
Now you can use this texture anywhere directly like::
from kivy.uix.image import Image
image = Image()
image.texture = tex
source is a StringProperty and is expecting a path to file. That's why you got errors when you tried to pass PIL.Image object, StringIO object or string representation of image. It's not what framework wants. As for getting image from StringIO, it was discussed before here:
https://groups.google.com/forum/#!topic/kivy-users/l-3FJ2mA3qI
https://github.com/kivy/kivy/issues/684
You can also try much simpler, quick and dirty method - just save your image as a tmp file and read it normal way.

PyGTK FileChooserDialog Keep Getting Errors

Now I made WidgetArea originally for Windows, but being primarily a Linux user. I wanted to make it for Linux as well, but mainly to learn more about the file dialog in PyGTK. So I took a look at this tutorial to have a better understanding of it, while working on this simple, yet small application, as that's easier for me to learn, and understand by experimentation.
So here's my source code.
#!/usr/bin/env python
import sys, os
import pygtk, gtk, gobject
import pygst
pygst.require("0.10")
import gst
class WidgetArea(gtk.Window):
def addwidget(self, w):
self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.win.set_title("Widget")
self.win.set_decorated(False)
self.win.set_has_frame(False)
self.win.set_resizable(False)
self.win.set_keep_above(True)
self.win.set_property('skip-taskbar-hint', True)
self.previewimage = gtk.Image()
self.win.add(self.previewimage)
self.win.show_all()
def pinning(self, checkbox):
if checkbox.get_active():
self.set_keep_above(True)
else:
self.set_keep_above(False)
def change_size(self, w):
width = int(self.entryw.get_text())
height = int(self.entryh.get_text())
self.win.set_size_request(width,height)
def __init__(self):
super(WidgetArea, self).__init__()
self.set_position(gtk.WIN_POS_CENTER)
self.set_title("WidgetArea")
self.set_resizable(False)
self.set_keep_above(True)
self.set_property('skip-taskbar-hint', True)
self.connect("destroy", gtk.main_quit, "WM destroy")
vbox = gtk.VBox(spacing=0)
hbox = gtk.HBox(spacing=0)
hbox2 = gtk.HBox(spacing=0)
hbox3 = gtk.HBox(spacing=0)
hbox4 = gtk.HBox(spacing=0)
self.widgetsize = gtk.Label("Widget Size:")
self.widgetsize.set_size_request(100, 30)
self.entryw = gtk.Entry()
self.entryh = gtk.Entry()
self.entryw.set_text("270")
self.entryw.set_size_request(75, 30)
labelcoma = gtk.Label(",")
labelcoma.set_size_request(10, 30)
self.entryh.set_text("221")
self.entryh.set_size_request(75, 30)
labelspac1 = gtk.Label(" ")
labelspac1.set_size_request(10, 30)
hbox.pack_start(self.widgetsize)
hbox.pack_start(self.entryw)
hbox.pack_start(labelcoma)
hbox.pack_start(self.entryh)
hbox.pack_start(labelspac1, 0, 0, 10)
check = gtk.CheckButton("Pin This Window")
check.set_active(True)
check.connect("clicked", self.pinning)
hbox.pack_start(check, 0, 0, 10)
labelspac2 = gtk.Label(" ")
labelspac2.set_size_request(250, 15)
hbox2.pack_start(labelspac2)
filefilter = gtk.FileFilter()
filefilter.set_name("Images")
filefilter.add_mime_type("image/png")
filefilter.add_mime_type("image/jpeg")
filefilter.add_mime_type("image/gif")
filefilter.add_mime_type("image/tiff")
filefilter.add_mime_type("image/svg+xml")
filefilter.add_pattern("*.jpg")
self.ref_file_button = gtk.FileChooserButton('Add Widget')
self.ref_file_button.set_current_folder("/".join([self.rootdir,"pics"]))
self.ref_file_button.set_filter(filefilter)
self.ref_file_button.connect("file-set", self.on_open_clicked)
hbox3.pack_start(self.ref_file_button, 150, 150, 10)
labelspac5 = gtk.Label(" ")
labelspac5.set_size_request(0, 10)
hbox4.pack_start(labelspac5)
vbox.pack_start(hbox)
vbox.pack_start(hbox2)
vbox.pack_start(hbox3)
vbox.pack_start(hbox4)
self.add(vbox)
self.show_all()
def on_open_clicked(self, widget, data=None):
ref_image_path = widget.get_filename()
self.previewimage.set_from_file(ref_image_path)
self.addwidg.connect("clicked", self.addwidget)
self.addwidg.connect("clicked", self.change_size)
ref_image_path.destroy()
WidgetArea()
gtk.gdk.threads_init()
gtk.main()
I removed the following code (1st), due to the following error (2nd).
self.ref_file_button.set_current_folder("/".join([self.rootdir,"pics"]))
Traceback (most recent call last):
File "./widgetarea.py", line 109, in <module>
WidgetArea()
File "./widgetarea.py", line 86, in __init__
self.ref_file_button.set_current_folder("/".join([self.rootdir,"pics"]))
AttributeError: 'WidgetArea' object has no attribute 'rootdir'
Now this isn't a big deal at this point. My main goal is to get the image displayed in a new window. So after I removed the code above, due to that error I got another one.
Traceback (most recent call last):
File "./widgetarea.py", line 103, in on_open_clicked
self.previewimage.set_from_file(ref_image_path)
AttributeError: 'WidgetArea' object has no attribute 'previewimage'
All I'm having problems with is when you browse to select an image I want the chosen image, when pressed OK to launch as a new window displaying the chosen image in that window, as stated above.
To correct the first error, use gtk.FILE_CHOOSER_ACTION_OPEN instead of gtk.FileChooserAction.OPEN.
The second problem is because there is no variable named image at that point in your code (line 116). Perhaps you are coming from a C++ or Java background, where a name like image can be resolved by looking at the attributes of the enclosing class, i.e. this.image?
In Python you can't do that. You have to assign explicitly to self.image in your addwidget() method. Otherwise the name image remains local to the addwidget() method and is not available outside of it.
This raises a different problem, what happens every time the button gets clicked and addwidget() is called? self.win and self.image are overwritten. That may be what you want, but I'm just calling it to your attention --- it seems a little odd to me.
I have used something like this in one of my projects. And it's working well for me in Linux.
def __init__(self):
# Define all the widgets
image_filter = gtk.FileFilter()
image_filter.set_name("Images")
image_filter.add_mime_type("image/png")
image_filter.add_mime_type("image/jpeg")
image_filter.add_mime_type("image/gif")
image_filter.add_mime_type("image/tiff")
image_filter.add_mime_type("image/svg+xml")
image_filter.add_pattern("*.jpg")
self.ref_file_button = gtk.FileChooserButton('Select Image')
self.ref_file_button.set_size_request(100,30)
self.ref_file_button.set_current_folder("/".join([self.rootdir,"pics"])) # set directory path
self.ref_file_button.set_filter(image_filter)
self.ref_file_button.set_tooltip_text('Select Image')
self.ref_file_button.connect("file-set", self.ref_image_selected)
def ref_image_selected(self,widget,data=None):
ref_image_path = widget.get_filename()
print ref_image_path
After getting the path of the image, you can load it using gtk.Image
EDIT:
Your code is a bit erroneous. You are never calling the function addwidget(), and hence self.previewimage is not defined. and so it gives AttributeError.
def __init__(self):
# your code
self.add(vbox)
self.addwidget(200) # I don't know what 'w' is. so I took a random number.
self.show_all()
def on_open_clicked(self, widget, data=None):
ref_image_path = widget.get_filename()
self.previewimage.set_from_file(ref_image_path)
self.addwidg.connect("clicked", self.addwidget)
self.addwidg.connect("clicked", self.change_size)
ref_image_path.destroy()
What is self.addwidg ?
And I am able to view the image now.

Resources