Image Comparison with a updating comparison image (PIL) - python-3.x

I need to compare the 1st image from a folder, deleting duplicates, until it finds one that doesn't match. Then it compares the new image with the rest of the picture, repeating the first process. I don't know if I'm forgetting something obvious but how to I set the new image as the comparison. Below is my first attempt at it, the idea that first_image for im1 would be updated after the loop. Any advice would be appreciated.
def add():
for image in files:
im1 = Image.open(path+ "/"+first_image).histogram() #the image to be compared too
im = Image.open(path+ "/" +image).histogram() #the image to be compared
if im == im1:
os.rename(path + "/" + file, path + "/delete")
else:
os.rename(path + "/" + file, path + "/save") ```

The answer was so simple, was overthinking it a lot. just defined the image before the for loop then have the for loop update it as necessary

Related

Upload every photo present in specific folder, without duplicates

I need help. After obtaining a list of photo names (.jpg) in a specific folder (chosen randomly from various folders), is there a way to upload every photo in that folder to a site, until they are finished? I would also like to avoid uploading duplicates.
So far, I have only managed to get a list of photos in a folder:
current_dir = os.getcwd()
folder = random.randint(1, 27)
img_path = current_dir + '\\' + 'Photo' + '\\' + str(folder)
img_dir = os.listdir(img_path)
img_list = []
for N in img_dir:
img_list.append(N)
At this point, I wanted to upload every single photo in the randomly selected folder using the send_keys() command, for example: driver.find_element(By.XPATH, "//input[#type='file']").send_keys(). But how can I do that? Thank you in advance for your help.

How to determine the file extension?

I was trying to copy an image with the path : userDATA\user1\profile_image, but unfortunaly, I don`t know the file extention, because the user can drop his own image, so that the suffix is sometimes different.
file_path = drop_image_instance.image_path # the path looks like this: C:\user\images\download1.png
copyfile(file_path, "userDATA\\" + username + "\\profile_image")
My question is, how can I find out, which suffix the file has, or how can i detect the ending in file_path?
For example:
def find_suffix(path):
file_ending = path[1]
return file_ending
Thanks for answers!

How to change where the output directory where the new images go

I have a small query I'm hoping someone can help me out within Python 3. I am resizing a dataset of 10000 images to all be 1000x1000 in dimension before I do any pytorch analysis with it. I just wanted to ask how I change my code to save the outgoing images to a new folder I have created ''train_resized'' instead of the same folder as the original files as it is doing now when I run. Thanks
# Testing dataset
from PIL import Image
import os, sys
path = (r'G:\My Drive\CATSVDOGS2.0\test1\\')
dirs = os.listdir( path )
def resize():
for item in dirs:
if os.path.isfile(path+item):
im = Image.open(path+item)
f, e = os.path.splitext(path+item)
imResize = im.resize((1000,1000), Image.ANTIALIAS)
imResize.save(f + ' resized.jpg', 'JPEG', quality=90)
resize()
In your line
imResize.save(f + ' resized.jpg', 'JPEG', quality=90)
you're setting the path when using the variable f, as f uses the path variable you defined. A quick way to set the path is to do something like:
imResize.save('G:\\My Drive\\Path\\To\\Folder\\' + item + ' resized.jpg', 'JPEG', quality=90)
of course specify the path to be whatever you want. Untested as I don't have Python installed on my work machine, but that is the general gist.

for loop and file saving

I am using Jupyter and which is working on pyspark(python).
I have used "for" loop to iterate the process and trying to save the file after each iteration.
for example:
name = "mea"
for i in range(2):
print "name[i]"
i +=1
and output is:
name[i]
name[i]
this above algorithm is the short explaination related to the main algorithm that i am working on.
the problem is it is giving an output name[i] and I want it to give me name1 and for second iteration name[2].
I need to use " " because i wanted to save my file to specific folder and i need to speacify the path in " ". So after firsdt iteration it should save the file as name1 and after second iteration it should save the file as name[2].
enter image description here
so from image in my actual algorithm, result is the output that i am getiing after each for loop iteration and for each output, i wanted to save it in new files like result[0],result1,result[2] instead of result[i],result[i],result[i]. because the latter one, it is replacing the file to the old one.
I guess it has nothing specific to pyspark that you are trying to achieve. As per your example, what you need is - use of variable in strings,
so this will suffice for your example:
name = "mea"
for i in range(2):
print "name[%s]" % i
i +=1
You can modify your print statement as follows
print "name[" + str(i) + "]"

Python not reading file/ executing code properly

I have a strange problem which I'm not sure why is occurring.
In a game, I type a command (::pos) which writes the positional coords to a .txt file. When I try automate this process (using win32com to type the command) and read the file, the previous coords are displayed instead of the current ones. This makes no sense as I update the coords before displaying them:
def add(self):
id = self.myTextInput.text()
shell.AppActivate('App title')
# Update position file
shell.SendKeys('::pos')
shell.SendKeys('{ENTER}')
# Add object
shell.SendKeys('::object ' + id + ' 0')
shell.SendKeys('{ENTER}')
# Read position
with open('.txt file path', 'r') as f:
f = f.read().split()
self.listWidget.addItem(id + ' ' + f[1] + ' ' + f[3] + ' 0')
The code is executed upon pressing a button 'add'. The weird thing is that it all works fine if I create a separate button and function called 'updatePos' (which I press before pressing 'add') but doesn't work if I just call the updatePos() function within add. I have also tried using sleeps to stagger commands but that seems to work strangely too (sleeps block of code before it, not sure if its the shell commands messing with it?).
I think the code is executing in an abnormal order perhaps? E.g. opening and reading text file before the position is updated - but not sure why this is happening.
You need to close the file before you can read in the changes.
try adding f.close() to the end of this.

Resources