<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Call python image function in pyspark in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/call-python-image-function-in-pyspark/m-p/4788#M1406</link>
    <description>&lt;P&gt;I have a function for rotating images written in python:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from PIL import Image
def rotate_image(image, rotation_angle):
  im = Image.open(image)
  out = im.rotate(rotation_angle, expand = True)
  return out&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I now want to use this function as a pyspark udf.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import pyspark.sql.functions as fn
&amp;nbsp;
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() &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can you please assist with registering and using udf with images.&lt;/P&gt;</description>
    <pubDate>Fri, 05 May 2023 09:07:40 GMT</pubDate>
    <dc:creator>MichaelO</dc:creator>
    <dc:date>2023-05-05T09:07:40Z</dc:date>
    <item>
      <title>Call python image function in pyspark</title>
      <link>https://community.databricks.com/t5/data-engineering/call-python-image-function-in-pyspark/m-p/4788#M1406</link>
      <description>&lt;P&gt;I have a function for rotating images written in python:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from PIL import Image
def rotate_image(image, rotation_angle):
  im = Image.open(image)
  out = im.rotate(rotation_angle, expand = True)
  return out&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I now want to use this function as a pyspark udf.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import pyspark.sql.functions as fn
&amp;nbsp;
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() &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can you please assist with registering and using udf with images.&lt;/P&gt;</description>
      <pubDate>Fri, 05 May 2023 09:07:40 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/call-python-image-function-in-pyspark/m-p/4788#M1406</guid>
      <dc:creator>MichaelO</dc:creator>
      <dc:date>2023-05-05T09:07:40Z</dc:date>
    </item>
    <item>
      <title>Re: Call python image function in pyspark</title>
      <link>https://community.databricks.com/t5/data-engineering/call-python-image-function-in-pyspark/m-p/4789#M1407</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Images are loaded in 1 Struct column containing multiple fields according to &lt;A href="https://spark.apache.org/docs/latest/ml-datasource.html#image-data-source" alt="https://spark.apache.org/docs/latest/ml-datasource.html#image-data-source" target="_blank"&gt;image data source docs&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;I guess the return datatype can be whatever you want it to be as long as it matches the output of the UDF.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2023 15:02:42 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/call-python-image-function-in-pyspark/m-p/4789#M1407</guid>
      <dc:creator>FabriceDeseyn</dc:creator>
      <dc:date>2023-05-10T15:02:42Z</dc:date>
    </item>
    <item>
      <title>Re: Call python image function in pyspark</title>
      <link>https://community.databricks.com/t5/data-engineering/call-python-image-function-in-pyspark/m-p/4790#M1408</link>
      <description>&lt;P&gt;Let's try the following : &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from pyspark.ml.image import ImageSchema, ImageTransformer
from pyspark.sql.functions import udf
from pyspark.sql.types import StringType
&amp;nbsp;
angle = 90
&amp;nbsp;
def rotate_image(image):
    np_array = ImageSchema.toNDArray(image)
    rotated = np.rot90(np_array, k=angle//90, axes=(1, 0))
    return ImageSchema.fromNDArray(rotated)
&amp;nbsp;
rotate_udf = udf(rotate_image, StringType())
&amp;nbsp;
image_df = spark.read.format("image").load("path/to/image/directory")
rotated_images_df = ImageTransformer().setOutputCol("rotated").transform(image_df).withColumn("rotated", rotate_udf("image"))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2023 15:06:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/call-python-image-function-in-pyspark/m-p/4790#M1408</guid>
      <dc:creator>artsheiko</dc:creator>
      <dc:date>2023-05-10T15:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: Call python image function in pyspark</title>
      <link>https://community.databricks.com/t5/data-engineering/call-python-image-function-in-pyspark/m-p/4791#M1409</link>
      <description>&lt;P&gt;Hi @Michael Okelola​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for posting your question in our community! We are happy to assist you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To help us provide you with the most accurate information, could you please take a moment to review the responses and select the one that best answers your question?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This will also help other community members who may have similar questions in the future. Thank you for your participation and let us know if you need any further assistance!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 08:17:07 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/call-python-image-function-in-pyspark/m-p/4791#M1409</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-05-19T08:17:07Z</dc:date>
    </item>
    <item>
      <title>Re: Call python image function in pyspark</title>
      <link>https://community.databricks.com/t5/data-engineering/call-python-image-function-in-pyspark/m-p/45946#M28000</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Stock photos, I've come to realize, are the catalysts of imagination. This website's vast reservoir of images &lt;A href="https://depositphotos.com/photos/new-york-seal.html" target="_self"&gt;new york seal&lt;/A&gt; sparks ideas that ripple through my projects. They empower me to envision the previously unimagined, helping me breathe life into concepts and messages that captivate and inspire.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2023 03:42:04 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/call-python-image-function-in-pyspark/m-p/45946#M28000</guid>
      <dc:creator>Raluka</dc:creator>
      <dc:date>2023-09-24T03:42:04Z</dc:date>
    </item>
  </channel>
</rss>

