ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 리눅스에서 이미지 사이즈 줄이기
    프로그래밍/Linux 2022. 9. 9. 16:11
    반응형

     

    리눅스(ubuntu)에서 앱에 사용되는 이미지 폴더 사이즈가 너무 커지면서 

    imagemagick 툴을 사용해서 이미지 사이즈를 줄여보기로 했다. 

     

    설치

    sudo apt install imagemagick 

     

    실행

    convert 명령을 사용해서 크기를 조정할 수 있다. 

     

     convert [input-option] input-file [output-option] output-file

     

    입력파일과 출력파일은 필수이니 참고하자. 

    처음에 출력파일을 안쓰니 아래와 같은 에러메세지가 나오는데 출력 파일 관련 오류인지 모르고 한참 검색했다(...)

     

    $ convert -resize 20% 2533.jpg
    .convert.bin: no images defined `2533.jpg' @ error/convert.c/ConvertImageCommand/3258.

     

     

    convert --resize 옵션을 이용하여  파일의 크기를 원래 크기의 20%로 변경하였다. 

    -rw-r--r-- 1 45956 Sep  8 21:59 2533.jpg
    
    convert -resize 20% 2533.jpg 2533.jpg
    
    -rw-r--r-- 1 6218 Sep  9 15:41 2533.jpg

     

     

     

    사진 포맷 변경도 가능하다. png 파일을 jpg 파일로 변경하기만 해도 파일의 사이즈가 확 줄어든다.  

     

    mogrify -format jpg \*.PNG  

    파일 사이즈가 11M->411K, 600K->29K로 줄어든 것을 확인할 수 있다. 

    -rw-rw-r-- 1 bi 411K Feb 19 21:02 5_large.jpg
    -rw-r--r-- 1 bi  11M Feb 19 20:31 5_large.png
    -rw-rw-r-- 1 bi  29K Feb 19 21:02 5_small.jpg
    -rw-rw-r-- 1 bi 600K Feb 19 20:32 5_small.png
    

     

     

    폴더 전체의 이미지 사이즈를 줄이기 위해서는 아래와 같이 shell script를 사용한다. 

     

    우선 이미지 줄이기 전 폴더 사이즈 

    $ du -sh *
    134M    food_pictures

     

    1M가 넘는 이미지만 사이즈를 줄일 경우는 아래와 같이 스크립트를 작성하였다. 

     

    #!/bin/bash
    
    for img in $(find /home/images/ -type f -size +1M)
    do
        echo $img
        convert -resize 50% "$img" "$img"
        sleep 1
    done

     

    아래는 사이즈 조건 없이 전체 파일을 처리할 경우이다. 

    #!/bin/bash
    
    # Catch user input for file type.
    
    echo "Enter the file extension for your image files:"
    
    # Store user input in $files.
    
    read files
    
    # Resize images.
    
    for img in *.$files; do
    convert -resize 20% "$img" "resize-$img"
    done

     

     

    참고 : genuine-lamps.com/ko/linux/4502-how-to-resize-image-files-on-linux.html

     

     

     

     

     

     

     

     

     

     

     

    728x90
    반응형
Designed by Tistory.