Image processing using ObjC
Hi all. Non-programmer here. I've been using pythonista to process photos of student work, to remove the background and shadows etc. It is working fairly well, however, it is incredibly slow if I use full resolution photos. I thought I might be able to speed things up by using ObjC and CoreImage Filters, however, I have no idea how to get started using ObjC within Pythonista, other than what is shown in the sample script Camera Scanner.py, which is significantly past my understanding.
Can anyone give me an idea on how to get started? I'm happy to experiment with different filters, just not sure how to get a filter actually working and have an image file outputted.
My original Pythonista script is very simple - just a series of filters (I use the workflow app to get the photos into Pythonista, then the cleaned up photos are PDFed so I can open them in Notability to annotate, to send back to my students (highschool Chemistry)).
Any help would be much appreciated!
from PIL import Image, ImageOps, ImageFilter, ImageChops import photos, clipboard, webbrowser imo=clipboard.get_image(idx=0) if not imo.mode == 'RGB': img=imo.convert('RGB') im1=img.filter(ImageFilter.MaxFilter(size=9)) im2=ImageChops.subtract(im1,img) im3=ImageOps.invert(im2) im4=im3.filter(ImageFilter.SHARPEN) im5=ImageOps.autocontrast(im4,cutoff=1) clipboard.set_image(im5,jpeg_quality=1.0) webbrowser.open('workflow://')
Take a look at this as it uses some CoreImage filters.
Thanks for that - trying to figure it out now - steep learning curve!
Hi plessner14. The photo editor script fails when I try and run it - 'no module named toolz' (line 5).
So I'm trying to do something like the following script:
import photos, console
from objc_util import *
CIFilter, CIImage, CIContext, CIDetector, CIVector = map(ObjCClass, ['CIFilter', 'CIImage', 'CIContext', 'CIDetector', 'CIVector'])
imo = photos.pick_image()
img = ObjCClass('CIGaussianBlur').imo
Unfortunately I'm obviously not getting the syntax correct, as it fails, and I can't find a simple example of what I want to do - just run a filter, with parameters I specify, on an image I pick from the photo album.
Can anyone help?
@bruceathome Sorry about that. toolz is a pure Python helper module that I used. You can download it with pip in Stash
@bruceathome CoreImage filters are certainly an interesting use case for ObjC bridging, but I understand that it can be a bit daunting to figure out how to get started. So I've made this little wrapper module that allows you to use CoreImage in a more “pythonic” manner, without needing to know very much about Objective-C or Cocoa.
It's meant to be used as a module, though you can also run it directly for a quick demo.
Save the script above as
core_image.pyin the same folder as your other script. Then try something like this:
import photos from core_image import CImage img = CImage(photos.pick_asset()) img2 = img.filter('CIGaussianBlur', radius=10) img2.show() # or: # img2.save_jpeg('output.jpg')
You can find a bunch of other examples, and details on how to set different kinds of filter parameters in the gist.
You'll also need Apple's Core Image Filter Reference to look up filter names and their different parameters. There are a lot.
@plessner - thanks for that - all up and running now.
@omz admin - that's pretty much exactly what I was after - very much appreciated.