So this is one of the big reasons why I decided to make this site. I had run into an issue while making adjustments to my discord bot. I wanted to be able to upload a file on discord and get my bot to download the attachment. Then do something to it and then send it back. So here is the code and then I will explain what it does:
#pip install exif if you do not have this
import os
import os.path
import discord
from exif import Image
#Get image / send metadata back
@bot.command()
async def imagedata(ctx):
#Uploading photo portion
attachment = ctx.message.attachments[0]
await ctx.author.send("Uploading file")
await ctx.author.send(attachment.url)
await attachment.save(attachment.filename)
await ctx.author.send("File uploaded")
#Reading metadata
with open (attachment.filename, 'rb') as image_file:
my_image = Image(image_file)
with open("image_data.txt", "w") as f:
print("Latitude data", file=f)
print("-"*60, file=f)
print(f"gps_latitude: {my_image.get('gps_latitude', 'Not found')}\n", file=f)
print(f"gps_latitude_ref: {my_image.get('gps_latitude_ref', 'Not found')}\n", file=f)
print(f"gps_longitude: {my_image.get('gps_longitude', 'Not found')}\n")
print(f"gps_longitude_ref: {my_image.get('gps_longitude_ref', 'Not found')}\n", file=f)
print("Other info\n", file=f)
print("-"*60, file=f)
print(f"Lens make: {my_image.get('lens_make', 'Unknown')}\n", file=f)
print(f"Lens model: {my_image.get('lens_model', 'Unknown')}\n", file=f)
print(f"Lens specification: {my_image.get('lens_specification', 'Unknown')}\n", file=f)
print(f"OS version: {my_image.get('software', 'Unknown')}\n", file=f)
path = "image_data.txt"
await ctx.author.send(file = discord.File(path))
#removing saved attachment
os.remove(attachment.filename)
os.remove(path)
So this piece of code will take the uploaded image and then remove all the metadata that is in it. So lets say you want to upload a photo somewhere that you aren’t sure that will remove the metadata. By metadata I mean removing all the finger prints that show that you took this photo (what took the photo, where it was taken longitude latitude, etc). I had run into the issue of not having anyone having the answer as to how to download the attachment.