Extract Image Metadata
Use the image-info endpoint to get metadata describing an image.
The image-info
endpoint is for obtaining metadata describing an image. In this tutorial we illustrate using this endpoint to obtain an image's metadata. We first call the image-info
REST endpoint directly using cURL. We then use the DynamicPDF API client libraries to programmatically call the endpoint.
Required Resources
To complete this tutorial, you must add the Get Image Information (image-info Endpoint) sample to your samples
folder in your cloud storage space using the the File Manager. After adding the sample resources, you should see a samples/get-image-info-image-info-endpoint
folder containing the resources for this tutorial.
Sample | Sample Folder | Resources |
---|---|---|
Get Image Information (image-info Endpoint) | samples/get-image-info-image-info-endpoint | dynamicpdfLogo.png |
- From the File Manager, download
dynamicpdfLogo.png
to your local system; here we assume/temp/dynamicpdf-api-samples/get-image-info
.
Resource | Cloud/Local |
---|---|
dynamicpdfLogo.png | local |
See Sample Resources for instructions on adding sample resources.
Obtaining API Key
This tutorial assumes a valid API key obtained from the DynamicPDF API's Portal
. Refer to the following for instructions on getting an API key.
If you are not familiar with the File Manager or Apps and API Keys, refer to the following tutorial and relevant Users Guide pages.
Make Request Using API
The image-info
endpoint is a form post that takes the API key in the header and the image as binary data in the request's body. The following uses cURL to make an HTTP POST request, passing the image as binary data. Note that in the Header we pass the Content-Type
as image/png
.
curl -X POST "https://api.dpdf.io/v1.0/image-info"
-H "Authorization: Bearer DP.xxx-api-key-xxx"
-H "Content-Type: image/png"
--data-binary "@c:/temp/dynamicpdf-api-samples/get-image-info/dynamicpdflogo.png"
Examine API Response
The REST call returns the following JSON describing the image.
[
{
"pageNumber": 1,
"width": 262,
"height": 250,
"horizondalDpi": 300,
"verticalDpi": 300,
"numberOfComponents": 3,
"bitsPerComponent": 8,
"colorSpace": "indexed"
}
]
The JSON response returns an array so that the endpoint can support multi-page tiffs. Refer to the Users Guide for an example using a multi-page tiff image.
Make Request Using Client Library
Now let's use the a client library to call the REST endpoint. To simplify your development, you can use any of the DynamicPDF API client libraries to complete this tutorial section. Each client library tab contains tutorial steps particular to the selected language.
Complete Source
You can access the complete source for this project at one of the following GitHub projects.
Language | File Name | Location (package/namespace/etc.) | GitHub Project |
---|---|---|---|
Java | GetImageInfo.java | com.dynamicpdf.api.examples | https://github.com/dynamicpdf-api/java-client-examples |
C# | Program.cs | GetImageInfo | https://github.com/dynamicpdf-api/dotnet-client-examples |
Nodejs | GetImageInfo.js | nodejs-client-examples | https://github.com/dynamicpdf-api/nodejs-client-examples |
PHP | GetImageInfo.php | php-client-examples | https://github.com/dynamicpdf-api/nodejs-client-examples |
GO | image-info-example.go | go-client-examples | https://github.com/dynamicpdf-api/go-client-examples/tree/main |
Python | ImageInfoExample.py | python-client-examples | https://github.com/dynamicpdf-api/python-client-examples |
Click on the language tab of choice to view the tutorial steps for the particular language.
- C# (.NET)
- Java
- Node.js
- PHP
- GO
- Python
Available on NuGet:
Install-Package DynamicPDF.API
- Create a new Console App (.NET Core) project named
GetImageInfo
. - Add the DynamicPDF.API NuGet package.
- Create a new static method named
Run
. - Add a new
ImageResource
instance and pass the path to the image in the constructor. - Create a new
ImageInfo
instance and pass theImageResource
instance to the constructor. - Add a call to the
ImageInfo
instance'sProcess
method and return the response. - Check that the call was successful and print the image metadata as JSON to the console.
- Run the application and the JSON image metadata appears in the console.
using DynamicPDF.Api;
using System;
namespace GetImageInfo
{
class Program
{
static void Main(string[] args)
{
Run("DP.xxx-api-key-xxx", "c:/temp/dynamicpdf-api-samples/get-image-info/");
}
public static void Run(String apiKey, String basePath)
{
ImageResource imageResource = new ImageResource(basePath + "dynamicpdflogo.png");
ImageInfo imageInfo = new ImageInfo(imageResource);
imageInfo.ApiKey = apiKey;
ImageResponse response = imageInfo.Process();
if(response.IsSuccessful)
{
Console.WriteLine(response.JsonContent);
} else
{
Console.WriteLine(response.ErrorJson);
}
}
}
}
Available on NPM:
npm i @dynamicpdf/api
- Use npm to install the DynamicPDF API module.
- Create a new class named
GetImageInfo
. - Create a static
Run
method. - Add a new
ImageResource
instance and pass the path to the image in the constructor. - Create a new
ImageInfo
instance and pass theImageResource
instance to the constructor. - Add a call to the
ImageInfo
instance'sprocess
method and return the response. - Check that the call was successful and print the image metadata as JSON to the console.
- Add a call to the
GetImageInfo.Run()
method.
import {
ImageResource,
ImageInfo,
ImageResponse
} from "@dynamicpdf/api"
export class GetImageInfo {
static async Run() {
var imageResource = new ImageResource("C:/temp/dynamicpdf-api-samples/get-image-info/dynamicpdfLogo.png");
var imageInfo = new ImageInfo(imageResource);
imageInfo.apiKey = "DP.xxx-api-key-xxx";
var imageResponse = await imageInfo.process();
if (imageResponse.isSuccessful) {
console.log(JSON.parse(imageResponse.content));
} else {
console.log(imageResponse.errorJson);
}
}
}
await GetImageInfo.Run();
- Run the application
node ImageInfo.js
and the image metadata prints to the console.
Available on Maven:
https://search.maven.org/search?q=g:com.dynamicpdf.api
<dependency>
<groupId>com.dynamicpdf.api</groupId>
<artifactId>dynamicpdf-api</artifactId>
<version>1.0.0</version>
</dependency>
-
Create a new Maven project and add the DynamicPDF API as a dependency
-
Create a new class named
GetImageInfo
with amain
method. -
Create a new static method named
Run
. -
Add a new
ImageResource
instance and pass the path to the image in the constructor. -
Create a new
ImageInfo
instance and pass theImageResource
instance to the constructor. -
Add a call to the
ImageInfo
instance'sProcess
method and return the response. -
Check that the call was successful and print the image metadata as JSON to the console.
package com.dynamicpdf.api.examples;
import com.dynamicpdf.api.ImageInfo;
import com.dynamicpdf.api.ImageResource;
import com.dynamicpdf.api.ImageResponse;
public class GetImageInfo {
public static void main(String[] args) {
GetImageInfo.Run("DP.xxx-api-key-xxx",
"C:/temp/dynamicpdf-api-samples/get-image-info/");
}
public static void Run(String key, String basePath) {
ImageResource imageResource = null;
imageResource = new ImageResource(basePath + "/dynamicpdflogo.png");
ImageInfo imageInfo = new ImageInfo(imageResource);
imageInfo.setApiKey(key);
ImageResponse response = imageInfo.process();
if(response.getIsSuccessful()) {
System.out.println(response.getJsonContent());
} else {
System.out.println(response.getErrorJson());
}
}
}
Available as a Composer package:
composer require dynamicpdf/api
- Use composer to ensure you have the required PHP libraries.
- Create a new class named
GetImageInfo
. - Add a
Run
method. - Add a new
ImageResource
instance and pass the path to the image in the constructor. - Create a new
ImageInfo
instance and pass theImageResource
instance to the constructor. - Add a call to the
ImageInfo
instance'sProcess
method and return the response. - Check that the call was successful and print the image metadata as JSON to the console.
<?php
require __DIR__ . '/vendor/autoload.php';
use DynamicPDF\Api\ImageResource;
use DynamicPDF\Api\ImageInfo;
class GetImageInfo
{
private static string $BasePath = "C:/temp/dynamicpdf-api-samples/get-image-info/";
public static function Run()
{
$imageResource = new ImageResource(GetImageInfo::$BasePath . "dynamicpdfLogo.png");
$imageInfo = new ImageInfo($imageResource);
$imageInfo->ApiKey = "DP.xxx-api-key-xxx";
$response = $imageInfo->Process();
if($response->IsSuccessful)
{
echo ($response->JsonContent);
} else {
echo("Error: ");
echo($response->StatusCode);
echo($response->ErrorMessage);
}
}
}
GetImageInfo::Run();
- Run the application
php GetImageInfo.php
and the image JSON metadata prints to the console.
Available a GO package: https://pkg.go.dev/github.com/dynamicpdf-api/go-client
- Ensure you have the required GO libraries.
- Create a new file named
image-info-example.go
. - Add a
main
method. - Add a new
ImageResource
instance and pass the path to the image in the constructor. - Create a new
ImageInfo
instance and pass theImageResource
instance to the constructor. - Add a call to the
ImageInfo
instance'sProcess
method and return the response. - Check that the call was successful and print the image metadata as JSON to the console.
- Run the application
go run image-info-example.go
and the image JSON metadata prints to the console.
package main
import (
"fmt"
"github.com/dynamicpdf-api/go-client/endpoint"
"github.com/dynamicpdf-api/go-client/resource"
)
func main() {
basePath := "C:/temp/dynamicpdf-api-samples/"
resource := resource.NewImageResourceWithResourcePath(basePath+"dynamicpdfLogo.png", "")
imageInfo := endpoint.NewImageInfo(resource)
imageInfo.Endpoint.BaseUrl = "https://api.dpdf.io/"
imageInfo.Endpoint.ApiKey = "DP.xxx-api-key-xxx"
resp := imageInfo.Process()
res := <-resp
if res.IsSuccessful() == true {
fmt.Print(string(res.Content().Bytes()))
}
}
Available at: pip install dynamicpdf-api
- Ensure you have the required Python libraries.
- Create a new file named
ImageInfoExample.py
. - Add a
run
method. - Add a new
ImageResource
instance and pass the path to the image in the constructor. - Create a new
ImageInfo
instance and pass theImageResource
instance to the constructor. - Add a call to the
ImageInfo
instance'sprocess
method and return the response. - Check that the call was successful and print the image metadata as JSON to the console.
- Run the application
python ImageInfoExample.py
and the image JSON metadata prints to the console.
from dynamicpdf_api.image_resource import ImageResource
from dynamicpdf_api.image_info import ImageInfo
import pprint
import json
def run(api_key, basePath):
resource = ImageResource(basePath + "dynamicpdfLogo.png")
image_info = ImageInfo(resource)
image_info.api_key = api_key
response = image_info.process()
print(response.json_content)
if __name__ == "__main__":
api_key = 'DP.xxx-api-key-xxx'
basePath = "C:/temp/dynamicpdf-api-samples/"
run(api_key, basePath)
In all six languages, the steps were similar. First, we created a new ImageResource
instance by loading the path to the image via the constructor. Next, we created a new instance of the ImageInfo
class, which abstracts the image-info
endpoint. Then the ImageInfo
instance prints the extracted image information as JSON after processing. Finally, we called the Process
method and print the resultant JSON to the console.