Do a search by image from the camera roll using Pythonista3

Thing you want to do

View the results in Safari using Google's SearchByImage from the images saved in the iOS camera roll.

Realization

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.

Written

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

General flow

  1. Select an image and execute the script.
  2. Get the first file path from the received attachment.
  3. Check if it is an image, run search_by_image (), and if successful, the url of the search result will be returned.
  4. Open the returned url in Safari and exit.

Receiving images

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 don't know what it is, but I can't use webbrowser

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.

end

Next, I think it would be nice if SearchByImage could be used from the images that came to Twitter.

Recommended Posts

Do a search by image from the camera roll using Pythonista3
Try a similar search for Image Search using the Python SDK [Search]
Posture detection by openpose from USB camera image using python code
I tried to detect the iris from the camera image
[Python] Specify the range from the image by dragging the mouse
Get the image of "Suzu Hirose" by Google image search.
DJango Note: From the beginning (using a generic view)
[Python] Mask the image into a circle using Pillow
A little bit from Python using the Jenkins API
Create a dictionary by searching the table using sqlalchemy
Reuse the behavior of the @property method by using a descriptor [16/100]
Image collection by calling Bing Image Search API v5 from Python
Create a GCE instance from a GCR Docker image using terraform
Shoot time-lapse from a PC camera using Python and OpenCV
Let's search from the procession
Remove the frame from the image
The image is a slug
[Map display] Display a map from the address registered by the user using the Google Maps JavaScript API and Geocoding API!
Let's do it by dividing numpy without using the for statement
Try to edit a new image using the trained StyleGAN2 model
Create a pandas Dataflame by searching the DB table using sqlalchemy
Let's do it by multiplying numpy without using the for statement
I tried to cut out a still image from the video
Prepare a high-speed analysis environment by hitting mysql from the data analysis environment
Disclose the know-how that created a similar image search service for AV actresses by deep learning by chainer
Generate a Docker image using Fabric
Make a Santa classifier from a Santa image
Let's cut the face from the image
A Python script that automatically collects typical images using bing image search
[EV3 x Python] Stream the camera image to your PC using mjpg-streamer.
Prepare a URL to display the image uploaded by Active Storage by yourself
Search for synonyms from the word list (csv) using Python Japanese WordNet