By Brandon Buck
Search Engine Optimization is a Hydra Whack-A-Mole: you can follow all of the instructions, guides and suggestions provided by Google and industry experts, but you’ll never have a perfect or complete website. Technology, search habits, standards, and capabilities constantly change and update, so your best bet is to focus on what’s important for your business, followed by what makes your website easier to navigate for users and for search engine bots.
Search engines rank your site for a number of reasons, one of which is how easy it is for a search engine to crawl your site and understand the context of what you’re trying to say. Text is the easiest way for a bot to navigate and contextualize your website, so providing text wherever possible is a great way to rank well. Images, audio, and video assets provide a rich experience for the human user, but are more difficult for a bot to understand and contextualize. This is where Alternative Text (“Alt Text”) can come in handy for a crawler. On one end, Alt Text provides a way for Accessiblity tools to help users navigate the web (ie. text descriptions of pictures for visually-impaired users). But on the other, it gives a search engine crawler a quick description of rich media as a frame of reference.
But writing Alt Text is a dull, monotonous process, and the time spent manually writing descriptions could be better spent creating new content- scratch that, should be spent writing new content. And for a vast website like ours, with over 10 years of blogs and thousands of images, is there a better way?
Enter AI.
We’ve already written a ton of articles about AI and ChatGPT, but the truth is AI works best when you provide a measurable goal that you’re trying to achieve. For one task, it’s writing SEO-friendly Alt Text descriptions for over 3,000 individual image files.
One recent update with ChatGPT 4 is the inclusion of Vision in the API, which allows us to send images to OpenAI to analyze. Pair image analysis with writing a short description, and we have a script that can automate Alt Tag writing.
1. Export a list of all image URLs as a CSV file.
- If you use WordPress, WP AllExport is the most thorough tool for this task, both exporting the list and for importing later on.
- https://www.wpallimport.com/documentation/export-woocommerce-products-with-images/#bulk-edit-images-seo
2. Create a separate “clean” list exclusively of the URLs.
- We want to keep the script as simple as possible to reduce the likelihood of errors or overrunning our API calls. It will also keep the process of double-checking our work simple.
3. Create a Python script for the API call.
- Here is the list we used, however ChatGPT itself can help out with creating your Python script if you need some help.
- Replace [your API key], [input.csv], and [output.csv] with your inputs.
import openai
import csv
# Set your OpenAI API key
openai.api_key = '[your API key]'
# Function to generate alt text using OpenAI
def generate_alt_text(image_url):
prompt = "Generate an SEO-friendly alt text for an image that showcases the key visual elements, is concise, and under 125 characters. Do not place quotation marks at the beginning and end of the text. Do not write 'Alt Text: '. Only write the 125 character text."
response = openai.ChatCompletion.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": image_url,
},
],
}
],
max_tokens=300,
)
return response.choices[0].message.content
# Read from CSV and process each URL
with open('[Input.csv]', mode='r') as infile, open('[output.csv]', mode='w', newline='') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
for row in reader:
image_url = row[0]
alt_text = generate_alt_text(image_url)
writer.writerow([image_url, alt_text])
print("Processing complete. Alt text written to output.csv.")
4. Run the script.
- If you have a lot of images, it might be handy to break them down by a couple of hundred items at a time to check OpenAI’s work along the way. We had to run the script about 10 times to render out our full list.
- Avoid PNGs with transparencies or images of just color- Vision gets confused if the image is too simple.
- The process takes around 3.5 seconds per image.
5. Check over the results.
- ChatGPT will create a new CSV file with one column for the image URL and one column for the Alt Text description. Check that the work is done correctly and consistently.
6. Re-import the results to the CSV file you will import back to your website.
- For WordPress, WP AllImport has a tutorial for updating your existing image files without needing to download/upload any new assets.
- https://www.wpallimport.com/documentation/export-woocommerce-products-with-images/#bulk-edit-images-seo
Every CMS is different, and your mileage may vary depending on your needs, but this basic process can be applied to any platform that will allow you to work with image files in bulk.
Once again, AI is a fantastic tool to have in the arsenal as it’s a tool that can build other tools, or be used as an agent to expedite menial automated tasks.