Find images in a Linux directory based on their resolution












7















I would like to scan all images in a directory (recursively within sub-folders), and find those with resolution higher than a specific threshold (e.g. say those with resolutions at least 800x600 or if easier, say with width higher than 1000 pixels). Then I would like to log their address in a text file, accompanying their resolution (or [width], [height] for a better formatting).



So log.txt would look like this:



/home/users/myuser/test/image1.jpg, 1800, 1600
/home/users/myuser/test/image20.jpg, 2800, 2600
/home/users/myuser/test/image30.jpg, 1500, 1200


How can I do that that using a bash script? I have to scan millions of images.










share|improve this question




















  • 2





    also asked on superuser: superuser.com/q/1333517/4714

    – glenn jackman
    Jun 22 '18 at 20:38
















7















I would like to scan all images in a directory (recursively within sub-folders), and find those with resolution higher than a specific threshold (e.g. say those with resolutions at least 800x600 or if easier, say with width higher than 1000 pixels). Then I would like to log their address in a text file, accompanying their resolution (or [width], [height] for a better formatting).



So log.txt would look like this:



/home/users/myuser/test/image1.jpg, 1800, 1600
/home/users/myuser/test/image20.jpg, 2800, 2600
/home/users/myuser/test/image30.jpg, 1500, 1200


How can I do that that using a bash script? I have to scan millions of images.










share|improve this question




















  • 2





    also asked on superuser: superuser.com/q/1333517/4714

    – glenn jackman
    Jun 22 '18 at 20:38














7












7








7


1






I would like to scan all images in a directory (recursively within sub-folders), and find those with resolution higher than a specific threshold (e.g. say those with resolutions at least 800x600 or if easier, say with width higher than 1000 pixels). Then I would like to log their address in a text file, accompanying their resolution (or [width], [height] for a better formatting).



So log.txt would look like this:



/home/users/myuser/test/image1.jpg, 1800, 1600
/home/users/myuser/test/image20.jpg, 2800, 2600
/home/users/myuser/test/image30.jpg, 1500, 1200


How can I do that that using a bash script? I have to scan millions of images.










share|improve this question
















I would like to scan all images in a directory (recursively within sub-folders), and find those with resolution higher than a specific threshold (e.g. say those with resolutions at least 800x600 or if easier, say with width higher than 1000 pixels). Then I would like to log their address in a text file, accompanying their resolution (or [width], [height] for a better formatting).



So log.txt would look like this:



/home/users/myuser/test/image1.jpg, 1800, 1600
/home/users/myuser/test/image20.jpg, 2800, 2600
/home/users/myuser/test/image30.jpg, 1500, 1200


How can I do that that using a bash script? I have to scan millions of images.







command-line bash search find image-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 14 at 17:32









Zanna

50.4k13133241




50.4k13133241










asked Jun 22 '18 at 19:11









Tina JTina J

179212




179212








  • 2





    also asked on superuser: superuser.com/q/1333517/4714

    – glenn jackman
    Jun 22 '18 at 20:38














  • 2





    also asked on superuser: superuser.com/q/1333517/4714

    – glenn jackman
    Jun 22 '18 at 20:38








2




2





also asked on superuser: superuser.com/q/1333517/4714

– glenn jackman
Jun 22 '18 at 20:38





also asked on superuser: superuser.com/q/1333517/4714

– glenn jackman
Jun 22 '18 at 20:38










1 Answer
1






active

oldest

votes


















7














Via bash's recursive glob and ImageMagick's identify command:



shopt -s globstar
identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg}


Saving such output to file , is just a matter of adding > mylog.txt to previous command, that is



identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg} > mylog.txt


From there, you could use awk or perl to compare mylog.txt columns



awk -F ',' '$2 > 800 && $3 > 600' mylog.txt


awk here uses , as separator for columns, and the usual structure for awk is /PATTERN/{COMMANDS}, which defaults to just printing if {COMMANDS} omitted ; in the particular example above, if the pattern $2 > 800 && $3 > 600 is true, that is it's the image above desired resolution, you'll get it printed to the screen.



And probably skipping the log step in between, it would be a little better to just pipe everything:



shopt -s globstar
identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg} | awk -F ',' '$2 > 800 && $3 > 600' > filtered_images.txt




In case you encounter arguments list too long error, typically find command is better approach for recursively walking the directory tree. The identify can be called through find's -exec flag, and filtering still can be handled by awk:



$ find -type f -regex "^.*.(png|jpg|jpeg)$" -exec identify -format "%f, %w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600' 
fanart.jpg, 1920, 1080
fanart.jpg, 1920, 1080
globalsearch-background.jpg, 1920, 1080
fanart.jpg, 1280, 720


As usual, don't forget to add > log2.txt to save everything to file.



Full path of to the file could be handled in either one of two ways. One, by specifying %d/%f in identify command's format string, or use find's -printf option. That is either



find -type f -regex "^.*.(png|jpg|jpeg)$" -exec identify -format "%d/%f, %w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600'


Or



find -type f -regex "^.*.(png|jpg|jpeg)$" -printf "%p, " -exec identify -format "%w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600'





share|improve this answer





















  • 1





    @TinaJ Yep, or as I just added to my answer, you can just pipe everything from identify to awk, and save on creating the log1.txt in between.

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 19:23






  • 1





    @TinaJ Not anymore :) Edited to include that as well

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 19:30






  • 1





    @TinaJ Alright. I'll add a slightly different approach then in a minute, alright ?

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:31






  • 1





    @TinaJ See the edit :)

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:35






  • 1





    @TinaJ Very welcome :)

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:41











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1048914%2ffind-images-in-a-linux-directory-based-on-their-resolution%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









7














Via bash's recursive glob and ImageMagick's identify command:



shopt -s globstar
identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg}


Saving such output to file , is just a matter of adding > mylog.txt to previous command, that is



identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg} > mylog.txt


From there, you could use awk or perl to compare mylog.txt columns



awk -F ',' '$2 > 800 && $3 > 600' mylog.txt


awk here uses , as separator for columns, and the usual structure for awk is /PATTERN/{COMMANDS}, which defaults to just printing if {COMMANDS} omitted ; in the particular example above, if the pattern $2 > 800 && $3 > 600 is true, that is it's the image above desired resolution, you'll get it printed to the screen.



And probably skipping the log step in between, it would be a little better to just pipe everything:



shopt -s globstar
identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg} | awk -F ',' '$2 > 800 && $3 > 600' > filtered_images.txt




In case you encounter arguments list too long error, typically find command is better approach for recursively walking the directory tree. The identify can be called through find's -exec flag, and filtering still can be handled by awk:



$ find -type f -regex "^.*.(png|jpg|jpeg)$" -exec identify -format "%f, %w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600' 
fanart.jpg, 1920, 1080
fanart.jpg, 1920, 1080
globalsearch-background.jpg, 1920, 1080
fanart.jpg, 1280, 720


As usual, don't forget to add > log2.txt to save everything to file.



Full path of to the file could be handled in either one of two ways. One, by specifying %d/%f in identify command's format string, or use find's -printf option. That is either



find -type f -regex "^.*.(png|jpg|jpeg)$" -exec identify -format "%d/%f, %w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600'


Or



find -type f -regex "^.*.(png|jpg|jpeg)$" -printf "%p, " -exec identify -format "%w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600'





share|improve this answer





















  • 1





    @TinaJ Yep, or as I just added to my answer, you can just pipe everything from identify to awk, and save on creating the log1.txt in between.

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 19:23






  • 1





    @TinaJ Not anymore :) Edited to include that as well

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 19:30






  • 1





    @TinaJ Alright. I'll add a slightly different approach then in a minute, alright ?

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:31






  • 1





    @TinaJ See the edit :)

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:35






  • 1





    @TinaJ Very welcome :)

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:41
















7














Via bash's recursive glob and ImageMagick's identify command:



shopt -s globstar
identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg}


Saving such output to file , is just a matter of adding > mylog.txt to previous command, that is



identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg} > mylog.txt


From there, you could use awk or perl to compare mylog.txt columns



awk -F ',' '$2 > 800 && $3 > 600' mylog.txt


awk here uses , as separator for columns, and the usual structure for awk is /PATTERN/{COMMANDS}, which defaults to just printing if {COMMANDS} omitted ; in the particular example above, if the pattern $2 > 800 && $3 > 600 is true, that is it's the image above desired resolution, you'll get it printed to the screen.



And probably skipping the log step in between, it would be a little better to just pipe everything:



shopt -s globstar
identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg} | awk -F ',' '$2 > 800 && $3 > 600' > filtered_images.txt




In case you encounter arguments list too long error, typically find command is better approach for recursively walking the directory tree. The identify can be called through find's -exec flag, and filtering still can be handled by awk:



$ find -type f -regex "^.*.(png|jpg|jpeg)$" -exec identify -format "%f, %w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600' 
fanart.jpg, 1920, 1080
fanart.jpg, 1920, 1080
globalsearch-background.jpg, 1920, 1080
fanart.jpg, 1280, 720


As usual, don't forget to add > log2.txt to save everything to file.



Full path of to the file could be handled in either one of two ways. One, by specifying %d/%f in identify command's format string, or use find's -printf option. That is either



find -type f -regex "^.*.(png|jpg|jpeg)$" -exec identify -format "%d/%f, %w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600'


Or



find -type f -regex "^.*.(png|jpg|jpeg)$" -printf "%p, " -exec identify -format "%w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600'





share|improve this answer





















  • 1





    @TinaJ Yep, or as I just added to my answer, you can just pipe everything from identify to awk, and save on creating the log1.txt in between.

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 19:23






  • 1





    @TinaJ Not anymore :) Edited to include that as well

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 19:30






  • 1





    @TinaJ Alright. I'll add a slightly different approach then in a minute, alright ?

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:31






  • 1





    @TinaJ See the edit :)

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:35






  • 1





    @TinaJ Very welcome :)

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:41














7












7








7







Via bash's recursive glob and ImageMagick's identify command:



shopt -s globstar
identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg}


Saving such output to file , is just a matter of adding > mylog.txt to previous command, that is



identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg} > mylog.txt


From there, you could use awk or perl to compare mylog.txt columns



awk -F ',' '$2 > 800 && $3 > 600' mylog.txt


awk here uses , as separator for columns, and the usual structure for awk is /PATTERN/{COMMANDS}, which defaults to just printing if {COMMANDS} omitted ; in the particular example above, if the pattern $2 > 800 && $3 > 600 is true, that is it's the image above desired resolution, you'll get it printed to the screen.



And probably skipping the log step in between, it would be a little better to just pipe everything:



shopt -s globstar
identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg} | awk -F ',' '$2 > 800 && $3 > 600' > filtered_images.txt




In case you encounter arguments list too long error, typically find command is better approach for recursively walking the directory tree. The identify can be called through find's -exec flag, and filtering still can be handled by awk:



$ find -type f -regex "^.*.(png|jpg|jpeg)$" -exec identify -format "%f, %w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600' 
fanart.jpg, 1920, 1080
fanart.jpg, 1920, 1080
globalsearch-background.jpg, 1920, 1080
fanart.jpg, 1280, 720


As usual, don't forget to add > log2.txt to save everything to file.



Full path of to the file could be handled in either one of two ways. One, by specifying %d/%f in identify command's format string, or use find's -printf option. That is either



find -type f -regex "^.*.(png|jpg|jpeg)$" -exec identify -format "%d/%f, %w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600'


Or



find -type f -regex "^.*.(png|jpg|jpeg)$" -printf "%p, " -exec identify -format "%w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600'





share|improve this answer















Via bash's recursive glob and ImageMagick's identify command:



shopt -s globstar
identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg}


Saving such output to file , is just a matter of adding > mylog.txt to previous command, that is



identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg} > mylog.txt


From there, you could use awk or perl to compare mylog.txt columns



awk -F ',' '$2 > 800 && $3 > 600' mylog.txt


awk here uses , as separator for columns, and the usual structure for awk is /PATTERN/{COMMANDS}, which defaults to just printing if {COMMANDS} omitted ; in the particular example above, if the pattern $2 > 800 && $3 > 600 is true, that is it's the image above desired resolution, you'll get it printed to the screen.



And probably skipping the log step in between, it would be a little better to just pipe everything:



shopt -s globstar
identify -format "%f, %w, %hn" **/*.{png,jpg,jpeg} | awk -F ',' '$2 > 800 && $3 > 600' > filtered_images.txt




In case you encounter arguments list too long error, typically find command is better approach for recursively walking the directory tree. The identify can be called through find's -exec flag, and filtering still can be handled by awk:



$ find -type f -regex "^.*.(png|jpg|jpeg)$" -exec identify -format "%f, %w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600' 
fanart.jpg, 1920, 1080
fanart.jpg, 1920, 1080
globalsearch-background.jpg, 1920, 1080
fanart.jpg, 1280, 720


As usual, don't forget to add > log2.txt to save everything to file.



Full path of to the file could be handled in either one of two ways. One, by specifying %d/%f in identify command's format string, or use find's -printf option. That is either



find -type f -regex "^.*.(png|jpg|jpeg)$" -exec identify -format "%d/%f, %w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600'


Or



find -type f -regex "^.*.(png|jpg|jpeg)$" -printf "%p, " -exec identify -format "%w, %hn" {} ; | awk -F ',' '$2 > 800 && $3 > 600'






share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 22 '18 at 21:15

























answered Jun 22 '18 at 19:16









Sergiy KolodyazhnyySergiy Kolodyazhnyy

71k9147311




71k9147311








  • 1





    @TinaJ Yep, or as I just added to my answer, you can just pipe everything from identify to awk, and save on creating the log1.txt in between.

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 19:23






  • 1





    @TinaJ Not anymore :) Edited to include that as well

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 19:30






  • 1





    @TinaJ Alright. I'll add a slightly different approach then in a minute, alright ?

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:31






  • 1





    @TinaJ See the edit :)

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:35






  • 1





    @TinaJ Very welcome :)

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:41














  • 1





    @TinaJ Yep, or as I just added to my answer, you can just pipe everything from identify to awk, and save on creating the log1.txt in between.

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 19:23






  • 1





    @TinaJ Not anymore :) Edited to include that as well

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 19:30






  • 1





    @TinaJ Alright. I'll add a slightly different approach then in a minute, alright ?

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:31






  • 1





    @TinaJ See the edit :)

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:35






  • 1





    @TinaJ Very welcome :)

    – Sergiy Kolodyazhnyy
    Jun 22 '18 at 20:41








1




1





@TinaJ Yep, or as I just added to my answer, you can just pipe everything from identify to awk, and save on creating the log1.txt in between.

– Sergiy Kolodyazhnyy
Jun 22 '18 at 19:23





@TinaJ Yep, or as I just added to my answer, you can just pipe everything from identify to awk, and save on creating the log1.txt in between.

– Sergiy Kolodyazhnyy
Jun 22 '18 at 19:23




1




1





@TinaJ Not anymore :) Edited to include that as well

– Sergiy Kolodyazhnyy
Jun 22 '18 at 19:30





@TinaJ Not anymore :) Edited to include that as well

– Sergiy Kolodyazhnyy
Jun 22 '18 at 19:30




1




1





@TinaJ Alright. I'll add a slightly different approach then in a minute, alright ?

– Sergiy Kolodyazhnyy
Jun 22 '18 at 20:31





@TinaJ Alright. I'll add a slightly different approach then in a minute, alright ?

– Sergiy Kolodyazhnyy
Jun 22 '18 at 20:31




1




1





@TinaJ See the edit :)

– Sergiy Kolodyazhnyy
Jun 22 '18 at 20:35





@TinaJ See the edit :)

– Sergiy Kolodyazhnyy
Jun 22 '18 at 20:35




1




1





@TinaJ Very welcome :)

– Sergiy Kolodyazhnyy
Jun 22 '18 at 20:41





@TinaJ Very welcome :)

– Sergiy Kolodyazhnyy
Jun 22 '18 at 20:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1048914%2ffind-images-in-a-linux-directory-based-on-their-resolution%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How did Captain America manage to do this?

迪纳利

南乌拉尔铁路局