Image Upload Functionlity in wit.ai, without using facebook messenger - python

I am developing a chat-bot using wit.ai and my own UI instead of facebook messenger. I am using python to implement actions. This post and this post gives some insights about how this can be done in facebook messenger. However I want to have image upload and display functionality in my own UI, which uses wit.ai. How can this be done?
My current code can extract an intent named upload and call uploadImage() function. What should be there in uploadImage() function that can upload an image and even display in Chat UI?

The following works for a general Python program. I am not sure it is the proper way to do it with wit.ai.
If you want to do image processing on the image I recommend the OpenCV library. Using that and the easygui library you can prompt the user for an image, read it, and display it. The following code shows how do to it. The dialog box defaults to the folder "c:\" and has filters for png and jpg files. You will need to figure out how to display the image in your UI.
import numpy as np
import cv2
import easygui
# Prompt the user to open a file.
file_path = easygui.fileopenbox(msg='Locate an image file',
filetypes=["*.png", "*.jpg"],
title='Specify the image file to upload',
default='c:\*.png')
# Load an image
img = cv2.imread(file_path)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Related

How do I take pictures from Pythonista and them export them into a specific album already on my iOS device

I already have the body of my code in which I can create an album and then take a picture directly from Pythonista. After that, I would want to take this recently took picture and transfer it into the album that I just created. This is what I have:
import photos
import console
console.clear()
nom_album = contacts
photos.create_album(nom_album)
img=photos.capture_image()
The photos.create_album method returns an Asset-Collection object. Asset-Collections have a method, add_assets, that take a list of “assets”, that is, photos. Specifically, as I read it, an Asset is a photo that is already in the iOS device’s photo library. To add a photo to an album, the photo must already be in the device’s photo library.
The capture_image method does not return Asset objects, however. That is, it does not automatically add the new photo to the device’s photo library. You can verify this in your own code: the images you take using that code should not be in your device’s “recents” album.
Instead, capture_image returns a PIL image. I do not see any way to add a PIL image to the device’s photo library directly. What I was able to do was save the PIL image locally and then convert the saved file into an Asset. That is, (1) add the saved file to the device’s photo library, using create_image_asset; then (2) that Asset can then be added to an Asset-Collection.
Here‘s an example:
import photos
#a test album in the device’s library
#note that multiple runs of this script will create multiple albums with this name
testAlbum = 'Pythonista Test'
#the filename to save the test photograph to in Pythonista
testFile = testAlbum + '.jpg'
#create an album in the device’s photo library
newAlbum = photos.create_album(testAlbum)
#take a photo using the device’s camera
newImage = photos.capture_image()
#save that photo to a file in Pythonista
newImage.save(testFile)
#add that newly-created file to the device’s photo library
newAsset = photos.create_image_asset(testFile)
#add that newly-created library item to the previously-created album
newAlbum.add_assets([newAsset])
If you don’t want to keep the file around in your Pythonista installation, you can use os.remove to remove it.
import os
os.remove(testFile)
While saving a PIL image to a local file first and then adding the file to the device’s library seems a convoluted means of adding a PIL image to the device’s library, it appears to be the expected means of doing this. In Photo Library Access on iOS, the documentation says:
To add a new image asset to the photo library, use the create_image_asset() function, providing the path to an image file that you’ve saved to disk previously.

How to break "select different image" CAPTCHA with Python

So I have this code in Python3 that scraps data from websites through object recongnition (I used this to automate the download process inside a flash player based website) and Selenium. The problem is that I'm stuck with this website that have a custom made Captcha where the user have to select the different image from the group and I don´t know how to download or get these images from the site in order to identify the different one, has anyone solved a problem like this? or have an idea on how to solve this captcha with any other technique or method?
This is the login that has the CAPTCHA
And here's the link to the site which is in spanish. The captcha basically says "Select the different image"
https://portalempresas.sb.cl/login.php
Thanks!
To download those images as png files you could do:
from io import BytesIO
from PIL import Image
# Download image function
def downloadImage(element,imgName):
img = element.screenshot_as_png
stream = BytesIO(img)
image = Image.open(stream).convert("RGB")
image.save(imgName)
# Find all the web elements of the captcha images
image_elements = driver.find_elements_by_xpath("*//div[contains(#class,'captcha-image')]")
# Output name for the images
image_base_name = "Imagen_[idx].png"
# Download each image
for i in range(len(image_elements)):
downloadImage(image_elements[i],image_base_name.replace("[idx]","%s"%i))
Edit 1:
If you want to compare 2 images to see if they are equal you could try with this post
Edit 2:
Using the solution edited above, these are the results:

How can i get someone's profile pic on Discord to edit it using PIL?

I'm trying to make some code on python to edit someone's profile pic, but all I've got so far is this:
image = ctx.message.author.avatar_url
background = Image.open(image)
Apparently that just gets the URL itself, but i need the image itself to edit a picture with PIL. Any insight on how to get it?
with requests.get(ctx.message.author.avatar_url) as r:
img_data = r.content
with open('image_name.jpg', 'wb') as handler:
handler.write(img_data)
So I played about with this link a bit:
https://cdn.discordapp.com/avatars/190434822328418305/6a56d4edf2a82409ffc8253f3afda455.png
And I was able to save my own avatar image (the one I use for my accounts everywhere). I was then able to open the file regularly with the photo viewer app within Pycharm.
After, it would simply become a case of opening the new jpeg file with PIL or pillow instead of trying to open anything from a website, if that makes sense.
You should consider that this will save a file onto your Discord bot server, so this is extremely crude, a malformed or maliciously formed jpeg file could lead to some sort of remote vulnerability.
Furthermore to your comment, if you want the size of the image you download to be bigger, for example, please see the amended link below to solve your problem there:
https://cdn.discordapp.com/avatars/190434822328418305/6a56d4edf2a82409ffc8253f3afda455.png?size=<Number from list [16,32,64,128,256,512,1024,2048]>
Hope this helps :)

Save loaded images from a web browser with Webkit in Python

today I started developing my Python app. Using GTK and Webkit for Python. And now I have a problem: I want to save a loaded image from the web browser to my computer but I can't download it as a normal image because it's a dynamic image generated by PHP.
With VB.NET I managed to do it, but I want my application to be cross-platform.
Any idea how can I do it?
I want to "clone" the image shown in the web browser, but I can't download it because it is dynamic. In vb.net you execute the command copy and then you save it.
EDIT 2: I have found the solution:
web.copy_clipboard()
clipboard = gtk.clipboard_get()
image = clipboard.wait_for_image()
image.save("test.png", "png")
First make sure that you selected the image.

How do I display a PIL Image object in a template?

If a user uploads an image, and I resize it using PIL, I get a PIL Image object.
How do I display a PIL Image file in a template, before it has been saved to the database? Can it even be passed in as an image and rendered?
For a limited set of browsers, you can base64 encode the image and use inline images. See Embedding Base64 Images.
A solution that works for all browsers is an image tag referencing a view that returns the image.
[update]
All I want is for the user to submit the original image, and then be prompted by another form to input a caption for the image (with the resized image to the left of the caption field). Then when the user hits "submit" the image and the caption get saved in a model instance.
Well... When you use <img src="foo">, foo is always retrieved by a GET perhaps that is why it is not working - request.FILES will not be available in a GET request. If you open firebug or the chrome debug toolbar, in the network tab, you will see the POST request with the uploaded image and after that a GET request to fetch the image.
You have to save the image somewhere between both steps.
how else could i save it? I would love for it to be temporary. Do you think there's a really easy way to do this, or should I go look into those options?
Popular choices are redis and memcached. You can think of them as giant shared python dict with an expire date. If the images are small, like an avatar, you can also save the image data in a session variable.
Yes and no.
Yes, you can put the images as raw Base64 data. Here's a little script you can use to test this:
import Image
import base64
import StringIO
output = StringIO.StringIO()
im = Image.open("test.png") # Your image here!
im.save(output, format='PNG')
output.seek(0)
output_s = output.read()
b64 = base64.b64encode(output_s)
open("test.html","w+").write('<img src="data:image/png;base64,{0}"/>'.format(b64))
However, this is a really bad idea. With multiple thumbnails, your single HTML page might be 10MB+.
What you really should be doing is using a separate Django view to return images from PIL objects as PNG files, and then referencing that view in the img href attributes on your page.
You can embed base64 encoded images into an tag. So you could convert PIL image to base64 and then display it.
from PIL import Image
import StringIO
x = Image.new('RGB',(400,400))
output = StringIO.StringIO()
x.save(output, "PNG")
contents = output.getvalue().encode("base64")
output.close()
contents = contents.split('\n')[0]
Then show with:
<img src="data:image/png;base64,' + contents + ' />
Look to an example output.

Categories