Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: How can I batch convert images to b/w while preserving the folder structure

  1. #1
    Join Date
    Apr 2010
    Beans
    261

    Exclamation How can I batch convert images to b/w while preserving the folder structure

    I want to batch process images but I have a very specific task that I want to do

    1. I do not want to change image type
    2. I want to make them black and white
    3. I want it to create/preserve images and sub folder structure


    what I mean by 3

    what I want is this for example main folder "F1" contains 4 different sub folders "2003" "2004" "2005" and "2006" and all of this sub folders Constantine 100-120 files
    so what I want is the new black and white files have the same structure as they where I do not care if it will overwrite or create a new folder all I care is them to be black and white and be in the same folders as they were for example if williams 2003.png was in "2003" I want the new file to be in "2003" as well



    I did this in Photoshop but it did not preserve folders and sub folder content it just threw every converted file in one directory.
    My only hope is Linux
    Thank you in advance!
    Last edited by Chelidze; May 10th, 2013 at 10:54 PM.

  2. #2
    Join Date
    Aug 2011
    Beans
    334
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: How can I batch convert images to b/w while preserving the folder structure

    Look at ImageMagik's convert utility. If you are dealing with images I suspect you really want 'grayscale', not 'pure black and white'. The general form of the command would be 'convert <input> -colorspace Gray <output>. Of course it can be a whole lot more complicated if you wish, and batch processing is also possible. See the ImageMagick web site for more detailed instructions and example commands. You might have to do each directory separately, I'm not sure of that, but I do know you can batch process and entire directory and put the results anywhere you wish.

  3. #3
    Join Date
    Apr 2010
    Beans
    261

    Re: How can I batch convert images to b/w while preserving the folder structure

    Here is the solution

    Code:
    for img in $(find . -iname '*.png'); do echo -n "Converting $img"; convert -colorspace GRAY $img $img && echo ' [Done]'; done
    http://askubuntu.com/a/293713/42591


    thank you agillator I will give that a shot as well

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,399
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How can I batch convert images to b/w while preserving the folder structure

    Code:
    #!/bin/bash
    
    # source and destination dirs
    srcdir="$HOME/source"  
    destdir="$HOME/grayscale"
    
    while read -rd $'\0' f
    do
      echo "$f"
      fdir=${f%/*}
      mkdir -p "${fdir/#$srcdir/$destdir}"
      convert "$f" -set colorspace Gray -separate -average "${f/#$srcdir/$destdir}"
    done < <( find "$srcdir" -regextype posix-extended -iregex '.*(png|bmp|jpe?g|tga|gif|tiff)$' -print0 )
    your solution sort of sucks because it will fail in case of space-filled filenames (never do this: for x in <output of some command>) and it does only PNGs
    Also it's not too safe to perform irreversible operations, it's too easy to damage/destroy original data.
    Last edited by Vaphell; May 10th, 2013 at 11:05 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Apr 2010
    Beans
    261

    Re: How can I batch convert images to b/w while preserving the folder structure

    Quote Originally Posted by Vaphell View Post
    Code:
    #!/bin/bash
    
    # source and destination dirs
    srcdir="$HOME/source"  
    destdir="$HOME/grayscale"
    
    while read -rd $'\0' f
    do
      echo "$f"
      fdir=${f%/*}
      mkdir -p "${fdir/#$srcdir/$destdir}"
      convert "$f" -set colorspace Gray -separate -average "${f/#$srcdir/$destdir}"
    done < <( find "$srcdir" -regextype posix-extended -iregex '.*(png|bmp|jpe?g|tga|gif|tiff)$' -print0 )
    your solution sort of sucks because it will fail in case of space-filled filenames and it does only PNGs
    Also it's not too safe to perform irreversible operations, it's too easy to damage/destroy original data.

    Honestly I have no idea if that is a god solution or a bad one I am not that good in linux

    I the solution I provided is not mine I linked the askubuntu page where that person helped me solve my problem and my I wanted to convert png files you can see there but if you tell me how to use your commend that will be a blast

  6. #6
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: How can I batch convert images to b/w while preserving the folder structure

    Please check out BashPitfalls 001 (link in my signature). That command will fail if the files/directories have white spaces in their names.

    You could use the -execdir action of the GNU/find command:
    Code:
    find ./path/to/dir -iname '*.png' -type f -execdir sh -c 'convert -colorspace GRAY $1 $1' _ {} \;
    If you don't want to overwrite the original files, then try something like:
    Code:
    find ./path/to/dir -iname '*.png' -type f -execdir sh -c 'convert -colorspace GRAY $1 ${1%.???}.bw.png' _ {} \;

  7. #7
    Join Date
    Apr 2010
    Beans
    261

    Re: How can I batch convert images to b/w while preserving the folder structure

    Quote Originally Posted by sisco311 View Post
    Please check out BashPitfalls 001 (link in my signature). That command will fail if the files/directories have white spaces in their names.

    You could use the -execdir action of the GNU/find command:
    Code:
    find ./path/to/dir -iname '*.png' -type f -execdir sh -c 'convert -colorspace GRAY $1 $1' _ {} \;
    If you don't want to overwrite the original files, then try something like:
    Code:
    find ./path/to/dir -iname '*.png' -type f -execdir sh -c 'convert -colorspace GRAY $1 ${1%.???}.bw.png' _ {} \;

    wow you people are like mad scientists, you do waowowa and write some commends and the result is awesome one question your second commend will it create a saperate directory or in the original directory after the process will be done I will have the original file and the black and white file ??

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,399
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How can I batch convert images to b/w while preserving the folder structure

    if i read it correctly it will create a bw copy next to the original file
    my script does separate dir with recreated structure, at least in theory (worked fine on my test files).

    you simply save the code as a text file named as you wish
    and then run it with bash file
    or if you made the file executable with chmod +x file : ./file
    Last edited by Vaphell; May 10th, 2013 at 11:22 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  9. #9
    Join Date
    Apr 2010
    Beans
    261

    Re: How can I batch convert images to b/w while preserving the folder structure

    Quote Originally Posted by Vaphell View Post
    if i read it correctly it will create a copy next to the original file
    my script does separate dir with recreated structure, at least in theory.
    I really do not want to bother you with a question like this, but I am relatively new ubuntu user

    should I "cd" in the maine directory and then past you commend in the terminal
    This full thing I am asking because I never seen such a commend befor it is tall commends that I have used were alwates long not tall

    #!/bin/bash# source and destination dirssrcdir="$HOME/source" destdir="$HOME/grayscale"while read -rd $'\0' fdo echo "$f" fdir=${f%/*} mkdir -p "${fdir/#$srcdir/$destdir}" convert "$f" -set colorspace Gray -separate -average "${f/#$srcdir/$destdir}"done < <( find "$srcdir" -regextype posix-extended -iregex '.*(png|bmp|jpe?g|tga|gif|tiff)$' -print0 )

  10. #10
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,399
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How can I batch convert images to b/w while preserving the folder structure

    i updated my earlier post but i'll repeat myself
    the code is a script, you need to save the code as a text file, just paste it into text editor and save
    and then run the script
    bash name_you_have_chosen
    obviously change srcdir and destdir to suit your needs
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •