KeyError: 'APP1' when reading metadata with exif - python

I want to cycle through every jpg in my pictures folder and return the name and the date it was taken and add it to a list. Running my code however results in KeyError: APP1. Below you can see my code:
from exif import Image
path = 'F:/Bilder/'
bilder_list = []
for i in os.listdir(path):
if ".jpg" in i:
with open(path+i, 'rb') as image_file:
image = Image(image_file)
bilder_list.append(image.datetime)
print(bilder_list)
Any idea what went wrong here? Any help is greatly appreciated :)

Related

How to analyze multiple images in a folder using a loop?

I am using google cloud vision api in python.
I am a Python beginner. So, I am struggling to implement the content I want to analyze in Python code.
It's a simple thing, but if you can help me, I'd appreciate it.
I want to do label detection in Google Cloud Vision.
I've done loading a single image and implementing the code, but I want to run it on an entire folder containing multiple images.
file_name = r'img_3282615_1.jpg'
image_path = f'.\save_img\{file_name}'
with io.open(image_path, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.label_detection(image=image, max_results=100)
labels = response.label_annotations
df = pd.DataFrame(columns=['description', 'score', 'topicality'])
for label in labels:
df = df.append(
dict(
description=label.description,
score=label.score,
topicality=label.topicality
), ignore_index=True)
print(df)
I've tried analyzing individual images using this code.
Here I would like to do the following steps.
Open the folder
Analyze label detection for all images in the folder(The image names are 'img_3282615_1.jpg', 'img_3282615_2.jpg', 'img_3282615_3.jpg', 'img_1115368_1.jpg', 'img_1115368_2.jpg' ...)
Saving the result as csv (image name, description, score)
I studied that it is possible to repeat using the for statement, but it is difficult to actually write in code. Because I'm just starting to deal with python and lack the basics.
Your answer can be of great help to me.
thank you:)
Can you try this:
from google.cloud import vision
import os
import csv
# Create a client for the Cloud Vision API
client = vision.ImageAnnotatorClient()
# Set the path to the folder containing the images
folder_path = './image_for_text/'
fields = ['description', 'score', 'topicality']
filename_CSV = "./z.csv"
list1=[]
with open(filename_CSV, 'a+') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(fields)
# Loop through all the files in the folder
for filename in os.listdir(folder_path):
# Check if the file is an image
if filename.endswith('.jpg') or filename.endswith('.png'):
# Build the full path to the image
file_path = os.path.join(folder_path, filename)
# Open the image file
with open(file_path, 'rb') as image_file:
# Read the image file into memory
content = image_file.read()
#Create a vision image from the binary data
image = vision.Image(content=content)
#Perform label detection on the image
response = client.label_detection(image=image)
labels = response.label_annotations
# Print the labels for the image
print(f'Labels for {filename}:')
for label in labels:
list1.append(f'{label.description}')
list1.append(f'{label.score*100:.2f}%')
list1.append(f'{label.topicality}')
print(list1)
with open(filename_CSV, 'a+') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(list1)
list1.clear()

Beautifulsoup - why arent the images im scraping saving?

Im iterating through and scraping images off a website... but for some reason the "write" isn't working and saving the image. Am I supposed to declare a directory to save them to or something? here's my request. Im using python 2.7
for img in imgs:
image = img['href']
img_url = my_url + image
resource = urllib.urlretrieve(img_url)
resource = resource[0]
output = open(resource, "wb")
output.write(resource)
output.close()
You're working too hard! urlretrieve will already have written the file to disk, all you need to do is copy it to somewhere more permanent.
filename,headers = urllib.urlretreive(img_url)
import shutil
shutil.copy(filename, "/path/to/somewhere")
But to answer your question about what is going on...
resource = urllib.urlretrieve(img_url) # the file is on disk at /tmp/foobar
resource = resource[0] # resource now contains "/tmp/foobar"
output = open(resource, "wb") # oops! You just opened "/tmp/foobar" for writing, which clears the file

Open .json.xz files

I have a list of directories, in which are contained sub-directories. In each sub-directories there are some 'json.xz' compressed file. If I try to open one of them with my code I get the error:
raise EOFError("Compressed file ended before the "
EOFError: Compressed file ended before the end-of-stream marker was reached
This is my code:
subject = 'AntonioGio'
path = '/home/rootdebian/Scrivania/Socialisys/projects/'+subject+'/competitor/'
for competitors in os.listdir(path):
for f in os.listdir(path+competitors):
if f.endswith('.xz'):
with lzma.open(path+competitors+'/'+f) as f:
json_bytes = f.read()
stri = json_bytes.decode('utf-8')
data = json.loads(stri)
print(data)
what is the best way to fix it? Thank you in advice.
This is probably because the compressed data file you have is incomplete/corrupted. The code you have provided works fine for decompressing json.xz files.

python - Saving images to generated directories

Hopefully this is a quick one for someone. its been annoying me for a while now.
I can create the directory and save the images to the directory where the script is ran, but i cannot figure how to save the images to its specific folder created for that specific advert.
Would someone be able to shed some light on this please?
gundir = soup.find("title").text #keep - folder creation for each advert using title
gun_folders = os.makedirs(gundir)
for img in imgs:
clean = re.compile('src=".*?"')
strings = clean.findall(str(img))
for string in strings:
imgUrl = string.split('"')[1]
filename = imgUrl.split('/')[-1]
resp = requests.get(imgUrl, stream=True)
local_file = open(filename, 'wb')
resp.raw.decode_content = True
shutil.copyfileobj(resp.raw, local_file)
del resp
I understand the above code does what its supposed to do, but its not enough for what i wish it to do.
Could someone point me in the direction on how to achieve what i'm after?
Thanks!
String concatenation
local_file = open('{}/{}'.format(gun_folders ,filename), 'wb')

PIL cannot identify image file for a Google Drive image streamd into io.BytesIO

I am using the Drive API to download an image. Following their file downloading documentation in Python, I end up with a variable fh that is a populated io.BytesIO instance. I try to save it as an image:
file_id = "0BwyLGoHzn5uIOHVycFZpSEwycnViUjFYQXR5Nnp6QjBrLXJR"
request = service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print('Download {} {}%.'.format(file['name'],
int(status.progress() * 100)))
fh.seek(0)
image = Image.open(fh) # error
The error is: cannot identify image file <_io.BytesIO object at 0x106cba890>. Actually, the error does not occur with another image but is thrown with most images, including the one I linked at the beginning of this post.
After reading this answer I change that last line to:
byteImg = fh.read()
dataBytesIO = io.BytesIO(byteImg)
image = Image.open(dataBytesIO) # still the same error
I've also tried this answer, where I change the last line of my first code block to
byteImg = fh.read()
image = Image.open(StringIO(byteImg))
But I still get a cannot identify image file <StringIO.StringIO instance at 0x106471e60> error.
I've tried using alternates (requests, urllib) with no fruition. I can Image.open the the image if I download it manually.
This error was not present a month ago, and has recently popped up into the application this code is in. I've spent days debugging this error with no success and have finally brought the issue to Stack Overflow. I am using from PIL import Image.
Ditch the Drive service's MediaIOBaseDownload. Instead, use the webContentLink property of a media file (a link for downloading the content of the file in a browser, only available for files with binary content). Read more here.
With that content link, we can use an alternate form of streaming—the requests and shutil libraries and the —to get the image.
import requests
import shutil
r = requests.get(file['webContentLink'], stream=True)
with open('output_file', 'wb') as f:
shutil.copyfileobj(r.raw, f)

Categories

Resources