+ 1

Some images in the folder need text from csv file

i have some images and i want to draw text on them from csv file image by image and row by row then give them the same name of that row . this is my try but still not working Can anyone give me some help with that thank you all. my code import os import csv from PIL import Image, ImageFont, ImageDraw import numpy as np IMG_DIR = 'D:\programs\python project\images' rows = [] with open("data.csv", "w") as file: csvreader = csv.reader(file) for img_name in os.listdir(IMG_DIR): for row in csvreader: rows.append(row) i = Image.open() Im = ImageDraw.Draw(i) mf = ImageFont.truetype("arial.ttf", 65) # Add Text to an image Im.text((30,30), "row", (0,0,0), font=mf) i.save("row.png")

6th Dec 2023, 8:22 PM
Hamza A Foji
Hamza A Foji - avatar
4 Answers
+ 5
Hamza A Foji , i am currently not able to check your code in a debugger, but 2 things that should be checked by you: > Im.text((30,30), "row", (0,0,0), font=mf) this line is writing the string `row` as *text* to the image (so all images are labeld the same). try it without using quotes. > i.save("row.png") this line is saving all images to the same file name `row.png` (and overwriting them. finally it creates only one file) . you should create a filename from variable `row` + the file extension.
7th Dec 2023, 7:23 AM
Lothar
Lothar - avatar
+ 1
Lothar thank you brother I have my time to learn more about python and practice.have a good day bro
7th Dec 2023, 7:30 AM
Hamza A Foji
Hamza A Foji - avatar
+ 1
Pseudo code Open file data.csv For loop every image in file images For loop every row in data.csv Draw text raw1 on image1 Rename image1 by raw 1 Replay that to last image
7th Dec 2023, 7:35 AM
Hamza A Foji
Hamza A Foji - avatar
+ 1
Hello all I change some steps and try with a simple part by part here where i reach put text in image done loop of images done Csv file row in image done Gathe all the steps in one code will be my next goal https://sololearn.com/compiler-playground/c7Jrv94PzMCu/?ref=app but there is a bug invalid escape sequence
7th Dec 2023, 8:59 PM
Hamza A Foji
Hamza A Foji - avatar