📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

 
(同じ利用者による、間の5版が非表示)
15行目: 15行目:
ImageMagickライブラリは高度な画像処理機能を提供する強力なツールとして活用できる。<br>
ImageMagickライブラリは高度な画像処理機能を提供する強力なツールとして活用できる。<br>
上記の例は基本的な使用方法の一部であり、さらに多くの機能や最適化オプションが用意されている。<br>
上記の例は基本的な使用方法の一部であり、さらに多くの機能や最適化オプションが用意されている。<br>
<br><br>
== ImageMagickのインストール ==
==== パッケージ管理システムからインストール ====
# RHEL
sudo dnf install ImageMagick-devel libMagick++-devel
# SUSE
sudo zypper install ImageMagick-devel libMagick++-devel
<br>
==== ソースコードからインストール ====
まず、ImageMagickのビルドに必要なライブラリをインストールする。<br>
# RHEL
# SUSE
sudo zypper install make gcc libtool libzip-devel zlib-devel libzstd-devel fontconfig-devel freetype2-devel libxml2-devel \
                    libdjvulibre-devel libraqm-devel liblcms2-devel pango-devel cairo-devel \
                    libjpeg8-devel libjxl-devel libjbig-devel openjpeg2-devel libheif-devel liblqr-devel \
                    libpng16-devel openexr-devel libtiff-devel libraw-devel libraw1394-devel libwebp-devel libwmf-devel
<br>
[https://imagemagick.org/script/install-source.php ImageMagickの公式Webサイト]または[https://github.com/ImageMagick/ImageMagick Github]にアクセスして、ソースコードをダウンロードする。<br>
ダウンロードしたファイルを解凍する。<br>
tar ImageMagick.tar.gz または tar xf ImageMagick-<バージョン>.tar.gz
cd ImageMagick-<バージョン>
<br>
ImageMagickをビルドおよびインストールする。<br>
mkdir build && cd build
../configure --prefix=<ImageMagickのインストールディレクトリ> \
              --with-modules
make -j $(nproc)
make install
<br>
~/.profileファイル等に、ImageMagickへの環境変数<code>PATH</code>等を追記する。<br>
vi ~/.profile
<br>
<syntaxhighlight lang="sh">
# ~/.profileファイル等
export PATH="/<ImageMagickのインストールディレクトリ>/bin:$PATH"
export LD_LIBRARY_PATH="<ImageMagickのインストールディレクトリ>/lib:$LD_LIBRARY_PATH"
</syntaxhighlight>
<br>
ImageMagickが正しく動作していることを確認する。<br>
magick logo: logo.gif
identify logo.gif
display logo.gif
<br>
ImageMagickを動作させるため、以下に示すライブラリをインストールする。<br>
# RHEL
sudo dnf install libraqm0
# SUSE
sudo zypper install libraqm0
<br><br>
<br><br>


326行目: 380行目:
   
   
     return app.exec();
     return app.exec();
}
</syntaxhighlight>
<br><br>
== 画像のリサイズと形式変換 ==
<syntaxhighlight lang="c++">
#include <Magick++.h>
#include <stdexcept>
#include <filesystem>
/**
  * @brief 画像のリサイズと形式変換を行う
  * @param inputPath 入力画像のパス
  * @param outputPath 出力画像のパス
  * @param width 目標の幅
  * @param height 目標の高さ
  * @param maintainAspectRatio アスペクト比を維持するかどうか
  */
void resizeAndConvert(const std::string& inputPath,
                      const std::string& outputPath,
                      size_t width,
                      size_t height,
                      bool maintainAspectRatio = true)
{
    try {
      Magick::Image image(inputPath);
      image.resize(Magick::Geometry(width, height, 0, 0, maintainAspectRatio));
      image.quality(90);  // JPEG品質を設定
      image.write(outputPath);
    }
    catch (const Magick::Exception& e) {
      throw std::runtime_error("リサイズ処理でエラーが発生: " + std::string(e.what()));
    }
}
// 使用例
try {
    // 画像のリサイズと形式変換
    ImageUtils::resizeAndConvert("input.jpg", "output.png", 800, 600 );
}
catch (const std::exception &e) {
    std::cerr << "エラーが発生: " << e.what() << std::endl;
    return -1;
}
</syntaxhighlight>
<br><br>
== フィルタの適用 ==
<syntaxhighlight lang="c++">
#include <Magick++.h>
#include <stdexcept>
/**
  * @brief 画像に各種フィルタを適用する
  * @param inputPath 入力画像のパス
  * @param outputPath 出力画像のパス
  * @param blurRadius ぼかしの半径
  * @param sharpenRadius シャープの半径
  */
void applyFilters(const std::string& inputPath,
                  const std::string& outputPath,
                  double blurRadius = 0.0,
                  double sharpenRadius = 0.0)
{
    try {
      Magick::Image image(inputPath);
      // ノイズ除去
      image.reduceNoise();
      // ぼかし処理 (指定された場合)
      if (blurRadius > 0.0) {
          image.blur(0.0, blurRadius);
      }
      // シャープ処理 (指定された場合)
      if (sharpenRadius > 0.0) {
          image.sharpen(0.0, sharpenRadius);
      }
      // コントラスト調整
      image.normalize();
      image.write(outputPath);
    }
    catch (const Magick::Exception &e) {
        throw std::runtime_error("フィルタ処理でエラーが発生: " + std::string(e.what()));
    }
}
// 使用例
try {
    applyFilters("input.jpg", "filtered.jpg"
                2.0,  // ブラー
                1.0  // シャープ
    );
}
catch (const std::exception &e) {
    std::cerr << "エラーが発生: " << e.what() << std::endl;
    return -1;
}
</syntaxhighlight>
<br><br>
== 透かしの追加 ==
<syntaxhighlight lang="c++">
#include <Magick++.h>
#include <stdexcept>
/**
  * @brief 画像に透かしを追加する
  * @param inputPath 入力画像のパス
  * @param watermarkPath 透かし画像のパス
  * @param outputPath 出力画像のパス
  * @param opacity 透かしの不透明度(0.0-1.0)
  */
void addWatermark(const std::string& inputPath,
                  const std::string& watermarkPath,
                  const std::string& outputPath,
                  double opacity = 0.5)
{
    try {
      Magick::Image baseImage(inputPath);
      Magick::Image watermark(watermarkPath);
      // 透かしのサイズを調整 (元画像の1/4サイズにリサイズ)
      watermark.resize(Magick::Geometry(baseImage.columns() / 4, baseImage.rows() / 4));
      // 透かしの不透明度を設定
      watermark.opacity(static_cast<unsigned int>(65535 * (1.0 - opacity)));
      // 透かしを右下に配置
      baseImage.composite(watermark, baseImage.columns() - watermark.columns() - 10,
                          baseImage.rows() - watermark.rows() - 10, Magick::OverCompositeOp);
      baseImage.write(outputPath);
    }
    catch (const Magick::Exception &e) {
      throw std::runtime_error("透かし処理でエラーが発生: " + std::string(e.what()));
    }
}
// 使用例
try {
    addWatermark("input.jpg", "watermark.png",
                "watermarked.jpg",  // 透かし画像
                0.7                // 透かしのOpacity
    );
}
catch (const std::exception &e) {
    std::cerr << "エラーが発生: " << e.what() << std::endl;
    return -1;
}
</syntaxhighlight>
<br><br>
== バッチ処理 (画像の一括変換) ==
<syntaxhighlight lang="c++">
#include <Magick++.h>
#include <filesystem>
#include <vector>
#include <stdexcept>
/**
  * @brief 画像のバッチ処理(一括リサイズ)を行う
  * @param inputDir 入力ディレクトリ
  * @param outputDir 出力ディレクトリ
  * @param width 目標の幅
  * @param height 目標の高さ
  * @param extensions 処理対象の拡張子リスト
  */
void batchResize(const std::string& inputDir,
                  const std::string& outputDir,
                  size_t width,
                  size_t height,
                  const std::vector<std::string>& extensions = {".jpg", ".jpeg", ".png"})
{
    namespace fs = std::filesystem;
    try {
      // 出力ディレクトリが存在しない場合は作成
      if (!fs::exists(outputDir)) fs::create_directories(outputDir);
      // ディレクトリ内のファイルを走査
      for (const auto& entry : fs::directory_iterator(inputDir)) {
          if (!entry.is_regular_file()) continue;
          // 拡張子のチェック
          std::string ext = entry.path().extension().string();
          std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
          if (std::find(extensions.begin(), extensions.end(), ext) != extensions.end()) {
            std::string outPath = (fs::path(outputDir) / entry.path().filename()).string();
            resizeAndConvert(entry.path().string(), outPath, width, height);
          }
      }
    }
    catch (const std::exception &e) {
      throw std::runtime_error("バッチ処理でエラーが発生: " + std::string(e.what()));
    }
}
// 使用例
try {
    // 1024x768に変換
    batchResize("input_folder", "output_folder", 1024, 768);
}
catch (const std::exception &e) {
    std::cerr << "エラーが発生: " << e.what() << std::endl;
    return -1;
}
</syntaxhighlight>
<br><br>
== 画像に効果を適用  (セピア、モノクロ等) ==
<syntaxhighlight lang="c++">
#include <Magick++.h>
#include <stdexcept>
/**
  * @brief 画像に効果を適用する(セピア、モノクロなど)
  * @param inputPath 入力画像のパス
  * @param outputPath 出力画像のパス
  * @param effectType 効果の種類(1:セピア, 2:モノクロ, 3:ネガティブ)
  */
void applyEffect(const std::string& inputPath,
                  const std::string& outputPath,
                  int effectType)
{
    try {
      Magick::Image image(inputPath);
      switch (effectType) {
          case 1: // セピア
            image.modulate(100, 50, 100);  // 彩度を下げる
            image.colorize(30, 30, 0, "rgb(112,66,20)");
            break;
          case 2: // モノクロ
            image.type(Magick::GrayscaleType);
            break;
          case 3: // ネガティブ
            image.negate();
            break;
          default:
            throw std::runtime_error("未知の効果タイプです");
            break;
      }
      image.write(outputPath);
    }
    catch (const Magick::Exception &e) {
      throw std::runtime_error("効果の適用でエラーが発生: " + std::string(e.what()));
    }
}
// 使用例
try {
    applyEffect("input.jpg", "sepia.jpg",
                1  // セピア効果
    );
}
catch (const std::exception &e) {
    std::cerr << "エラーが発生: " << e.what() << std::endl;
    return -1;
}
</syntaxhighlight>
<br><br>
== 画像形式の変換 ==
<syntaxhighlight lang="c++">
#include <Magick++.h>
#include <stdexcept>
/**
  * @brief 画像形式を変換する(例:JPG → PNG)
  * @param inputPath 入力画像のパス
  * @param outputPath 出力画像のパス(拡張子で形式を判断)
  * @param quality 出力画質(0-100)
  */
void convertFormat(const std::string& inputPath,
                    const std::string& outputPath,
                    int quality = 90)
{
    try {
      Magick::Image image(inputPath);
      image.quality(quality);
      image.write(outputPath);
    }
    catch (const Magick::Exception &e) {
      throw std::runtime_error("形式変換でエラーが発生: " + std::string(e.what()));
    }
  }
  }
  </syntaxhighlight>
  </syntaxhighlight>