Call python image function in pyspark

MichaelO
New Contributor III

I have a function for rotating images written in python:

from PIL import Image
def rotate_image(image, rotation_angle):
  im = Image.open(image)
  out = im.rotate(rotation_angle, expand = True)
  return out

I now want to use this function as a pyspark udf.

import pyspark.sql.functions as fn
 
df = spark.read.format("image").load("image2.jpg")
#Trying to register the function here
#Not sure what data type to declare the image as here
_udf = fn.udf(rotate_image) 
#To apply the function - not really sure what I am doing here
df.withColumn('x1', _udf(array('image', 90))).show() 

Can you please assist with registering and using udf with images.