Not able to call python script from matlab using py command

22 ビュー (過去 30 日間)
baibhav kumar
baibhav kumar 2019 年 7 月 10 日
回答済み: Shrinidhi KR 2020 年 5 月 8 日
I downloaded the Add - on How to call Python Library function in Matlab.
I also downloaded python version 3.6. When I am using the command pyversion, it is showing correctly that version of python is 3.6.
But when I am trying to call the python code in matlab , it is giving an error - Undefined variable "py" or class "py.call_new1.view_img".
Here is my python code stored in file which is working fine in python editor . What my code does is that it is taking random shapes from the folder E:/baibhav/MPEG7dataset/Distinct_images. Afterwards it is filling a random colour inside that shape from colour_list.After that it is also superimposing a random alphabet from string.ascii_letters on the above obtained image.
import os,random
import string
from PIL import Image,ImageDraw, ImageFont
def random_string_generator(str_size, allowed_chars):
return ''.join(random.choice(allowed_chars) for x in range(str_size))
def view_img(str):
for i in range(1, 10):
file1 = folder1+'\\'+a
img1 ="RGBA")
width, height = img1.size
center = (int(0.5 * width), int(0.5 * height))
#yellow = (100, 100, 0, 255)
colour_list = ((255,0,0,255),(0,255,0,255),(0,0,255,255),(210,105,30,255),(255, 255, 0, 255),(0,255,255,255),(128,128,128,255),(255,0,255,255),(100, 100, 0, 255),(100,0,255,255),(0,100,100,255))
#col = random.choice(colour_list)
ImageDraw.floodfill(img1, xy=center, value=random.choice(colour_list))
#img2 = img1;
chars = string.ascii_letters
size = 1
print('Random alphabet is', random_string_generator(size, chars))
# initialise the drawing context with
# the image object as background
draw = ImageDraw.Draw(img1)
font = ImageFont.truetype('Roboto-Bold.ttf', size=100)
(x, y) = (150, 150)
name = random_string_generator(size, chars)
color = 'rgb(255, 255, 255)' # white color
draw.text((x, y), name, fill=color, font=font)
# save the edited image"H:/superimpose/filename"+str(i)+".png", 'PNG')
Here is how I am calling the same code in matlab command window :-
But it is giving me following error:
Undefined variable "py" or class "py.call_new1.view_img".
Please help me with this.Thanks in Advance.

回答 (2 件)

Jalaj Gambhir
Jalaj Gambhir 2019 年 7 月 19 日
編集済み: Jalaj Gambhir 2019 年 7 月 19 日
This is a common error that occurs when python is not able to locate your script. You can add the path of your file to the python searchable directories as:
  1. In the MATLAB command window check whether the path to your folder containing the file exists. It should be not. To do so, execute
P = py.sys.path
2. For your case, execute the following, to add the path of your folder in python searchable directories:
if count(P,'E:/baibhav/MPEG7dataset/Distinct_images') == 0
insert(P,int32(0), 'E:/baibhav/MPEG7dataset/Distinct_images');
3. Execute
to check the output again. If the path is added to the list, execute the following for the desired result.

Shrinidhi KR
Shrinidhi KR 2020 年 5 月 8 日
Verify all of the points mentioned in this link, either one of these should be causing the issue.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by