compare directory of pictures to screen with loop

Asked by jhonvinage475

Hello,

First i wish to congratulate for this program and the help you provide.

I have a directory with 221 file *.png and i need to find similar image in a software and delete :

i made this simple script but i need to capture manual each time the var "img" how to load from disk directory the file compare and go to second etc... in a loop

img="1437079537448.png"

img1="delete.png"

wait(0.5)
type(Key.HOME)
wait(0.5)

while True: # loop forever

    if exists(Pattern(img).similar(0.99)):
        rightClick(Pattern(img).similar(0.99))
        click(Pattern(img1).similar(0.99))
        break
    else:
        wait(0.5)
        type(Key.PAGE_DOWN)

Thank.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
jhonvinage475
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Have a look at class Finder in the docs
http://sikulix-2014.readthedocs.org/en/latest/finder.html

Python has a function os.listdir() to get all filenames in a folder
see Python docs:
https://docs.python.org/2/library/os.html#os.listdir

Revision history for this message
jhonvinage475 (gabriel-jean1) said :
#2

Thank RaiMan your answere are much appreciated,

I try to add the sample code finder but the class is very complex, sorry my level programming is not very good.

how to pass the variable of image paths in a loop finder ?

print filename, display path in the log once but when i launch CTRL+R EXECUTE a second time i don't see anythink in the log,
when i restart the sotware sikuli same state :
i have windows 7 x64 bit, sikuli v 1.0.1 it's a bug ?
For this script do i consider the update to SikuliX 1.1.0 final under development (nightly builds available) ?

import os
dir = "D:\\stockage\\brush\\new\\original\\images\\new\\"
for e in os.listdir(dir): # this starts the loop
    fileName = os.path.join(dir, e)
    print fileName

f = Finder()
img= # the image you are searching

f.findAll(img) # find all matches

while f.hasNext(): # loop as long there is a first and more matches
        print "found: ", f.next() # access the next match in the row

print f.hasNext() # is False, because f is empty now
f.destroy() # release the memory used by finder

img="1437079537448.png"

img1="photoshop.png"

wait(0.5)
type(Key.HOME)
wait(0.5)

while True: # loop forever
#while exists(Pattern(img).similar(0.99)):

    if exists(Pattern(img).similar(0.99)):
        rightClick(Pattern(img).similar(0.99))
            # wait(0.5)
        click(Pattern(img1).similar(0.99))
        break
    else:
        wait(0.5)
        type(Key.PAGE_DOWN)

Revision history for this message
RaiMan (raimund-hocke) said :
#3

Ok, for someone with not so much programming experience I admit the solution for that might be a challenge.

Before talking about the solution:
-- are all images of the same size (same width, same height in pixels)

or
- are at least the images of same size, that might be double?

meaning: if images are not of the same size, then they are not equal.

Revision history for this message
jhonvinage475 (gabriel-jean1) said :
#4

help is really appreciated thank you.

The goal of this script :

I have a paint program with many bruhes and i need to remove duplicates :

here find a link with a screen of my computer : on the left you see the panel with brush of the paint software and on the right the windows directory with picture

http://picpaste.com/pics/brush-u1t7pOUn.1437144292.jpg

(i can compare directly duplicate in the drawing software panel with sikuli because same thumbnail of a brush are not necessarily a duplicate because a brush have other parameter like density etc ... so first treatment that's why I export a directory)

in the directory the image file have the same size 32x32 pixels in PNG
and on the software the picture have 1 pixels more in grey around.

when i use manual capture no problem for sikuli to find similar...

Thanks.

Revision history for this message
RaiMan (raimund-hocke) said :
#5

ok, this should do it (but not tested of course;-)

# collect the image files (import os not needed in Sikuli IDE)
dir = "D:\\stockage\\brush\\new\\original\\images\\new\\"
images = [] # to store the filenames
for e in os.listdir(dir): # this starts the loop
    if not e.endswith(".png"): continue
    images.append(os.path.join(dir, e)) # collect

# see the list
print "*** images in folder"
for e in images:
    print e

# walk through the list and compare with all images further down
imagesToDelete = [] # to store images we want to delete
for i in range(len(images)-1):
  if not images[i]: continue # already deleted
  f = Finder(images[i]) # create the Finder
  for k in range(i+1, len(images)): # compare all images in the rest of the list
    f.find(Pattern(images[k]).similar(0.9)) # to avoid false positives
    if not f.hasNext(): continue # not equal, next image
    # the compared image is equal
    imagesToDelete.append(images[k]) # store as to be deleted
    images[k] = None # take away from file list

# see the list of doubles
print "*** images to be deleted"
for e in imagesToDelete:
    print e

exit() # leave here, until the compare works

# delete the double images
for img in imagesToDelete:
  os.remove(img)

Revision history for this message
jhonvinage475 (gabriel-jean1) said :
#6

thank you for this wonderful code that helps a lot.

it's a good idea to compare the directory for duplicates but I have already done this step with the VisiPics software.

What I want is clear from the panel software directory those who are already all duplicates.

Your help allowed me to do that but I always compare the same and it does not cycle up and down in the panel with the scrollbar to the right,
To check the three screen (3 page down) because there are in the software 2913 brushes and only 221 duplicates in the directories.

a help would be welcome :

# collect the image files (import os not needed in Sikuli IDE)
dir = "D:\\stockage\\brush\\new\\original\\images\\new\\"
images = [] # to store the filenames
for e in os.listdir(dir): # this starts the loop
    if not e.endswith(".png"): continue
    images.append(os.path.join(dir, e)) # collect

# declare variables
img1="photoshop.png"

# loop for each image in directory (one by one with path and name) and delete in the panel software and leave the loop or exit after the last picture in the directory
# need check only one time of an image because i want to delete the duplicate in the software panel but keep the other

for e in images:
    wait(0.5)
    type(Key.HOME)
    wait(0.5)
    if exists(Pattern(e).similar(0.99)):
        rightClick(Pattern(e).similar(0.99))
            # wait(0.5)
        click(Pattern(img1).similar(0.99))
        break
    else:
        wait(0.5)
        type(Key.PAGE_DOWN)

Revision history for this message
jhonvinage475 (gabriel-jean1) said :
#7

better work but don't make good image recognition delete the wrong :

# collect the image files (import os not needed in Sikuli IDE)
dir = "D:\\stockage\\brush\\new\\original\\images\\new\\"
images = [] # to store the filenames
for e in os.listdir(dir): # this starts the loop
    if not e.endswith(".png"): continue
    images.append(os.path.join(dir, e)) # collect

# declare variables
img1="photoshop.png"

# loop for image in directory and delete in the panel software
for e in images:
    wait(0.5)
    type(Key.PAGE_DOWN)
    wait(0.5)
    if exists(Pattern(e).similar(0.99)):
        rightClick(Pattern(e).similar(0.99))
            # wait(0.5)
        click(Pattern(img1).similar(0.99))
        type(Key.PAGE_DOWN)
    else:
        wait(0.5)
        type(Key.HOME)

Revision history for this message
RaiMan (raimund-hocke) said :
#8

indeed a bit more complicated.

I thought, that if you deleted duplicates in the filesystem, they would no longer show up in the visual display (left side).

please tell me exactly what you want to do in your own words (no code!).
accompanying screenshots always help.

Revision history for this message
jhonvinage475 (gabriel-jean1) said :
#9

thank RailMan,

Its a pleasure to discuss with you and creat script.
you are very helpful.

but I had to finish yesterday.

So i use this script and with Autoit i make the replace for each var of img (I have more experience with this language)

So autoit control sikuli and all work fine.

I launch more than 10 000 brush for check duplicate this night and all work fine.

I know is not optimised at all and certainly in python i have more performance but it works.

Thank for your time and kindness.

Revision history for this message
RaiMan (raimund-hocke) said :
#10

Thanks for feedback.

I am happy, when you are happy ;-)

all the best.