I am using xlsxwriter to insert images and their metadata in excel cells. but I can't seem to scale it properly to get it in a readable manner.
my code is:
def resize_image(path,name):
img=Image.open(path)
img= img.resize((512,512))
global temp_path
full_path=os.path.join(temp_path,name)
img.save(full_path)
return full_path
def rotate_image(path ,name):
img=Image.open(path)
img= img.rotate(90,Image.NEAREST, expand=1)
global temp_path
full_path=os.path.join(temp_path,name)
img.save(full_path)
return full_path
def scale(size):
row=100
col=50
return (row/size[0],col/size[1])
row=1
workbook=xlsxwriter.Workbook('data.xlsx')
bold = workbook.add_format({'bold': True})
worksheet=workbook.add_worksheet()
wrap = workbook.add_format({'text_wrap': True})
worksheet.set_column(1,4,50 ,wrap)
worksheet.write('A1','Image Name',bold)
worksheet.write('B1',"Resized Image",bold)
worksheet.write('C1',"Rotated Image",bold)
worksheet.write('D1',"Metadata",bold)
for i in glob.iglob(path+'\*.jpg',recursive=True):
col=0
size=np.asarray(Image.open(i)).shape
x,y=scale(size)
worksheet.set_row(row,50,wrap)
name=get_image_name(i)
worksheet.write(row,col, name)
col+=1
worksheet.insert_image(row,col,resize_image(i,name),{'x_scale':x, 'y_scale':y,'object_position': 1})
col+=1
worksheet.insert_image(row,col,rotate_image(i,name),{'x_scale':x, 'y_scale':y,'object_position': 1})
col+=1
worksheet.write(row,col,get_metadata(i),wrap)
row+=1
workbook.close()
and the output I am getting is output and I need something like this desired output. Can somebody tell what I am doing wrong? I tried scaling, changing width and height etc. but none seem to work.
EDIT here is my code with the changes in the comment:
image_data={}
row=1
workbook=xlsxwriter.Workbook('data.xlsx')
bold = workbook.add_format({'bold': True})
worksheet=workbook.add_worksheet()
wrap = workbook.add_format({'text_wrap': True})
worksheet.set_column(4,4,50)
worksheet.set_column(1,3,32)
worksheet.set_column(0,0,20)
worksheet.write('A1','Image Name',bold)
worksheet.write('B1',"Resized Image",bold)
worksheet.write('C1',"Rotated Image",bold)
worksheet.write('D1',"Metadata",bold)
for i in glob.iglob(path+'\*.jpg',recursive=True):
col=0
size=np.asarray(Image.open(i)).shape
x,y=scale(size)
worksheet.set_row(row,64)
name=get_image_name(i)
worksheet.write(row,col, name)
col+=1
worksheet.insert_image(row,col,resize_image(i,name),{'x_scale':x, 'y_scale':y})
col+=1
worksheet.insert_image(row,col,rotate_image(i,name),{'x_scale':x, 'y_scale':y})
col+=1
s=get_metadata(i)
if(len(s)!=0):
worksheet.write(row,col,s,wrap)
image_data[name]=s
else:
print('No Metadata found for image {}'.format(name))
row+=1
workbook.close()