Skip to content
Advertisement

How to extract images from pandas dataframe

I want to extract findings and filename columns from below dataframe. enter image description here

For extracting images I use manual path to images having total number of 7467 images.

path = "/content/drive/MyDrive/iuxray/images/images_normalized"

Next I use the following code for extracting findings and images.

for images in os.listdir(path):
 for row in df["findings"]:
   if (images.endswith(".png") or images.endswith(".jpg")
    or images.endswith(".jpeg")):
    caption = row
    image_path = os.path.join(path, images)
    image_path_to_caption[image_path].append(caption) 

But I want to extract images from filename something like this for row in df[filename].

Advertisement

Answer

For future posts, please paste the data in a form that can be more easily used in an example.

I think this is what you are trying to accomplish? (Try to resist the use of for loops):

import pandas as pd
columns = ['uid','MeSH','Problems','image','indication','comparison','findings','impression','filename','projection']
df = pd.DataFrame([
    [1,'normal','normal','Xray Chest PA and Lateral','Positive TB test',None,'The cardiac silhouette and mediastinum size ar...','Normal chest x-XXX.','1_IM-0001-4001.dcm.png','Frontal'],
    [1,'nor,al','normal','Xray Chest PA and Lateral','Positive TB test',None,'The cardiac silhouette and mediastinum size ar...','Normal chest x-XXX.','1_IM-0001-3001.dcm.png','Lateral'],
    [2,'Cardiomegaly/borderline;Pulmonary Artery/enlarged','Chest,2 views. frontal and lateral','Cardiomegaly;Pulmonary Artery','Preop bariatric surgery.',None,'Borderline cardiomegaly. Midline sternotomy XX...','No acute pulmonary findings.','2_IM-0652-1001.dcm.png','Frontal']
], columns=columns)
extensions = ('.png','.jpg','.jpeg')
path = '/conent/drive/MyDrive/iuxray/images/images_normalized'
images = df[df['filename'].str.endswith(extensions)]['filename']
paths = [f'{path}/{i}' for i in images]

paths
['/conent/drive/MyDrive/iuxray/images/images_normalized/1_IM-0001-4001.dcm.png',
 '/conent/drive/MyDrive/iuxray/images/images_normalized/1_IM-0001-3001.dcm.png',
 '/conent/drive/MyDrive/iuxray/images/images_normalized/2_IM-0652-1001.dcm.png']
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement