I have this code working in Python 3.11.5 running in my Windows Python environment and it is fine. It adds text the the bottom of images, in this case, each images’ filename. I would like it to run inside Dynamo.
from PIL import Image, ImageDraw, ImageFont
import os
input_folder = "D:\Dynamo Working\QR"
output_folder = "D:\Dynamo Working\QR2"
font = ImageFont.truetype('arial.ttf', 24)
for filename in os.listdir(input_folder):
if filename.endswith('.jpg') or filename.endswith('.png'):
image_path = os.path.join(input_folder, filename)
image = Image.open(image_path)
if image.mode == 'RGBA':
image = image.convert('RGB')
draw = ImageDraw.Draw(image)
text = os.path.splitext(filename)[0]
# Hardcoded text size and position
text_width, text_height = 250, 50
text_x = (image.width - text_width) / 2 + 50
text_y = image.height - text_height - 10
draw.text((text_x, text_y), text, font=font, fill=(0,0,0))
output_path = os.path.join(output_folder, filename)
image.save(output_path)
In Dynamo, I tried adding:
import sys
sys.path.append(r'C:\Users\desig\AppData\Roaming\Python\Python311\site-packages')
from PIL import Image, ImageDraw, ImageFont
But get this error:
Warning: ImportError : cannot import name '_imaging' from 'PIL' (C:\Users\desig\AppData\Roaming\Python\Python311\site-packages\PIL\__init__.py) [' File "<string>", line 3, in <module>\n', ' File "C:\\Users\\desig\\AppData\\Roaming\\Python\\Python311\\site-packages\\PIL\\Image.py", line 82, in <module>\n from . import _imaging as core\n']
Using sys.version
3.8.3 and Revit 2022.1.
Ref: How to install Python modules in Dynamo Core Runtime 2.8.0?
PS: If I get it to run, I suppose it will only work on my machine, not on a client’s unless the client has a compatible Python install and I path it correctly, eh?