View the results in Safari using Google's SearchByImage from the images saved in the iOS camera roll.
Pythonista3 has a function that you can call your own Python script from the shared sheet of the application, so use this. For more information about Pythonista 3, see this article by Hitoburogu (Talking about the appeal of the revolutionary manufacturing environment "Pythonista 3" that runs on iOS). It has been. Pythonista3 can use Python2.7 and Python3.5, but this time I wrote it in Python3.5.
searchbyimage.py
import requests, appex, imghdr
from requests_toolbelt.multipart.encoder import MultipartEncoder
from objc_util import *
def search_by_image(fp):
user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.98 Safari/537.36 Vivaldi/1.6.689.40'
m = MultipartEncoder(fields={'encoded_image': (fp.name, fp)})
r = requests.post('http://www.google.co.jp/searchbyimage/upload',
data=m,
headers={'Content-Type': m.content_type, 'User-Agent': user_agent})
if r.status_code == 200:
return r.url
else:
return 0
def main():
image = appex.get_attachments()[0]
if imghdr.what(image):
url = search_by_image(open(image, 'rb'))
if url:
UIApplication.sharedApplication()._openURL_(nsurl(url))
if appex.is_running_extension():
main()
search_by_image ()
, and if successful, the url of the search result will be returned.Pythonista3 has a module called appex that can use the data received in the shared sheet and so on, and this will get the image file of the camera roll.
If true, I thought I could get the path to the image with ʻappex.get_file_path (), but I couldn't get it, so ʻAppex.get_attachments ()
is used to extract the path from the first attachment.
MultipartEncoder I have to use multipart / post to upload images, which I'm not sure. Apparently, there is a useful package called requests_toolbelt. However, early Pythonista 3 did not have requests_toolbelt. StaSh plays an active role there. Now that you can use pip, I installed it from here.
I do webbrowser.open ()
to open the url, but apparently it doesn't work from sharing.
First, enable the bridge function to Objective-C with from objc_util import *
.
Then, you can open the url in Safari by calling ʻUIApplication.sharedApplication () .openURL (nsurl (url)) `.
I don't understand the mechanism of this area, but by using this, it is possible to open a different application by using Safari or url scheme from the shared sheet.
Next, I think it would be nice if SearchByImage could be used from the images that came to Twitter.
Recommended Posts