Python で画像のコントラスト、明るさ、ガンマを調整する

画像の加工や編集では、明るさコントラストを操作して、画像の視覚的な品質を向上させます。さらに、これらのパラメータを使用すると、露出オーバーを軽減して画像を微調整できます。一方、γ3パラメータは画像の明るさを制御するためにも使用される。プログラムで画像を処理する際、これらのパラメータを処理する必要がある場合があります。そこでこの記事では、Pythonで画像の明るさ、コントラスト、ガンマを調整する方法を紹介します。

画像のコントラスト、明るさ、ガンマを調整するための Python ライブラリ

画像のコントラスト、明るさ、ガンマを調整するには、Aspose.Imaging for Pythonを使用します。これは、Python 開発者が画像を簡単に操作できるようにする、強力でありながら使いやすい画像処理ライブラリです。ライブラリを使用するには、ダウンロードするか、次のコマンドを使用してインストールします。

> pip install aspose-imaging-python-net 

Python で画像のコントラストを調整する

コントラストは、画像内の色または明るさのレベルの違いを指します。コントラストを調整すると、画像内のオブジェクトがより鮮明になります。コントラストが高いということは、明るい日光の下で撮影した写真と同様に、より鮮明で鮮明な画像を意味します。一方、コントラストが低いと、屋外で霧がかかっているときと同じように、物体を視認したり区別したりすることが困難になります。

Python を使用して画像のコントラストを調整する方法を見てみましょう。

  • まず、Image.load() メソッドを使用して画像を読み込みます。
  • 次に、オブジェクトを RasterImage タイプにキャストします。
  • その後、RasterImage.cachedata()メソッドを使用していない場合は画像をキャッシュします。
  • RasterImage.adjustcontrast()メソッドを使用して、[-100, 100]の範囲でコントラストを調整します。
  • 最後に、RasterImage.save() メソッドを使用して、結果のイメージを保存します。

次のコード サンプルは、Python で画像のコントラストを調整する方法を示しています。

import aspose.pycore as aspycore
from aspose.imaging import RasterImage, Image
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat, TiffPhotometrics
from aspose.imaging.imageoptions import TiffOptions
import os


if 'TEMPLATE_DIR' in os.environ:
	templates_folder = os.environ['TEMPLATE_DIR']
else:
	templates_folder = r"C:\Users\USER\Downloads\templates"

delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
# Image のインスタンスにイメージをロードします
with Image.load(os.path.join(data_dir, "template.jpg")) as image:
	# Image のオブジェクトを RasterImage にキャストします
	raster_image = aspycore.as_of(image, RasterImage)
	# RasterImage がキャッシュされているかどうかを確認し、パフォーマンスを向上させるために RasterImage をキャッシュします
	if not raster_image.is_cached:
		raster_image.cache_data()

	# コントラストを調整する
	raster_image.adjust_contrast(10)
	# 結果の画像の TiffOptions のインスタンスを作成し、TiffOptions のオブジェクトのさまざまなプロパティを設定して、結果の画像を TIFF 形式で保存します。
	tiff_options = TiffOptions(TiffExpectedFormat.DEFAULT)
	tiff_options.bits_per_sample = [8, 8, 8]
	tiff_options.photometric = TiffPhotometrics.RGB
	raster_image.save(os.path.join(data_dir, "result.tiff"), tiff_options)

if delete_output:
	os.remove(os.path.join(data_dir, "result.tiff"))

以下のスクリーンショットは、コントラストを調整する前と後の画像を示しています。

Python で画像のコントラストを調整する

画像のコントラストを調整する

Python で画像の明るさを調整する

明るさは画像の暗さを増減するために使用され、オブジェクトの可視性を調整できます。以下は、Python で画像の明るさを変更する手順です。

  • まず、Image.load() メソッドを使用して画像を読み込みます。
  • 次に、オブジェクトを RasterImage タイプにキャストします。
  • その後、RasterImage.cachedata()メソッドを使用していない場合は画像をキャッシュします。
  • RasterImage.adjustbrightness()メソッドを使用して画像の明るさを調整します。
  • 最後に、RasterImage.save() メソッドを使用して、変更した画像を保存します。

次のコード サンプルは、Python で画像の明るさを調整する方法を示しています。

import aspose.pycore as aspycore
from aspose.imaging import RasterImage, Image
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat, TiffPhotometrics
from aspose.imaging.imageoptions import TiffOptions
import os


if 'TEMPLATE_DIR' in os.environ:
	templates_folder = os.environ['TEMPLATE_DIR']
else:
	templates_folder = r"C:\Users\USER\Downloads\templates"

delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
# Image のインスタンスにイメージをロードします
with Image.load(os.path.join(data_dir, "template.jpg")) as image:
	# Image のオブジェクトを RasterImage にキャストします
	raster_image = aspycore.as_of(image, RasterImage)
	# RasterImage がキャッシュされているかどうかを確認し、パフォーマンスを向上させるために RasterImage をキャッシュします
	if not raster_image.is_cached:
		raster_image.cache_data()

	# 明るさを調整する
	raster_image.adjust_brightness(70)
	# 結果の画像の TiffOptions のインスタンスを作成し、TiffOptions のオブジェクトのさまざまなプロパティを設定して、結果の画像を保存します
	tiff_options = TiffOptions(TiffExpectedFormat.DEFAULT)
	tiff_options.bits_per_sample = [8, 8, 8]
	tiff_options.photometric = TiffPhotometrics.RGB
	raster_image.save(os.path.join(data_dir, "result.tiff"), tiff_options)

if delete_output:
	os.remove(os.path.join(data_dir, "result.tiff"))

明るさの値を変更した後の入力画像と出力画像の比較です。

Python で画像の明るさを調整する

画像の明るさを調整する

Python で画像のガンマを変更する

ガンマは、画像内の RGB カラーの比率を制御する属性を指します。それに加えて、画像の明るさも変更します。それでは、Python を使用して画像のガンマ パラメーターを調整する方法を見てみましょう。

  • まず、Image.load() メソッドを使用して画像を読み込みます。
  • 次に、オブジェクトを RasterImage タイプにキャストします。
  • その後、RasterImage.cachedata()メソッドを使用していない場合は画像をキャッシュします。
  • RasterImage.adjustgamma()メソッドを使用してガンマ値を変更します。
  • 最後に、RasterImage.save() メソッドを使用して、更新されたイメージを保存します。

次のコード サンプルは、Python で画像のガンマ値を調整する方法を示しています。

import aspose.pycore as aspycore
from aspose.imaging import RasterImage, Image
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat, TiffPhotometrics
from aspose.imaging.imageoptions import TiffOptions
import os


if 'TEMPLATE_DIR' in os.environ:
	templates_folder = os.environ['TEMPLATE_DIR']
else:
	templates_folder = r"C:\Users\USER\Downloads\templates"

delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
# Image のインスタンスにイメージをロードします
with Image.load(os.path.join(data_dir, "template.jpg")) as image:
	# Image のオブジェクトを RasterImage にキャストします
	raster_image = aspycore.as_of(image, RasterImage)
	# RasterImage がキャッシュされているかどうかを確認し、パフォーマンスを向上させるために RasterImage をキャッシュします
	if not raster_image.is_cached:
		raster_image.cache_data()

	# コントラストを調整する
	raster_image.adjust_gamma(10)
	# 結果の画像の TiffOptions のインスタンスを作成し、TiffOptions のオブジェクトのさまざまなプロパティを設定して、結果の画像を TIFF 形式で保存します。
	tiff_options = TiffOptions(TiffExpectedFormat.DEFAULT)
	tiff_options.bits_per_sample = [8, 8, 8]
	tiff_options.photometric = TiffPhotometrics.RGB
	raster_image.save(os.path.join(data_dir, "result.tiff"), tiff_options)

if delete_output:
	os.remove(os.path.join(data_dir, "result.tiff"))

下の画像は、ガンマ値を変更した後の入力画像と出力画像の比較を示しています。

Python で画像のガンマを調整する

画像ガンマを調整する

画像のコントラスト、明るさ、ガンマを調整するための無料ライセンス

無料の一時ライセンスを取得すると、評価制限なしで画像のコントラスト、明るさ、ガンマを調整できます。

無料のオンライン画像エディター

無料の Web ベースの画像編集ツール を使用して、オンラインで画像を変更できます。この画像エディターは Aspose.Imaging for Python を利用しており、アカウントを作成する必要はありません。

結論

この記事では、Python を使用して画像のコントラスト、明るさ、ガンマ値を調整する方法を説明しました。手順とコード サンプルを使用して、イメージ内のこれらのパラメーターを変更する方法を示しました。さらに、出力を画像を使用して示しました。また、Aspose.Imaging for Python に基づいた完全に無料の無料画像編集ツールも提供しています。

Python 画像処理ライブラリについてさらに詳しく知りたい場合は、ドキュメント を参照してください。ご質問やご不明な点がございましたら、フォーラムまでお問い合わせください。

関連項目