2008年2月16日星期六

把bmp和png圖像轉換成jpg

寫了個小Script
是nautilus用的
可以把bmp和png格式的圖片轉換成70%質量的jpg
完全是因為壓縮比的關系
使用很簡單,儲存成XXX.sh
然後放到$HOME/.gnome2/nautilus-script/下面即可
在nautilus裏選取需要轉換的圖片(可以選好幾張)
右鍵 Script -> XXX.sh 即可
………………………………

#!/bin/bash
# This Script can convert the images which in png format or bmp format to jpg format with 70% quality
# User can use this Script to Compress the png images or bmp images to the jpg images which has a smaller size, but the jpg format images may not as clear as the original format images

#for i in $1
# get all files' path
# note: the file name can NOT content spaces
for i in $NAUTILUS_SCRIPT_SELECTED_URIS
do
# get the file name without 'file://'
imgne=`echo $i | cut -c8-`
echo $imgne
#zenity --info --text=$imgne
# get the position of the suffix of the file name
declare -i imnw=`echo $imgne | wc -m`-5
declare -i imnww=$imnw+1
# get the file name without suffix
imgnen=`echo $imgne | cut -c-$imnw`
# get the suffix of the file name
iinn=`echo $imgne | cut -c$imnww-`
# check the suffix is png or bmp
if [ "$iinn" = ".png" ]||[ "$iinn" = ".bmp" ]
then
# check that is any jpg file has the same file anme
# if yes, put a number after original file name
declare -i a=0
b='1'
while [ "$b" -eq "1" ]
do
a=$a+1
if [ -e $imgnen".jpg" ]
then
imgnen="$oimgnen""_"`echo $a`
else
b='0'
fi
done
# try to convert the image, and get the error message
mesg=`convert $imgne -format jpg -quality 70 $imgnen".jpg" 2>&1`
if [ "$?" = "0" ]
then
# if it is successful to convert, remove the original image
mv $imgne "$HOME"/.Trash/
else
# if it is not successful, print out an error message
zenity --info --text="[$imgne]Convert Not success!!!\n---\n$mesg"
fi
else
# if the input file is not a support format
zenity --info --text="Image format does NOT support!!!"
fi
done


還有一個Script是壓縮png的
因為mplayer截圖是沒壓縮過的png,太大了
所以要壓一下,但貌似壓縮比不算太過
我用來壓一張網上的圖,原來四百多K。壓完後變了八百多OTL
………………………………

#!/bin/bash
# This Script can compress the png format images
# Because png format is non-lossy compression format, so it can compress to a smaller size without any loss
# but it depend the compression level of the original image
# the highest compression level is 9
# User also can use this script to compress bmp images to png images
# it will NOT loss any details of the original image and the quality is same
# This script also has more one function, that is convert pdf file to png images
# because jpg format is lossy compression algorithm, the words in pdf file will NOT as clear as the original, but pdf does NOT have this problem.

#for i in $1
# get all files' path
# note: the file name can NOT content spaces
for i in $NAUTILUS_SCRIPT_SELECTED_URIS
do
# get the file name without 'file://'
imgne=`echo $i | cut -c8-`
echo $imgne
#zenity --info --text=$imgne
# get the position of the suffix of the file name
declare -i imnw=`echo $imgne | wc -m`-5
declare -i imnww=$imnw+1
# get the file name without suffix
imgnen=`echo $imgne | cut -c-$imnw`
# get the suffix of the file name
iinn=`echo $imgne | cut -c$imnww-`
# check the suffix to decide which command should use
# because png images no need to change the file name, but other images should change the suffix to png
# this is png images
if [ "$iinn" == ".png" ]
then
# try to convert the image, and get the error message
mesg=`convert $imgne -format png -quality 9 $imgne 2>&1`
if [ "$mesg" == "" ]
then
# if it is successful to convert, remove the original image
#mv $imgne "$HOME"/.Trash/
echo "Compress Finished!!!"
else
# if it is not successful, print out an error message
zenity --info --text="[$imgne]Convert Not success!!!\n---\n$mesg"
fi
# this is images which is other format
elif [ "$iinn" == ".bmp" ]||[ "$iinn" == ".pdf" ]
then
# try to convert the image, and get the error message
mesg=`convert $imgne -format png -quality 9 $imgnen".png" 2>&1`
# if the image file is NOT pdf, it should be remove
if [ "$mesg" == '' ]
then
# if it is successful to convert, remove the original image
if [ "$iinn" != ".pdf" ]
then
mv $imgne "$HOME"/.Trash/
fi
echo "Compress Finished!!!"
else
# if it is not successful, print out an error message
zenity --info --text="[$imgne]Convert Not success!!!\n---\n$mesg"
fi
else
# if the input file is not a support format
zenity --info --text="Image format does NOT support!!!"
fi
done
發佈留言

熱門文章