在 Python 中調整圖像對比度、亮度和伽瑪

在圖像處理和編輯中,通過操縱亮度對比度來增強圖像的視覺質量。此外,這些參數允許您通過減少過度曝光來微調圖像。另一方面,γ3參數也用於控製圖像的亮度。以編程方式處理圖像時,您可能必須處理這些參數。因此,在本文中,我們將向您展示如何在 Python 中調整圖像的亮度、對比度和伽瑪。

用於調整圖像對比度、亮度和 Gamma 的 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 中修改圖像的 Gamma

Gamma 是指控製圖像中 RGB 顏色比率的屬性。除此之外,它還修改圖像的亮度。那麼讓我們看看如何使用 Python 調整圖像的 gamma 參數。

  • 首先,使用 Image.load() 方法加載圖像。
  • 然後,將對象轉換為 RasterImage 類型。
  • 之後,如果不使用 RasterImage.cachedata() 方法,則緩存圖像。
  • 使用 RasterImage.adjustgamma() 方法修改伽瑪值。
  • 最後,使用 RasterImage.save() 方法保存更新後的圖像。

以下代碼示例展示瞭如何在Python中調整圖像的gamma值。

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中調整圖像的Gamma

調整圖像伽瑪值

調整圖像對比度、亮度和伽瑪值的免費許可

通過獲取免費臨時許可證,您可以調整圖像的對比度、亮度和伽瑪,而不受評估限制。

免費在線圖像編輯器

您可以使用我們的免費的基於網絡的圖像編輯工具在線修改您的圖像。該圖像編輯器由 Aspose.Imaging for Python 提供支持,不需要您創建帳戶。

結論

本文演示瞭如何使用 Python 調整圖像中的對比度、亮度和伽瑪值。在步驟和代碼示例的幫助下,我們演示瞭如何修改圖像中的這些參數。此外,我們還使用圖像描述了輸出。我們還為您提供了一個免費的圖像編輯工具,它基於 Aspose.Imaging for Python,並且完全免費。

如果您想探索有關 Python 圖像處理庫的更多信息,請訪問 文檔。如果您有任何問題或疑問,請通過我們的論壇與我們聯繫。

也可以看看