白黒およびグレースケールへの画像変換
印刷や保存のために、カラー画像を白黒またはグレースケールに変換する必要がある場合があります。この記事では、Aspose.PSD for .NET APIを使用して、以下の2つの方法でこれを実珅する方法を示します。
- 二値化
- グレースケール化
二値化
二値化の概念を理解するために、バイナリ画像を定義することが重要です。それは各ピクセルに2つの可能な値しか持たないデジタル画像です。通常、バイナリ画像で使用される2つの色は黒と白ですが、他の2つの色も使用できます。 二値化は画像を2値レベルに変換するプロセスであり、各ピクセルが1ビット(0または1)として保存され、0は色の欠如を、1は色の存在を示します。Aspose.PSD for .NET APIは現在、2つの二値化メソッドをサポートしています。
閾値を使用した二値化
次のコードスニペットは、画像に固定された閾値二値化を適用する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFile = dataDir + @"sample.psd"; | |
string destName = dataDir + @"BinarizationWithFixedThreshold_out.jpg"; | |
// Load an image | |
using (Image image = Image.Load(sourceFile)) | |
{ | |
// Cast the image to RasterCachedImage and Check if image is cached | |
RasterCachedImage rasterCachedImage = (RasterCachedImage)image; | |
if (!rasterCachedImage.IsCached) | |
{ | |
// Cache image if not already cached | |
rasterCachedImage.CacheData(); | |
} | |
// Binarize image with predefined fixed threshold and Save the resultant image | |
rasterCachedImage.BinarizeFixed(100); | |
rasterCachedImage.Save(destName, new JpegOptions()); | |
} |
大津の閾値を使用した二値化
次のコードスニペットは、画像に大津の閾値二値化を適用する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFile = dataDir + @"sample.psd"; | |
string destName = dataDir + @"BinarizationWithOtsuThreshold_out.jpg"; | |
// Load an image | |
using (Image image = Image.Load(sourceFile)) | |
{ | |
// Cast the image to RasterCachedImage and Check if image is cached | |
RasterCachedImage rasterCachedImage = (RasterCachedImage)image; | |
if (!rasterCachedImage.IsCached) | |
{ | |
// Cache image if not already cached | |
rasterCachedImage.CacheData(); | |
} | |
// Binarize image with Otsu Thresholding and Save the resultant image | |
rasterCachedImage.BinarizeOtsu(); | |
rasterCachedImage.Save(destName, new JpegOptions()); | |
} |
グレースケール化
グレースケール化とは、連続トーン画像を不連続な灰色の階調の画像に変換するプロセスです。次のコードスニペットは、グレースケール化の方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFile = dataDir + @"sample.psd"; | |
string destName = dataDir + @"Grayscaling_out.jpg"; | |
// Load an image in an instance of Image | |
using (Image image = Image.Load(sourceFile)) | |
{ | |
// Cast the image to RasterCachedImage and Check if image is cached | |
RasterCachedImage rasterCachedImage = (RasterCachedImage)image; | |
if (!rasterCachedImage.IsCached) | |
{ | |
// Cache image if not already cached | |
rasterCachedImage.CacheData(); | |
} | |
// Transform image to its grayscale representation and Save the resultant image | |
rasterCachedImage.Grayscale(); | |
rasterCachedImage.Save(destName, new JpegOptions()); | |
} |
GIF画像のレイヤーをTIFF画像に変換
PSD画像のレイヤーを別のラスター画像形式に抽出および変換する必要がある場合があります。Aspose.PSD APIは、PSD画像のレイヤーを別のラスター画像形式に変換する機能をサポートしています。まず、画像のインスタンスを作成し、ローカルディスクからPSD画像をロードし、次にLayerプロパティ内の各レイヤーを反復処理します。その後、ブロックをTIFF画像に変換します。次のコードスニペットは、PSD画像のレイヤーをTIFF画像に変換する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFile = dataDir + @"sample.psd"; | |
string destName = dataDir + @"output"; | |
// Load a PSD image and Convert the image's layers to Tiff images. | |
using (PsdImage image = (PsdImage)Image.Load(sourceFile)) | |
{ | |
// Iterate through array of PSD layers | |
for (int i = 0; i < image.Layers.Length; i++) | |
{ | |
// Get PSD layer. | |
Layer layer = image.Layers[i]; | |
// Create an instance of TIFF Option class and Save the PSD layer as TIFF image | |
TiffOptions objTiff = new TiffOptions(TiffExpectedFormat.TiffDeflateRgb); | |
layer.Save("output" + i + "_out.tif", objTiff); | |
} | |
} |
CMYK PSDをCMYK TIFFに変換
Aspose.PSD for .NETを使用すると、開発者はCMYK PSDファイルをCMYK TIFF形式に変換することができます。この記事では、Aspose.PSDを使用してCMYK PSDファイルをCMYK TIFF形式にエクスポート/変換する方法を示します。Aspose.PSD for .NETを使用すると、PSD画像をロードし、TiffOptionsクラスを使用してさまざまなプロパティを設定し、画像を保存またはエクスポートすることができます。次のコードスニペットは、この機能を実珅する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFile = dataDir + @"sample.psd"; | |
string destName = dataDir + @"output.tiff"; | |
using (Image image = Image.Load(sourceFile)) | |
{ | |
image.Save(destName, new TiffOptions(TiffExpectedFormat.TiffLzwCmyk)); | |
} |
画像のエクスポート
Aspose.PSDには、PSDファイル形式を他の形式に変換するための専用クラスが付属しています。このライブラリを使用すると、PSD画像の変換が非常に簡単で直感的になります。以下は、ImageOptions名前空間でこの目的のために使用されるいくつかの専用クラスです。
Aspose.PSD for .NET APIを使用してPSD画像をエクスポートすることは簡単です。ImageOptions名前空間から適切なクラスのオブジェクトが必要です。これらのクラスを使用すると、Aspose.PSD for .NETで作成、編集、ただ読み込まれた任意のイメージを、サポートされているフォーマットに簡単にエクスポートできます。
画像の結合
この例では、Graphicsクラスを使用して2つ以上の画像を1つの完全な画像に結合する方法を示しています。操作をデモンストレーションするために、この例では新しいPsdImageを作成し、Graphicsクラスで公開されているDraw Imageメソッドを使用して、画像をキャンバス表面に描画します。Graphicsクラスを使用すると、2つ以上の画像を結合して、結果画像が画像部分間やページ間にスペースがなく、ページがない完全な1枚のイメージに見えるようにすることができます。キャンバスのサイズは、結果画像のサイズに等しくする必要があります。次に、DrawImageメソッドの使用方法を示すコードデモンストレーションが続きます。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Create an instance of PsdOptions and set its various properties | |
PsdOptions imageOptions = new PsdOptions(); | |
// Create an instance of FileCreateSource and assign it to Source property | |
imageOptions.Source = new FileCreateSource(dataDir + "Two_images_result_out.psd", false); | |
// Create an instance of Image and define canvas size | |
using (var image = Image.Create(imageOptions, 600, 600)) | |
{ | |
// Create and initialize an instance of Graphics, Clear the image surface with white color and Draw Image | |
var graphics = new Graphics(image); | |
graphics.Clear(Color.White); | |
graphics.DrawImage(Image.Load(dataDir + "example1.psd"), 0, 0, 300, 600); | |
graphics.DrawImage(Image.Load(dataDir + "example2.psd"), 300, 0, 300, 600); | |
image.Save(); | |
} |
画像の拡張および切り取り
Aspose.PSD APIを使用すると、画像変換プロセス中に画像を拡張または切り取ることができます。開発者は、XおよびY座標の長方形を作成し、長方形の幅と高さを指定する必要があります。長方形のX、Y、幅、高さは、ロードされた画像の拡張または切り取りを示すものである必要があります。画像変換中に画像を拡張または切り取る必要がある場合は、次の手順を実行します。
- RasterImageクラスのインスタンスを作成し、既存の画像をロードします。
- ImageOptionクラスのインスタンスを作成します。
- Rectangleクラスのインスタンスを作成し、長方形のX、Yおよび幅、高さを初期化します。
- RasterImageクラスのSaveメソッドを呼び出し、出力ファイル名、画像オプション、および長方形オブジェクトをパラメータとして渡します。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string sourceFile = dataDir + @"example1.psd"; | |
string destName = dataDir + @"jpeg_out.jpg"; | |
// Load an image in an instance of Image and Setting for image data to be cashed | |
using (RasterImage rasterImage = (RasterImage)Image.Load(sourceFile)) | |
{ | |
rasterImage.CacheData(); | |
// Create an instance of Rectangle class and define X,Y and Width, height of the rectangle, and Save output image | |
Rectangle destRect = new Rectangle { X = -200, Y = -200, Width = 300, Height = 300 }; | |
rasterImage.Save(destName, new JpegOptions(), destRect); | |
} |
画像にXMPデータを読み書きする
XMP(Extensible Metadata Platform)はISO標準です。XMPはデータモデル、シリアル化形式、および拡張メタデータの定義と処理のためのコアプロパティを標準化しています。また、XMP情報をJPEGなどの一般的な画像に埋め込むためのガイドラインを提供し、XMPをサポートしていないアプリケーションによってその可読性が損なわれることがないようにします。Aspose.PSD for .NET APIを使用すると、開発者は画像にXMPメタデータを読み取ったり書き込んだりすることができます。この記事では、画像からXMPメタデータを読み取り、画像にXMPメタデータを書き込む方法を示しています。
XMPメタデータの作成、書き込み、ファイルから読み取り
Xmp名前空間を使用することで、開発者はXMPメタデータオブジェクトを作成し、画像に書き込むことができます。次のコードスニペットは、Xmp名前空間に含まれるXmpHeaderPi、XmpTrailerPi、XmpMeta、XmpPacketWrapper、PhotoshopPackage、DublinCorePackageパッケージの使用方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Specify the size of image by defining a Rectangle | |
Rectangle rect = new Rectangle(0, 0, 100, 200); | |
// Create the brand new image just for sample purposes | |
using (var image = new PsdImage(rect.Width, rect.Height)) | |
{ | |
// Create an instance of XMP-Header | |
XmpHeaderPi xmpHeader = new XmpHeaderPi(Guid.NewGuid().ToString()); | |
// Create an instance of Xmp-TrailerPi, XMPmeta class to set different attributes | |
XmpTrailerPi xmpTrailer = new XmpTrailerPi(true); | |
XmpMeta xmpMeta = new XmpMeta(); | |
xmpMeta.AddAttribute("Author", "Mr Smith"); | |
xmpMeta.AddAttribute("Description", "The fake metadata value"); | |
// Create an instance of XmpPacketWrapper that contains all metadata | |
XmpPacketWrapper xmpData = new XmpPacketWrapper(xmpHeader, xmpTrailer, xmpMeta); | |
// Create an instacne of Photoshop package and set photoshop attributes | |
PhotoshopPackage photoshopPackage = new PhotoshopPackage(); | |
photoshopPackage.SetCity("London"); | |
photoshopPackage.SetCountry("England"); | |
photoshopPackage.SetColorMode(ColorMode.Rgb); | |
photoshopPackage.SetCreatedDate(DateTime.UtcNow); | |
// Add photoshop package into XMP metadata | |
xmpData.AddPackage(photoshopPackage); | |
// Create an instacne of DublinCore package and set dublinCore attributes | |
DublinCorePackage dublinCorePackage = new DublinCorePackage(); | |
dublinCorePackage.SetAuthor("Mudassir Fayyaz"); | |
dublinCorePackage.SetTitle("Confessions of a Man Insane Enough to Live With the Beasts"); | |
dublinCorePackage.AddValue("dc:movie", "Barfly"); | |
// Add dublinCore Package into XMP metadata | |
xmpData.AddPackage(dublinCorePackage); | |
using (var ms = new MemoryStream()) | |
{ | |
// Update XMP metadata into image and Save image on the disk or in memory stream | |
image.XmpData = xmpData; | |
image.Save(ms); | |
image.Save(dataDir + "ee.psd"); | |
ms.Seek(0, System.IO.SeekOrigin.Begin); | |
// Load the image from memory stream or from disk to read/get the metadata | |
using (var img = (PsdImage)Image.Load(ms)) | |
{ | |
// Getting the XMP metadata | |
XmpPacketWrapper imgXmpData = img.XmpData; | |
foreach (XmpPackage package in imgXmpData.Packages) | |
{ | |
// Use package data ... | |
} | |
} | |
} | |
} |
マルチスレッド環境で画像をエクスポート
Aspose.PSD for .NETは、マルチスレッド環境で画像を変換することをサポートしています。Aspose.PSD for .NETでは、ソースプロパティが設定されている場合は、すべての画像オプションクラス(たとえば、BmpOptions、TiffOptions、JpegOptionsなど)がIDisposableインターフェイスを実装しています。したがって、開発者はソースストリームへのアクセスを同期させるためにSyncRootプロパティを使用できます。次のコードスニペットは、SyncRootプロパティの使用方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string imageDataPath = dataDir + @"sample.psd"; | |
try | |
{ | |
// Create the stream of the existing image file. | |
using (System.IO.FileStream fileStream = System.IO.File.Create(imageDataPath)) | |
{ | |
// Create an instance of PSD image option class. | |
using (PsdOptions psdOptions = new PsdOptions()) | |
{ | |
// Set the source property of the imaging option class object. | |
psdOptions.Source = new Sources.StreamSource(fileStream); | |
// DO PROCESSING. | |
// Following is the sample processing on the image. Un-comment to use it. | |
//using (RasterImage image = (RasterImage)Image.Create(psdOptions, 10, 10)) | |
//{ | |
// Color[] pixels = new Color[4]; | |
// for (int i = 0; i < 4; ++i) | |
// { | |
// pixels[i] = Color.FromArgb(40, 30, 20, 10); | |
// } | |
// image.SavePixels(new Rectangle(0, 0, 2, 2), pixels); | |
// image.Save(); | |
//} | |
} | |
} | |
} | |
finally | |
{ | |
// Delete the file. This statement is in the final block because in any case this statement should execute to make it sure that resource is properly disposed off. | |
System.IO.File.Delete(imageDataPath); | |
} |