2 comments for “Converting and Resizing Images Using Python

  1. This can be done with a one liner in bash (it can be made even smaller by removing the echo statements). Here is the complete script and below is the same script in a one-liner.

    Note you would have to install imagemagick for the convert command but that is a massively useful thing to have anyway – sudo apt-get install imagemagick.

    The converted files are placed in the directory resized.
    [code]
    mkdir -p resized
    ls | while read line; do
    echo Processing $line
    a=$(file $line | grep “image data” | wc -l)
    if [ “$a” = “1” ]; then
    echo $line is an image, converting to jpeg and resizing
    convert “$line” -resize 1024x “resized/$line.jpg”
    else
    echo $line is not an image file, ignoring
    fi
    done
    [/code]

    or as a one-liner:
    [code]
    mkdir -p resized; ls | while read line; do echo Processing $line; a=$(file $line | grep “image data” | wc -l); if [ “$a” = “1” ]; then echo $line is an image, converting to jpeg and resizing; convert “$line” -resize 1024x “resized/$line.jpg”; else echo $line is not an image file, ignoring; fi; done
    [/code]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.