Cannot Copy Images Using Shutil.copy2 Error! url is not UTF-8 encoded Saving disabled

I wrote code to copy files to another jePG images and divide it into 20 separate folders according to the classifications. It worked for some folders initially and some folderes were emtpy. When i ran it again second time, none of the folders had any images. Giving error: Error! url is not UTF-8 encoded Saving disabled.

the code reads a csv file with 2 columns. first column is file name and second column is the class it belongs to. Subsequently, the filenames are copied into a list for every class. Then it goes through all the images and if its in the list it copies it into the respective folder.

””

import os
import pandas as pd

df_bodywarmer = pd.read_csv(r'csv/bodywarmer.csv', header = None)
df_cap = pd.read_csv(r'csv/cap.csv', header = None)
df_cargo_pants = pd.read_csv(r'csv/cargo_pants.csv', header = None)
df_chinos = pd.read_csv(r'csv/chinos.csv', header = None)
df_hoodie = pd.read_csv(r'csv/hoodie.csv', header = None)
df_jackets_coats = pd.read_csv(r'csv/jackets coats.csv', header = None)
df_jeans = pd.read_csv(r'csv/jeans.csv', header = None)
df_joggers = pd.read_csv(r'csv/joggers.csv', header = None)
df_overalls = pd.read_csv(r'csv/overalls.csv', header = None)
df_polo_shirts = pd.read_csv(r'csv/polo shirts.csv', header = None)
df_shirts = pd.read_csv(r'csv/shirts.csv', header = None)
df_shorts = pd.read_csv(r'csv/shorts.csv', header = None)
df_swimming_shorts = pd.read_csv(r'csv/swimming shorts.csv', header = None)
df_t_shirt = pd.read_csv(r'csv/t-shirt.csv', header = None)
df_trousers = pd.read_csv(r'csv/trousers.csv', header = None)
df_underwear = pd.read_csv(r'csv/underwear.csv', header = None)
df_vest_tank_top = pd.read_csv(r'csv/vest tank top.csv', header = None)
df_waterproof = pd.read_csv(r'csv/waterproof jacket.csv', header = None )
df_suits = pd.read_csv(r'csv/suits.csv', header = None)
df_jumper = pd.read_csv(r'csv/jumper.csv', header = None )
df1 = pd.read_csv(r'csv/df1.csv')

# creating a list of df
dic_bodywarmer=[]
dic_cap = []
dic_cargo_pants = []
dic_chinos = []
dic_hoodie = []
dic_jackets_coats = []
dic_jeans = []
dic_joggers = []
dic_overalls = []
dic_polo_shirts = []
dic_shirts = []
dic_shorts = []
dic_swimming_shorts =[]
dic_t_shirt = []
dic_trousers = []
dic_underwear = []
dic_vest_tank_top = []
dic_waterproof = []
dic_suits = []
dic_jumper = []


for i in range(len(df_bodywarmer)):
  dic_bodywarmer.append(df_bodywarmer.iloc[i][0])   # loops and adds filename if in df1, female.  eg: ['SPRING0014', 'SPRING0015', 'SPRING0017',
print(len(dic_bodywarmer))

for i in range(len(df_cap)):
  dic_cap.append(df_cap.iloc[i][0])   
print(len(dic_cap),'cap')

for i in range(len(df_cargo_pants)):
  dic_cargo_pants.append(df_cargo_pants.iloc[i][0])   
print(len(dic_cargo_pants),'cargo_pants')

for i in range(len(df_chinos)):
  dic_chinos.append(df_chinos.iloc[i][0])   
print(len(dic_chinos),'chinos')

for i in range(len(df_hoodie)):
  dic_hoodie.append(df_hoodie.iloc[i][0])   
print(len(dic_hoodie),'hoodie')

# continues for all the classes.. 



import os
import shutil 
path = os.path.expanduser('images') 

for file_name in os.listdir(path):
    prefix = file_name[:-5]
    if prefix in dic_bodywarmer:
        target = os.path.join('imagetype/bodywarmer')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_cap:
        target = os.path.join('imagetype/cap')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_cargo_pants:
        target = os.path.join('imagetype/cargo_pants')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_chinos:
        target = os.path.join('imagetype/chinos')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_hoodie:
        target = os.path.join('imagetype/hoodie')
        shutil.copy2(os.path.join(path, file_name), target)         
    if prefix in dic_jackets_coats:
        target = os.path.join('imagetype/jackets_coats')
        shutil.copy2(os.path.join(path, file_name), target)         
    if prefix in dic_jeans:
        target = os.path.join('imagetype/jeans')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_joggers:
        target = os.path.join('imagetype/joggers')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_overalls:
        target = os.path.join('imagetype/overalls')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_polo_shirts:
        target = os.path.join('imagetype/polo_shirts')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_shirts:
        target = os.path.join('imagetype/shirts')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_shorts:
        target = os.path.join('imagetype/shorts')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_t_shirt:
        target = os.path.join('imagetype/t_shirt')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_suits:
        target = os.path.join('imagetype/suits')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_trousers:
        target = os.path.join('imagetype/trousers')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_underwear:
        target = os.path.join('imagetype/underwear')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_vest_tank_top:
        target = os.path.join('imagetype/vest_tank_top')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_waterproof:
        target = os.path.join('imagetype/waterproof')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_suits:
        target = os.path.join('imagetype/suits')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_jumper:
        target = os.path.join('imagetype/jumper')
        shutil.copy2(os.path.join(path, file_name), target) 
    if prefix in dic_swimming_shorts:
        target = os.path.join('imagetype/swimming shorts')
        shutil.copy2(os.path.join(path, file_name), target) 

””

Leave a Comment