Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass image from Python to a MATLAB function

I'm trying to use a MATLAB function in Python using the MATLAB Python engine. The MATLAB function is for processing an image. Here is my code:

import matlab.engine
import os
from PIL import Image

img_rows, img_cols = 256, 256
img_channels = 1

path0 = r'D:\NEW PICS\non_stressed'
path2 = r'D:\empty2'

listing = os.listdir(path0) 
num_samples=size(listing)
print (num_samples)
eng = matlab.engine.start_matlab()

for file in listing:
    im = Image.open(path0 + '\\' + file)   
    img = im.resize((img_rows,img_cols))
    gray = img.convert('L')

    #gray: This is the image I want to pass from Python to MATLAB
    reg = eng.LBP(gray)   
    reg.save(path2 +'\\' +  file, "JPEG")

But it gives me this error:

TypeError: unsupported Python data type: PIL.Image.Image

Please help me with this. Thank you.

like image 440
Tejashree Avatar asked Feb 05 '26 06:02

Tejashree


1 Answers

As described in the MATLAB documentation on how to Pass Data to MATLAB from Python, only a certain number of types are supported. This includes scalar data types, such as int, float and more, as well as (partly) dict. Further, list, set, and tuple are automatically converted to MATLAB cell arrays.

But: array.array, and any module.type objects are not supported. This includes PIL.Image.Image, like in your case. You will have to convert the image to a supported datatype before passing it to MATLAB.

For arrays, MATLAB recommends to use their special MATLAB Array type for Python. You can convert the PIL Image to e.g. a uint8 MATLAB Array with

from PIL import Image
import matlab.engine

image = Image.new('RGB', (1024, 1280))
image_mat = matlab.uint8(list(image.getdata()))
image_mat.reshape((image.size[0], image.size[1], 3))

The final reshape command is required because PIL's getdata() function returns a flattened list of pixel values, so the image width and height are lost. Now, you can call any MATLAB function on the image_mat array.

like image 147
hbaderts Avatar answered Feb 07 '26 20:02

hbaderts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!