I am learning Python through the development of a Kodi add-on. I am trying to display an image using the xbmcplugin.setResolvedUrl() function but I get an error:
'Attempt to use invalid handle -1' error.
I am get rid of the error when including li.setProperty("IsPlayable", "true") in my addFile function but then the image is not displayed.
What am I doing wrong?
My code:
import sys
import urllib
import urlparse
import xbmcgui
import xbmcplugin
import xbmcaddon
my_addon = xbmcaddon.Addon()
addon_path = xbmc.translatePath(my_addon.getAddonInfo('path'))
base_url = sys.argv[0]
addon_handle = int(sys.argv[1])
args = urlparse.parse_qs(sys.argv[2][1:])
mode = args.get('mode', None)
def build_url(query):
return base_url + '?' + urllib.urlencode(query)
def addFile(filename, filepath):
filedir = build_url({'mode': 'display', 'filepath': filepath})
li = xbmcgui.ListItem(filename)
xbmcplugin.addDirectoryItem(handle=addon_handle, url=filedir, listitem=li)
def displayImage(url):
li = xbmcgui.ListItem('Sample image')
li.setPath(url)
xbmcplugin.setResolvedUrl(addon_handle, True, listitem=li)
xbmc.log('Display image...',level=xbmc.LOGNOTICE)
if mode is None:
fileurl = 'https://elmicrolector.files.wordpress.com/2016/10/breu-14-a1.jpg'
addFile('Sample image', fileurl)
xbmcplugin.setContent(addon_handle, 'files')
xbmcplugin.endOfDirectory(addon_handle)
elif mode[0] == 'display':
displayImage(args['filepath'][0])
I found out the xbmcplugin.setResolvedUrl() function can only be used with videos, not with images.
Related
I want to build a Line chatbot that when a POST requests is received and will parse the receive POST requests information to the chatbot users.
import os
import sys
from argparse import ArgumentParser
import json
from flask import Flask, request, abort,Response,jsonify
import requests
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError, LineBotApiError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,SourceUser
)
app = Flask(__name__)
data=''
#app.route('/data', methods=['POST'])
def get_data():
"""devicename,exceptionType,temperature,mask,stranger"""
device_name = request.form.get('device_name')
exceptionType = request.form.get('exceptionType')
temperature = request.form.get('temperature')
mask = request.form.get('mask')
stranger = request.form.get('stranger')
global data
data=f"device_name = {device_name} exceptionType = {exceptionType} temperature = {temperature} mask = {mask} stranger = {stranger}"
return_status = {"success":"true"}
return jsonify(return_status)
while len(data) > 1:
try:
line_bot_api.broadcast(
messages=TextSendMessage(text=str(data))
)
data=''
except LineBotApiError as e:
raise e
if __name__ == "__main__":
arg_parser = ArgumentParser(
usage='Usage: python ' + __file__ + ' [--port <port>] [--help]'
)
arg_parser.add_argument('-p', '--port', default=8000, help='port')
arg_parser.add_argument('-d', '--debug', default=False, help='debug')
options = arg_parser.parse_args()
app.run(debug=options.debug, port=options.port)
here I have two parts, first is the POST requests receiver. and second is the part that checks the condition if data is present. But when I send the POST requests, the line_bot_api.broadcast does not send the message to the chatbot user. I only saw examples of flask code that has a #app.route() so I was wondering if something like this works? or is there a better way to implement the condition (instead of checking data value, is it better to check if function get_data() ran or requests has been sent to our /data route)
The while loop stops immediately because the condition fails. One way to get around this and do what you want is by using threads.
import os
import sys
from argparse import ArgumentParser
import json
import threading
from flask import Flask, request, abort,Response,jsonify
import requests
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError, LineBotApiError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,SourceUser
)
app = Flask(__name__)
data=''
#app.route('/data', methods=['POST'])
def get_data():
"""devicename,exceptionType,temperature,mask,stranger"""
device_name = request.form.get('device_name')
exceptionType = request.form.get('exceptionType')
temperature = request.form.get('temperature')
mask = request.form.get('mask')
stranger = request.form.get('stranger')
global data
data=f"device_name = {device_name} exceptionType = {exceptionType} temperature = {temperature} mask = {mask} stranger = {stranger}"
return_status = {"success":"true"}
return jsonify(return_status)
def broadcast():
while True:
if len(data) < 1 : continue
try:
line_bot_api.broadcast(
messages=TextSendMessage(text=str(data))
)
data=''
except LineBotApiError as e:
raise e
if __name__ == "__main__":
arg_parser = ArgumentParser(
usage='Usage: python ' + __file__ + ' [--port <port>] [--help]'
)
arg_parser.add_argument('-p', '--port', default=8000, help='port')
arg_parser.add_argument('-d', '--debug', default=False, help='debug')
options = arg_parser.parse_args()
broadcast_thread = threading.Thread(target=broadcast)
broadcast_thread.start()
app.run(debug=options.debug, port=options.port)
This is definitely not the best way. But it works.
I have a wordcloud in python, i want to use MVC to call the python .py and show the output on my View.
Now I can already call the python .py in c#, it can successfully run and show the wordcloud.
But i dont know how to display the wordcloud output on my View.
I have referenced this pageHow do I execute and return the results of a python script in c#?.
Here is my wordcloud.py
# -*- coding: utf-8 -*-
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import matplotlib.pyplot as plt
import pyodbc, time
import pandas as pd
import numpy as np
import sys
import jieba
import jieba.posseg as pseg
from io import BytesIO
import base64
from datetime import datetime, timedelta
from collections import Counter
from PIL import Image
def wc():
#temp=sys.argv[1]
#print("ssssssssssssssssssssssssssssssssssss")
server = 'LAPTOP-22AVH2GL\MSSQLSERVER01'
#server = 'WILLY\SQLEXPRESS'
database = 'Project2'
uid = 'sa'
pwd = 'neil0627'
#pwd = 'h24664034'
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+uid+';PWD='+pwd)
cursor = cnxn.cursor()
#sqlcom = "SELECT DISTINCT JIEBA, COUNT(JIEBA) FROM Jieba WITH(NOLOCK) GROUP BY jieba" #News.api #Project
sqlcom = "SELECT DISTINCT JIEBA, COUNT(JIEBA) FROM Jieba1 WITH(NOLOCK) GROUP BY jieba" #News.api #Project2
news = pd.read_sql(sqlcom, con=cnxn)
print(news)
df3 = np.array(news)
print(df3)
df4 = dict(df3.tolist())
print(df4)
font_path = r'D:\anaconda3\Lib\site-packages\wordcloud\SimHei.ttf'
#mask = np.array(Image.open("D:/456.png"))
# back_color = imageio.imread("./taiwan.png")
#myWordClode = WordCloud(width=850, height=850, background_color="white",
# font_path=font_path, colormap="Dark2",mask=mask)
myWordClode = WordCloud(width=850, height=850, background_color="white",
font_path=font_path, colormap="Dark2")
myWordClode.generate_from_frequencies(df4)
# use PIL show the wordcloud
fig = plt.figure()
#fig = plt.figure(figsize=(20,10))
plt.imshow(myWordClode)
plt.axis("off")
#fig.savefig("testtttttt.png")
plt.show()
#sio = BytesIO()
#plt.savefig(sio, format='png')
#data = base64.encodebytes(sio.getvalue()).decode()
#print(data)
myWordClode.to_file('wcp1.png')
wc()
And here is my controller
private string run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = #"C:/Users/user/AppData/Local/Programs/Python/Python38-32/python.exe";
start.CreateNoWindow = true;
start.Arguments = string.Format("{0} {1}", cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
//Console.Write(result);
//string stderr = process.StandardError.ReadToEnd();
//string stdout = process.StandardOutput.ReadToEnd();
//Debug.WriteLine("STDERR: " + stderr);
//Debug.WriteLine("STDOUT: " + stdout);
return result;
}
}
}
I try to use Viewbag to pass the image to View.I dont know if this is right.Here is my code.
ViewBag.wordcloud = run_cmd("D:/Python/wordcloudjieba.py", "Some Input");
Has anyone to do this before?
Thanks for any idea!!
I followed the http://www.toadstorm.com/blog/?p=136 to open a maya scene without the UI.
This is the main script:
import maya.cmds as cmds
import subprocess
# replace mayaPath with the path on your system to mayapy.exe
mayaPath = 'C:/Program Files/Autodesk/Maya2018/bin/mayapy.exe'
# replace scriptPath with the path to the script you just saved
scriptPath = 'C:/Users/Rik/Desktop/CreateNewLayerWithMeshesForBatchRenderPurpose.py'
def massAddRenderLayer(filenames, layername):
for filename in filenames:
maya = subprocess.Popen(mayaPath+' '+scriptPath+' '+filename+' '+layername,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out,err = maya.communicate()
exitcode = maya.returncode
if str(exitcode) != '0':
print(err)
print 'error opening file: %s' % (filename)
else:
print 'added new layer %s to %s' % (out,filename)
filenames = ['C:\Users\Rik\Desktop\Test_maya_file_FILE1.ma']
renderLayerToAdd = 'newRenderLayer'
massAddRenderLayer(filenames, renderLayerToAdd)
and this is the script that is called for:
import sys
import maya.standalone as std
std.initialize(name='python')
import maya.cmds as cmds
from mtoa.cmds.arnoldRender import arnoldRender
filename = sys.argv[1]
layername = sys.argv[2]
def addRenderLayer(filename,layername):
try:
cmds.file(filename,o=True,f=True)
newLyr = cmds.createRenderLayer(n=layername,empty=True,makeCurrent=True)
meshes = cmds.ls(type='mesh')
xforms = []
for i in meshes:
xf = cmds.listRelatives(i,p=True)[0]
xforms.append(xf)
cmds.editRenderLayerMembers(layername,xforms)
cmds.loadPlugin( 'mtoa.mll' )
arnoldRender(1, 1, True, True,'camera1', ' -layer' + newLyr)
cmds.setAttr("defaultArnoldDriver.ai_translator", "png", type="string")
cmds.setAttr("defaultArnoldDriver.pre", "file_name", type="string")
arnoldRender(1920, 1080, True, True,'camera1', ' -layer' + newLyr)
sys.stdout.write(newLyr)
return newLyr
except Exception, e:
sys.stderr.write(str(e))
sys.exit(-1)
addRenderLayer(filename,layername)
When I run the main script he opens the mayapy.exe program, but when I want to render a frame then I get an error saying Failed to register renderer 'arnold' even when I have imported arnold and loaded the 'mtoa.mll' plugin.
How can I 'register 'arnold''?
this code should ask 2 optional and 1 required argument, 'format' and 'type' argument are optional and 'url' is required argument which will be the address of website from where you wants to download image(either jpg or/and .png type image,depend on what value you give in 'type' argument ) and save it as either as individual image or single JSON file(depend on what value you give in 'format' argument ), i am getting error can you tell where i am going wrong,here is my code..
import argparse
import base64
import json
import os
from bs4 import BeautifulSoup
import requests
def scrape(url,format_,type_):
try:
page = requests.get(url)
except requests.RequestException as rex:
print(str(rex))
else:
soup = BeautifulSoup(url,'html.parser')
images = _fetch_images(soup,url)
images = _filter_images(images,type_)
_save(images,format_)
def _fetch_images(soup,base_url):
images = []
for img in soup.find_all('img'):
src = img.get('src')
img_url = ('{base_url}/{src}'.format(base_url,src))
name = img_url.split('\\')[-1]
images.append(dict(name=name,url=img_url))
return images
def _filter_images(images,type_):
if type == 'all':
return images
ext_map = {'png':['.png'],'jpg':['.jpg','.jpeg']}
return [img for img in images if
_match_extension(img['name'],ext_map(type_))]
def _match_extension(filename,extension_list):
name,extension = os.path.splittext(filename.lower())
return extension in extension_list
def _save(images,format_):
if images:
if format_=='img':
_save_images(images)
else:
_save_json(images)
print('Done!')
else:
print('there are no images!')
def _save_images(images):
for img in images:
img_data = requests.get(img['url']).content
with open(img['name'],'wb') as f:
f.write(img_data)
def _save_json(images):
data = {}
for img in images:
img_data = requests.get(img['url']).content
b64_img_data = base64.b64encode(img_data)
str_img_data = b64_img_data.decode('utf-8')
data[img['name']]=str_img_data
with open('images.json','w') as ijson:
ijson.write(json.dump(data))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Scrape a webpage.')
parser.add_argument(
'-t',
'--type',
choices=['all', 'png', 'jpg'],
default='all',
help='The image type we want to scrape.')
parser.add_argument(
'-f',
'--format',
choices=['img', 'json'],
default='img',
help='The format images are saved to.')
parser.add_argument(
'url',
help='The URL we want to scrape for images.')
args = parser.parse_args()
args.url = str(input('enter the url\n'))
args.format = str(input('enter img or json \n'))
args.type = str(input('enter all or png or jpg'))
scrape(args.url,args.format,args.type)
i wants to run it in PyCharm and error i got is,
usage: scrape.py [-h] [-t {all,png,jpg}] [-f {img,json}] url
scrape.py: error: the following arguments are required: url
In order to pass parameters to your function in Pycharm, navigate to Run -> Edit Configurations and set the parameters there.
Alternatively, try invoking your program with parameters specified from the command line.
Is there a Python library I can use for processing files with chm extension that has similar features as HTML parser or BeautifulSoup?
PyCHM:
http://gnochm.sourceforge.net/pychm.html
I've struggled with PyCHM to create simple thumbnailer extracting cover images from .chm files. Here is the code for all those who find this question in the future:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import chm.chm as chm
from bs4 import BeautifulSoup
from PIL import Image
import urlparse
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
class CHMFile:
def __init__(self, file_name):
self.chmfile = chm.CHMFile()
self.chmfile.LoadCHM(file_name)
def create_thumb(self, out_file):
image = None
area = 0 # cover will propably be the biggest image from home page
iui = self.chmfile.ResolveObject(self.chmfile.home)
home = self.chmfile.RetrieveObject(iui[1])[1] # get home page (as html)
tree = BeautifulSoup(home)
for img in tree.find_all('img'):
src_attr = urlparse.urljoin(self.chmfile.home, img.get('src'))
chm_image = self.chmfile.ResolveObject(src_attr)
png_data = self.chmfile.RetrieveObject(chm_image[1])[1] # get image (as raw data)
png_img = Image.open(StringIO(png_data))
new_width, new_height = png_img.size
new_area = new_width * new_height
if(new_area > area and new_width > 50 and new_height > 50): # to ensure image is at least 50x50
area = new_area
image = png_img
if image:
image.save(out_file, format="PNG")
if __name__ == '__main__':
import sys
if len(sys.argv) != 3:
print 'Create thumbnail image from an chm file'
print 'Usage: %s INFILE OUTFILE' % sys.argv[0]
else:
chm = CHMFile(sys.argv[1])
chm.create_thumb(sys.argv[2])