安装图片处理软件
yum install ImageMagick
缩小图片50%的大小:
find ./ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +50k -exec convert -resize 50%x50% {} {} \;
或缩小到500*500像素:
find ./ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +50k -exec convert -resize 500x500 {} {} \;
降低图片质量:
convert -resize 500x500 -quality 75 xxx.jpg xxx.png
我想要批量统一所有图片尺寸到50K-100k。
find ./ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +800k -exec convert -resize 25%x25% {} {} \;
1.75M的1528号图片被压缩到147.09K。
find ./ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +200k -exec convert -resize 50%x50% {} {} \;
794.79K的2037号图片被压缩到155.23K,不符合要求。
find ./ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +1000k -exec convert -resize 20%x20% {} {} \;
1.75M 预计压缩为125.44K,实际上1.75M 的1528号图片被压缩到101.26KB。
880K 预计压缩为 35.20K,实际上888.31K的1969号图片转为了32.42K的超小图。
find ./ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +500k -exec convert -resize 30%x30% {} {} \;
find ./ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +400k -exec convert -resize 40%x40% {} {} \;
find ./ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +110k -exec convert -resize 60%x60% {} {} \;
还是写shell来解决比较方便:
rm -f goods_pic/*
cp goods_pic_big/* goods_pic/
find goods_pic/ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +1000k -exec convert -resize 20%x20% {} {} \;
find goods_pic/ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +500k -exec convert -resize 30%x30% {} {} \;
find goods_pic/ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +200k -exec convert -resize 45%x45% {} {} \;
find goods_pic/ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +120k -exec convert -resize 70%x70% {} {} \;
缺点就是个别图片(png失真会比较明显)会发生失真情况,和但大部分还是能用的。