Closed SingularisArt closed 1 year ago
3laykum Salaam. What are you attempting to do? Draw images from the text?
I'm trying to create images from the text. But I noticed an issue (I don't know if I'm the one causing the issue or not) where the first ayah of each surah is the second ayah. For example, in the image shown above, I was taking the first ayah from the API, but it was displaying to me the second ayah of surat Ash-Shura (42).
I'm trying to create images from the text. But I noticed an issue (I don't know if I'm the one causing the issue or not) where the first ayah of each surah is the second ayah. For example, in the image shown above, I was taking the first ayah from the API, but it was displaying to me the second ayah of surat Ash-Shura (42).
It's an array of ayahs that gets returned, so you need to start with 0, not 1. I can try and take a look at the script next week, inshaAllah.
When I run the following python3.10 main.py 42 --verse-num 0
(I'm getting the 0th ayah from the array of ayat that I received from the API), I get the following image:
When I run the following
python3.10 main.py 42 --verse-num 0
(I'm getting the 0th ayah from the array of ayat that I received from the API), I get the following image:
Which is correct. Now you just need to filter out the bismillah so you get the حم.
See https://github.com/islamic-network/api.alquran.cloud/issues/17
Salamu Alaykum
I've currently made the following program:
Toggle to view code (python)
```python import argparse import os import textwrap from PIL import Image, ImageDraw, ImageFont import convert_numbers import requests def draw_text(draw, x_loc, y_loc, text, fill, font): draw.text((x_loc, y_loc), text, fill=fill, font=font) def get_image(data, args, verse_num): # Get the Quranic text from the response JSON try: quranic_text = data["ayahs"][verse_num]["text"] except IndexError: return # Add the verse ending mark with number to the bottom right corner verse_num_arabic = convert_numbers.english_to_arabic(verse_num) ending_mark = "\u06DD" + verse_num_arabic quranic_text += ending_mark paragraphs = textwrap.wrap(quranic_text, width=80) y_text = 50 # Set up the image and font image_width = 750 image_height = len(paragraphs) * 200 font_size = 40 font_path = "/usr/share/fonts/truetype/Scheherazade-Regular.ttf" font = ImageFont.truetype(font_path, font_size) text_color = (0, 0, 0) background_color = (255, 255, 255) # Create the image image = Image.new("RGB", (image_width, image_height), background_color) draw = ImageDraw.Draw(image) # Draw the text for para in paragraphs: text_bbox = draw.textbbox((0, 0), para, font=font) text_width = text_bbox[2] - text_bbox[0] text_height = text_bbox[3] - text_bbox[1] x_text = (image_width - text_width) // 2 draw_text(draw, x_text, y_text, para, text_color, font) y_text += text_height + 10 # Format the surah and verse numbers out_surah_num = str(args.surah_num).zfill(3) out_surah_verse = str(verse_num).zfill(3) # Save the image to a file dir = f"surah-{out_surah_num}" image.save(f"{dir}/surah-{out_surah_num}-verse-{out_surah_verse}.png") def main(): parser = argparse.ArgumentParser() parser.add_argument("surah_num", type=int, help="The surah number (1-114)") parser.add_argument("--verse-num", "-v", type=int, help="The verse number") args = parser.parse_args() dir = f"surah-{str(args.surah_num).zfill(3)}" if not os.path.exists(f"{dir}"): os.makedirs(f"{dir}") # Set the API endpoint URL url = f"http://api.alquran.cloud/v1/surah/{args.surah_num}" # Make an HTTP GET request to the API endpoint response = requests.get(url) data = response.json()["data"] if args.verse_num and ( args.verse_num < 0 or args.verse_num > int(data["numberOfAyahs"]) + 1 ): print("Verse number is out of range") print( "For the surah, the verse number should be between 1 and", data["numberOfAyahs"] + 1, ) return if args.verse_num or args.verse_num == 0: get_image(data, args, args.verse_num) else: for verse_num in range(1, data["numberOfAyahs"] + 1): get_image(data, args, verse_num) if __name__ == "__main__": main() ```But when you run it with
python3 main.py 42 --verse-num 1
, it gives you the second ayah instead of the first.Am I doing something wrong, or is something wrong with the data in the API?