Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ImageMagick実践入門 画像加工サーバを作ってみよう / ImageMagick an...

ImageMagick実践入門 画像加工サーバを作ってみよう / ImageMagick and image server

新卒エンジニア研修の座学でImageMagickの話をしました。

Hiroshi Shimoju

October 29, 2017
Tweet

More Decks by Hiroshi Shimoju

Other Decks in Technology

Transcript

  1. ͱΓ͋͑ͣΠϯετʔϧ $ brew install imagemagick OR $ sudo apt-get install

    -y imagemagick OR $ sudo yum -y install ImageMagick
  2. *NBHF.BHJDL 6CVOUV $ ls -Alh /usr/lib/x86_64-linux-gnu/ImageMagick-6.8.9/bin-Q16 total 88K -rwxr-xr-x 1

    root root 6.3K Jul 31 13:22 animate -rwxr-xr-x 1 root root 6.3K Jul 31 13:22 compare -rwxr-xr-x 1 root root 6.3K Jul 31 13:22 composite -rwxr-xr-x 1 root root 6.3K Jul 31 13:22 conjure -rwxr-xr-x 1 root root 6.3K Jul 31 13:22 convert -rwxr-xr-x 1 root root 6.3K Jul 31 13:22 display -rwxr-xr-x 1 root root 6.3K Jul 31 13:22 identify -rwxr-xr-x 1 root root 6.3K Jul 31 13:22 import -rwxr-xr-x 1 root root 6.3K Jul 31 13:22 mogrify -rwxr-xr-x 1 root root 6.3K Jul 31 13:22 montage -rwxr-xr-x 1 root root 6.3K Jul 31 13:22 stream
  3. *NBHF.BHJDL .BD)PNFCSFX $ ls -Alh /usr/local/Cellar/imagemagick/7.0.7-7/bin | grep -v config

    total 64 lrwxr-xr-x 1 shimoju admin 6B 10 7 21:02 animate -> magick lrwxr-xr-x 1 shimoju admin 6B 10 7 21:02 compare -> magick lrwxr-xr-x 1 shimoju admin 6B 10 7 21:02 composite -> magick lrwxr-xr-x 1 shimoju admin 6B 10 7 21:02 conjure -> magick lrwxr-xr-x 1 shimoju admin 6B 10 7 21:02 convert -> magick lrwxr-xr-x 1 shimoju admin 6B 10 7 21:02 display -> magick lrwxr-xr-x 1 shimoju admin 6B 10 7 21:02 identify -> magick lrwxr-xr-x 1 shimoju admin 6B 10 7 21:02 import -> magick -r-xr-xr-x 1 shimoju admin 18K 10 9 18:25 magick lrwxr-xr-x 1 shimoju admin 6B 10 7 21:02 magick-script -> magick lrwxr-xr-x 1 shimoju admin 6B 10 7 21:02 mogrify -> magick lrwxr-xr-x 1 shimoju admin 6B 10 7 21:02 montage -> magick lrwxr-xr-x 1 shimoju admin 6B 10 7 21:02 stream -> magick
  4. JEFOUJGZ $ identify *.jpg original.jpg JPEG 800x533 800x533+0+0 8-bit sRGB

    104823B 0.000u 0:00.000 resize.jpg JPEG 400x267 400x267+0+0 8-bit sRGB 37116B 0.000u 0:00.000 ϑΥʔϚοτจࣈྻ͸͜͜Β΁Μࢀরɿhttps://www.imagemagick.org/script/escape.php $ identify -format '{"width": %w, "height": %h}' *.jpg | jq { "width": 800, "height": 533 } $ identify -format '%wx%h %f\n' *.jpg | grep -v 800x533 200x200 crop.jpg
  5. ճస SPUBUF ճస $ convert -rotate 90 original.jpg rotate.jpg ্Լ൓స

    $ convert -flip original.jpg flip.jpg ࠨӈ൓స $ convert -flop original.jpg flop.jpg ্Լࠨӈ൓స $ convert -flip -flop original.jpg flipflop.jpg
  6. αϯϓϧ TBNQMF ϐΫηϧΛؒҾ͘ $ convert -sample 10% original.jpg sample.jpg 10%ʹͳΔΑ͏ʹϐΫηϧΛؒҾ͍ͨ͋ͱɺ1000%ʹͳΔΑ͏ʹ֦େ

    →ݩը૾ͱಉ͡αΠζͰϞβΠΫ͕͔͔Δ $ convert -sample 10% -sample 1000% original.jpg sample.jpg
  7. ϦαΠζ SFTJ[F σϑΥϧτͰ͸ΞεϖΫτൺΛม͑ͳ͍ɿࢦఆͨ͠෯ɾߴ͞ʹऩ·ΔΑ͏ʹϦαΠζ͞ΕΔ $ convert -resize 400x400 original.jpg resize.jpg $

    identify resize.jpg resize.jpg JPEG 400x267 400x267+0+0 8-bit sRGB 37116B 0.000u 0:00.000 !Λ͚ͭΔͱΞεϖΫτൺΛແࢹͯ͠ࢦఆͨ͠஋ʹϦαΠζ͢Δ $ convert -resize 400x400! original.jpg resize2.jpg ෯·ͨ͸ߴ͞ͷΈࢦఆͰ͖Δ $ convert -resize 400x original.jpg resize3.jpg
  8. Τοδݕग़ FEHF ෆ࿈ଓʹมԽ͍ͯ͠ΔՕॴΛݕग़͢Δ $ convert -edge 5 original.jpg edge.jpg ஋ΛมԽͤͯ͞ΈΑ͏

    $ convert -edge 10 original.jpg edge.jpg $ convert -edge 1 original.jpg edge.jpg
  9. ੾Γൈ͖ DSPQ -gravityͰج४఺Λࢦఆ -crop widthxheightͰ੾Γൈ͘αΠζΛࢦఆ +/-Ͱج४఺͔Βͷx,y࠲ඪΛࢦఆ $ convert -gravity center

    -crop 200x200+0+0 original.jpg crop.jpg ը૾ӈ্Λج४ʹɺxʹ140px,yʹ50pxҠಈ͠ɺͦͷ఺͔Β200x200px੾Γൈ͘ $ convert -gravity northeast -crop 200x200+140+50 original.jpg crop.jpg
  10. ృΓ଍͠ FYUFOU ࢦఆͨ͠αΠζʹͳΔΑ͏ʹ༨നΛ௥Ճ͢Δ ༨നͷ৭͸-backgroundͰࢦఆͰ͖Δ ਖ਼ํܗͷαΠζ͕ඞཁͳͷʹ4:3ͷը૾͔͠ͳ͍ͱ͔ʹศར $ convert -background black -gravity

    center \ -extent 800x800 original.jpg extent.jpg PNG(ಁա͕ѻ͑ΔϑΥʔϚοτ)Ͱ͋Ε͹transparentͰಁաͰ͖Δ $ convert -background transparent -gravity north \ -extent 1000x1000 original.jpg extent.png
  11. ͦͷલʹจࣈͷը૾Λ࡞͓ͬͯ͜͏ $ convert -background transparent \ -fill '#ff6060' -font Arial

    -pointsize 128 label:LGTM lgtm.png ࢦఆαΠζͰ࡞੒ $ convert -size 400x200 -gravity center -background transparent \ -fill '#ff6060' -font Arial -pointsize 128 label:LGTM lgtm.png
  12. ߹੒ DPNQPTJUF original.jpgͷ্ʹlgtm.pngΛ߹੒ͯ͠ɺcompose-over.jpgͱͯ͠ग़ྗ $ convert original.jpg lgtm.png -gravity center \

    -compose over -composite compose-over.jpg -geometryͰج४఺͔ΒҠಈ $ convert original.jpg lgtm.png -gravity center -geometry +150+50 \ -compose over -composite compose-over.jpg
  13. ඳըϞʔυ CMFOENPEF -composeͰඳըϞʔυΛࢦఆ͢Δ ৐ࢉ (multiply) $ convert original.jpg lgtm.png -gravity

    center -geometry +150+50 \ -compose multiply -composite compose-multiply.jpg ΦʔόʔϨΠ (overlay) $ convert original.jpg lgtm.png -gravity center -geometry +150+50 \ -compose overlay -composite compose-overlay.jpg ݮࢉ (subtract) $ convert original.jpg lgtm.png -gravity center -geometry +150+50 \ -compose subtract -composite compose-subtract.jpg