Jump to content

Mass deletion of PNG files smaller than a certain size (pixels, not bytes)

So basically, I recovered a lot of files from an external HDD and now have around 250000 PNG files. And now the problem I am facing is that there are a lot of textures from Minecraft texture packs and Windows applications mix in because the recovery program didn't restore the folder structure. And now I am trying to find a tool to mass delete them when they have a certain size, e.g., 64x64 or 1024x1024. I'd prefer a Linux command line tool, but It's not so important. Thank you in advance.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Here's a small shell script that should do what you want. Not quite a one liner, but should be easy enough to adjust as needed. Requires ImageMagick to be installed.

#!/bin/sh

IMAGE_PATH=.
IMAGE_TYPE=*.png

find $IMAGE_PATH -type f -name $IMAGE_TYPE | while read IMAGE_FILE; do
    IMAGE_SIZE=$(identify -format '%w %h' $IMAGE_FILE)

    IMAGE_WIDTH=$(echo $IMAGE_SIZE | awk {'print $1'})
    IMAGE_HEIGHT=$(echo $IMAGE_SIZE | awk {'print $2'})

    if (($IMAGE_WIDTH < 1024 && $IMAGE_HEIGHT < 1024)); then
        echo "$IMAGE_FILE is $IMAGE_WIDTH x $IMAGE_HEIGHT"
        # TODO - DELETE FILE HERE
    fi
done

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

filebot -find will work on all platforms provided you don't mind installing filebot first.

 

e.g. find *.png files where the image height is 1024px:

filebot -find /input --filter "ext == /png/ && height == 1024"

 

e.g. find *.png files where the image height is 1024px and delete them:

filebot -find /input --filter "ext == /png/ && height == 1024" -exec rm -v {}

 

Link to comment
Share on other sites

Link to post
Share on other sites

@rednoah @Eigenvektor  Thank you both for the quick responses. Unfortunately, I only had the last few days to do it and was only able to try your solution. Unfortunately, I have to say that both of them didn't work for me. I installed both file bot (over apt) and image magic (also over apt). With your script, @Eigenvektor , I got the following ERROR :
./DelIMG.sh: 12: cannot open 1024: No such file
And with your command, @rednoah , I got the following ERROR:
Bad system encoding: ANSI_X3.4-1968
* Please configure your locale with LANG=C.UTF-8 or LANG=en_US.UTF-8
File does not exist: /input

 

Link to comment
Share on other sites

Link to post
Share on other sites

43 minutes ago, TechEnjoyer said:

@Eigenvektor , I got the following ERROR :
./DelIMG.sh: 12: cannot open 1024: No such file

Please try again with this version… I forgot to add quotes around the variables containing paths/files names, which works fine if it contains no blanks, but breaks if it does. This should hopefully work as expected:

#!/bin/sh

IMAGE_PATH=.
IMAGE_TYPE=*.png

find "$IMAGE_PATH" -type f -name "$IMAGE_TYPE" | while read "IMAGE_FILE"; do
    IMAGE_SIZE=$(identify -format '%w %h' "$IMAGE_FILE")

    IMAGE_WIDTH=$(echo $IMAGE_SIZE | awk {'print $1'})
    IMAGE_HEIGHT=$(echo $IMAGE_SIZE | awk {'print $2'})

    if (($IMAGE_WIDTH < 1024 && $IMAGE_HEIGHT < 1024)); then
        echo "$IMAGE_FILE is $IMAGE_WIDTH x $IMAGE_HEIGHT"
        # TODO - DELETE FILE HERE
    fi
done

 

~edit: Rather than delete the files, another idea might be to create a directory based on the resolution and move the files there. This way you can at least examine them before deletion.

 

~edit: #2 This rounds the image size to the nearest multiple of 32, then moves it into the appropriate directory/bucket

Spoiler
#!/bin/sh

IMAGE_PATH=.
IMAGE_TYPE=*.png
NEAREST_MULTIPLE=32

HALF_MULTIPLE=$((NEAREST_MULTIPLE / 2))
find "$IMAGE_PATH" -type f -name "$IMAGE_TYPE" | while read "IMAGE_FILE"; do
    IMAGE_SIZE=$(identify -format '%w %h' "$IMAGE_FILE")

    # Separate width and height
    IMAGE_WIDTH=$(echo $IMAGE_SIZE | awk {'print $1'})
    IMAGE_HEIGHT=$(echo $IMAGE_SIZE | awk {'print $2'})

    # Round to nearest
    (( BUCKET_WIDTH = (IMAGE_WIDTH + HALF_MULTIPLE) / NEAREST_MULTIPLE, BUCKET_WIDTH *=NEAREST_MULTIPLE ))
    (( BUCKET_HEIGHT = (IMAGE_HEIGHT + HALF_MULTIPLE) / NEAREST_MULTIPLE, BUCKET_HEIGHT *=NEAREST_MULTIPLE ))

    OUTPUT_PATH="restore/${BUCKET_WIDTH}x${BUCKET_HEIGHT}"
    SOURCE_NAME=$(basename "${IMAGE_FILE}")

    echo "copy $SOURCE_NAME to $OUTPUT_PATH"
    mkdir -p "$OUTPUT_PATH"
    mv "$IMAGE_FILE" "$OUTPUT_PATH"
done

 

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

On 1/1/2024 at 6:44 PM, TechEnjoyer said:

File does not exist: /input
 

We use /input as a placeholder for your actual input file path in the example above. Looks like you did a blind copy & paste without changing the input file path placeholder to a real file path.

 

 

e.g. use real path . to process files in the current working directory:

filebot -find . --filter "ext == /png/ && height == 1024"

e.g. use real path /home/TechEnjoyer/MinecraftTexturePacks to process files in that directory:

filebot -find "/home/TechEnjoyer/MinecraftTexturePacks" --filter "ext == /png/ && height == 1024"

The two commands here will list files (and not delete files) so run those first, see if it looks good, then add -exec rm -v {} at the end as seen in my previous post to actually delete files.

 

 

 

TIP: You'll want to add export LANG=en_US.UTF-8 to your .profile (the script code that is automatically called by your shell on startup) if you want your shell (and programs executed by your shell) to work with UTF-8 as file system encoding. Your environment is set to ASCII file system encoding, so you will run into trouble when you encounter non-ASCII file paths. Read https://unix.stackexchange.com/a/667863/78608 for details.

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 months later...

I am very sorry @rednoah and @Eigenvektor for not responding. I really appreciated your help, unfortunately I was very busy the last few months and forgot to reply. Fortunately, I found a backup of the HDD so I didn’t have to do it in the end. And again I am very great full for your help.
 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×