prompter: Write me a python code that processes a CSV file with the following structure ["Name","URL","Rating"], download each image under the URL row as save as PNG, use the Name as the file name and save it under a subfolder using the Rating as name, add a tkinter GUI to let me choose the CSV file and output path.

assistant: Here's a Python program with a TkInter GUI for downloading images specified in a CSV file with your provided format. Please note that the program requires the Requests library to be installed. You can install this library with, for example, the pip utility.

```python
import csv
import pathlib
import tkinter as tk
from tkinter.filedialog import askopenfilename, askdirectory
from tkinter import ttk, messagebox

import requests


class DownloadWindow:
    def __init__(self):
        self.csvfile = None
        self.outdir = None
        self.root = tk.Tk()
        self.root.geometry("200x100")
        self.filechoosebutton = ttk.Button(self.root, text='Choose CSV file', command=self.choose_file)
        self.filechoosebutton.pack()
        self.dirchoosebutton = ttk.Button(self.root, text='Choose output directory', command=self.choose_dir)
        self.dirchoosebutton.pack()
        self.runbutton = ttk.Button(self.root, text='Download files', command=self.download)
        self.runbutton.pack()

    def choose_file(self):
        self.csvfile = askopenfilename()

    def choose_dir(self):
        self.outdir = askdirectory()

    def download(self):
        if self.csvfile is None or self.outdir is None:
            messagebox.showerror(title='Error', message='CSV file or output path not provided!')
            return

        with open(self.csvfile, 'r') as file:
            reader = csv.reader(file)
            for line in reader:
                self.download_one(*line)

    def download_one(self, name, url, rating):
        output_directory = pathlib.Path(self.outdir) / rating
        output_directory.mkdir(parents=True, exist_ok=True)
        output_file = output_directory / f'{name}.png'
        response = requests.get(url)
        with open(output_file, 'wb') as out:
            out.write(response.content)

    def run(self):
        self.root.mainloop()


def main():
    download_window = DownloadWindow()
    download_window.run()


if __name__ == '__main__':
    main()
```

assistant: Sure, here is a sample code to accomplish this task:

import tkinter as tk
from tkinter import filedialog
import pandas as pd
import urllib.request

def process_csv():
    root = tk.Tk()
    root.withdraw()

    csv_file = filedialog.askopenfilename(title='Select the CSV file', filetypes=[('CSV Files', '*.csv')])
    output_path = filedialog.askdirectory(title='Select the output folder')

    df = pd.read_csv(csv_file)
    for i, row in df.iterrows():
        name = row['Name']
        url = row['URL']
        rating = row['Rating']
        
        folder = output_path + '/' + rating
        if not os.path.exists(folder):
            os.makedirs(folder)
        
        image_path = folder + '/' + name + '.png'
        urllib.request.urlretrieve(url, image_path)

process_csv()

This code uses the tkinter library to create a GUI that allows you to select the CSV file and output path. The code reads the CSV file using the pandas library, and then iterates over each row to download the image and save it as a PNG file. The name of the file is based on the "Name" column and the rating is used to create a subfolder in the output path.

prompter: Modify generated code to handle error in case target URL does not exist or does not contain an image.

prompter: How can I change it to support multiple csvs? reply only with the necessary changes

assistant: import csv
import requests
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox

def process_csv(csv_file, output_path):
    with open(csv_file, "r") as file:
        reader = csv.reader(file)
        header = next(reader) # skip the header row
        for row in reader:
            name, url, rating = row
            response = requests.get(url)
            if response.status_code == 200:
                subfolder = output_path + "/" + rating
                if not os.path.exists(subfolder):
                    os.makedirs(subfolder)
                file_path = subfolder + "/" + name + ".png"
                with open(file_path, "wb") as f:
                    f.write(response.content)
            else:
                messagebox.showerror("Error", f"Failed to download {name} from {url}")

def select_csv():
    csv_file = filedialog.askopenfilename(initialdir = "/", title = "Select CSV file", filetypes = (("CSV files", "*.csv"), ("all files", "*.*")))
    if csv_file:
        return csv_file

def select_output_path():
    output_path = filedialog.askdirectory(initialdir = "/", title = "Select output path")
    if output_path:
        return output_path

root = tk.Tk()
root.withdraw()
csv_file = select_csv()
if csv_file:
    output_path = select_output_path()
    if output_path:
        process_csv(csv_file, output_path)
        messagebox.showinfo("Success", "Processing completed")

prompter: When I run the provided code, I get the error "NameError: name 'os' is not defined". You should add "import os" to the top of the file.

prompter: Great, Thank You! Could you now modify the program so that it would, before saving them, also resize all the images to the same resolution. The resolution should be asked via GUI, with the default values 500x500.

prompter: I realized that I actually need to process multiple csvs at once, can you change the code to allow me to do that?

assistant: Sure, I can change the code so that it can process multiple csv files at once.
Here is the altered code:
```
import csv
import requests
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import simpledialog

def process_csv(csv_file, output_path):
    with open(csv_file, "r") as file:
        reader = csv.reader(file)
        header = next(reader) # skip the header row
        for row in reader:
            name, url, rating = row
            response = requests.get(url)
            if response.status_code == 200:
                subfolder = output_path + "/" + rating
                if not os.path.exists(subfolder):
                    os.makedirs(subfolder)
                file_path = subfolder + "/" + name + ".png"
                with open(file_path, "wb") as f:
                    f.write(response.content)
            else:
                messagebox.showerror("Error", f"Failed to download {name} from {url}")

def select_csv():
    csv_file = filedialog.askopenfilename(initialdir = "/", title = "Select CSV file", filetypes = (("CSV files", "*.csv"), ("all files", "*.*")))
    if csv_file:
        return csv_file

def select_output_path():
    output_path = filedialog.askdirectory(initialdir = "/", title = "Select output path")
    if output_path:
        return output_path

root = tk.Tk()
root.withdraw()
csv_file_number = simpledialog.askinteger("Number of files to open", "How many files do you want to open?")
for i in range(csv_file_number):
    csv_file = select_csv()
    if csv_file:
        output_path = select_output_path()
        if output_path:
            process_csv(csv_file, output_path)
            messagebox.showinfo("Success", "Processing completed")
```
The new program uses the tkinter library's simpledialog module to ask the user how many files they wish to open, and then it runs the main body of the program that amount of times.