<jupyter_start><jupyter_text>Putting text on images using PIL and Python 💯>  1. Make sure to edit the width of the font (where it should start, and the hright)  >  2. TODO: pull images and fonts from a github repo  Press the green button in the gutter to run the script. https://github.com/samamou/Quran/blob/main/TextWrapping_Images_PIL.ipynbhttps://mpcabd.xyz/python-arabic-text-reshaper/<jupyter_code>#install PIL library
!pip install pillow
!pip install Wand
!apt-get install libmagickwand-dev
!pip install python-bidi
#!pip install arabic_reshaper
!pip install --upgrade arabic-reshaper[with-fonttools]


!pip import requests


!pip install pyarabic
# Importing the PIL library
from PIL import Image, ImageFont, ImageDraw
import textwrap
from string import ascii_letters
from wand.image import Image
from wand.drawing import Drawing
from wand.font import Font
from wand.color import Color

import arabic_reshaper
from bidi.algorithm import get_display

import pyarabic.araby as araby
import pyarabic.number as number

# reshaper = arabic_reshaper.ArabicReshaper(
#     arabic_reshaper.config_for_true_type_font(
#         'DecoType Thuluth Regular.ttf',
#         arabic_reshaper.ENABLE_ALL_LIGATURES
#     )
# )

# import requests

# url = "https://api.quran.com/api/v4/verses/by_juz/{juz_number}?language=ar&words=true&page=1&per_page=6300"

# payload = "{}"
# response = requests.request("GET", url, data=payload)

# print(response.text)<jupyter_output><empty_output><jupyter_text><jupyter_code>#!/usr/bin/env python3
#@title Parameters


# Create a red canvas 600x300
# with Image(width=600, height=300, pseudo='xc:red') as canvas:
#     left, top, width, height = 10, 20, 400, 150

import arabic_reshaper
from arabic_reshaper import ArabicReshaper
# reshaper = arabic_reshaper.ArabicReshaper(
#     arabic_reshaper.config_for_true_type_font(
#         'Amiri-Regular.ttf',
#         arabic_reshaper.ENABLE_NO_LIGATURES
#     )
# )
bg_image = 'border2.png'#@param {type:"string"}


if bg_image == "None":
    bg_image = None
elif bg_image and bg_image.lower().startswith("http"):
    bg_image = download_img(bg_image)    

with Image(filename=bg_image) as background:
    background.format = 'png'
    left, top, width, height = 200, 200, 465, 890
    with Drawing() as context:
        # draw.text_encoding = 'utf-8'
        context.fill_color = 'white'
        context.background_color = Color('white')
        context.stroke_color = Color('white' )
        context.rectangle(left=left, top=top, width=width, height=height)
        font = Font(u'Amiri-Regular.ttf')  #@param {type:"string"}
        context(background)
        # background.caption('Considerably longer text that will need a smaller font and some wrapping tooConsiderably longer text that will need a smaller font and some wrapping tooConsiderably longer text that will need a smaller font and some wrapping tooConsiderably longer text that will need a smaller font and some wrapping too', left=left, top=top, width=width, height=height, font=font, gravity='center')
        text = u"\u0628\u0650\u0633\u0652\u0645\u0650 \u0671\u0644\u0644\u0651\u064E\u0647\u0650 \u0671\u0644\u0631\u0651\u064E\u062D\u0652\u0645\u064E\u0670\u0646\u0650 \u0671\u0644\u0631\u0651\u064E\u062D\u0650\u064A\u0645\u0650 \u0671\u0644\u0652\u062D\u064E\u0645\u0652\u062F\u064F \u0644\u0650\u0644\u0651\u064E\u0647\u0650 \u0631\u064E\u0628\u0651\u0650 \u0671\u0644\u0652\u0639\u064E\u0670\u0644\u064E\u0645\u0650\u064A\u0646\u064E \u0671\u0644\u0631\u0651\u064E\u062D\u0652\u0645\u064E\u0670\u0646\u0650 \u0671\u0644\u0631\u0651\u064E\u062D\u0650\u064A\u0645\u0650 \u0645\u064E\u0670\u0644\u0650\u0643\u0650 \u064A\u064E\u0648\u0652\u0645\u0650 \u0671\u0644\u062F\u0651\u0650\u064A\u0646\u0650 \u0625\u0650\u064A\u0651\u064E\u0627\u0643\u064E \u0646\u064E\u0639\u0652\u0628\u064F\u062F\u064F \u0648\u064E\u0625\u0650\u064A\u0651\u064E\u0627\u0643\u064E \u0646\u064E\u0633\u0652\u062A\u064E\u0639\u0650\u064A\u0646\u064F \u0671\u0647\u0652\u062F\u0650\u0646\u064E\u0627 \u0671\u0644\u0635\u0651\u0650\u0631\u064E\u0670\u0637\u064E \u0671\u0644\u0652\u0645\u064F\u0633\u0652\u062A\u064E\u0642\u0650\u064A\u0645\u064E \u0635\u0650\u0631\u064E\u0670\u0637\u064E \u0671\u0644\u0651\u064E\u0630\u0650\u064A\u0646\u064E \u0623\u064E\u0646\u0652\u0639\u064E\u0645\u0652\u062A\u064E \u0639\u064E\u0644\u064E\u064A\u0652\u0647\u0650\u0645\u0652 \u063A\u064E\u064A\u0652\u0631\u0650 \u0671\u0644\u0652\u0645\u064E\u063A\u0652\u0636\u064F\u0648\u0628\u0650 \u0639\u064E\u0644\u064E\u064A\u0652\u0647\u0650\u0645\u0652 \u0648\u064E\u0644\u064E\u0627 \u0671\u0644\u0636\u0651\u064E\u0627\u0653\u0644\u0651\u0650\u064A\u0646\u064E" #@param {type:"string"} 
        # text = u"اَلَّذِیۡ جَعَلَ مَعَ اللّٰہِ اِلٰہًا اٰخَرَ فَاَلۡقِیَاہُ فِی الۡعَذَابِ الشَّدِیۡدِ اَلَّذِیۡ جَعَلَ مَعَ اللّٰہِ اِلٰہًا اٰخَرَ فَاَلۡقِیَاہُ فِی الۡعَذَابِ الشَّدِیۡدِ اَلَّذِیۡ جَعَلَ مَعَ اللّٰہِ اِلٰہًا اٰخَرَ فَاَلۡقِیَاہُ فِی الۡعَذَابِ الشَّدِیۡدِ اَلَّذِیۡ جَعَلَ مَعَ اللّٰہِ اِلٰہًا اٰخَرَ فَاَلۡقِیَاہُ فِی الۡعَذَابِ الشَّدِیۡدِاَلَّذِیۡ جَعَلَ مَعَ اللّٰہِ اِلٰہًا اٰخَرَ فَاَلۡقِیَاہُ فِی الۡعَذَابِ الشَّدِیۡدِ اَلَّذِیۡ جَعَلَ مَعَ اللّٰہِ اِلٰہًا اٰخَرَ فَاَلۡقِیَاہُ فِی الۡعَذَابِ الشَّدِیۡدِ"
        configuration = {
          'language' : 'Arabic',
          'delete_harakat': False,
          'support_ligatures': False
          # 'shift_harakat_position' : True,
          # 'delete_tatweel' : True
          # 'support_zwj' : True
        }
        reshaper = ArabicReshaper(configuration=configuration)
        #reshaped_text = arabic_reshaper.reshape(text)
        reshaped_text = (reshaper.reshape(text))
        bidi_text = get_display(reshaped_text) 
        
        #######
        words = bidi_text.split(' ')
        # then reverse the split string list and join using space 
        reverse_sentence = ' '.join(reversed(words))
         # finally return the joined string 
        bidi_text = reverse_sentence 
        #######


        background.caption(bidi_text,  left=left, top=top, width=width, height=height, font=font, gravity='center')
    background.save(filename='result.png')


from PIL import Image, ImageFont, ImageDraw
img = Image.new('RGB', (200, 50), color = (255,255,255))
fnt = ImageFont.truetype("(A) Arslan Wessam A (A) Arslan Wessam A.ttf", 50)
#fnt = ImageFont.truetype("DecoType Thuluth Regular.ttf", 50)

configuration = {
    'delete_harakat': False,
    'support_ligatures': False
   
}
reshaper = ArabicReshaper(configuration=configuration)



text = u"اَلَّذِیۡ جَعَلَ مَعَ اللّٰہِ اِلٰہًا اٰخَرَ فَاَلۡقِیَاہُ فِی الۡعَذَابِ الشَّدِیۡدِ اَلَّذِیۡ جَعَلَ مَعَ اللّٰہِ اِلٰہًا اٰخَرَ فَاَلۡقِیَاہُ فِی الۡعَذَابِ الشَّدِیۡدِ اَلَّذِیۡ جَعَلَ مَعَ اللّٰہِ اِلٰہًا اٰخَرَ فَاَلۡقِیَاہُ فِی الۡعَذَابِ الشَّدِیۡدِ اَلَّذِیۡ جَعَلَ مَعَ اللّٰہِ اِلٰہًا اٰخَرَ فَاَلۡقِیَاہُ فِی الۡعَذَابِ الشَّدِیۡدِاَلَّذِیۡ جَعَلَ مَعَ اللّٰہِ اِلٰہًا اٰخَرَ فَاَلۡقِیَاہُ فِی الۡعَذَابِ الشَّدِیۡدِ اَلَّذِیۡ جَعَلَ مَعَ اللّٰہِ اِلٰہًا اٰخَرَ فَاَلۡقِیَاہُ فِی الۡعَذَابِ الشَّدِیۡدِ"
print(text)
text = reshaper.reshape(text )
text = get_display(text) 

ImageDraw.Draw(img).text((0,0),text, font=fnt, fill=(0,0,0))
img<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1639866925422789, 'fraction_numerical': 0.14430274466315499, 'mean_word_length': 4.364312267657993, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '529627', 'license': 'MIT', 'n_tokens_mistral': 4159, 'n_tokens_neox': 3705, 'n_words': 520, 'path': 'TextWrapping_Images_PIL.ipynb', 'repo_name': 'samamou/Quran', 'size': '7214'}