import os
import asposeimagingcloud.models.requests as requests

IMAGE_FILE_NAME= 'example_image.dng';
IMAGES_FOLDER = 'ExampleImages';
CLOUD_FOLDER = 'CloudImages';
OUTPUT_FOLDER = 'Output';

# Get ClientId and ClientSecret from https://dashboard.aspose.cloud/ 
# or use on-premise version (https://docs.aspose.cloud/imaging/getting-started/how-to-run-docker-container/)
_imaging_api = ImagingApi(client_secret, client_id, 'https://api.aspose.cloud')

def convert_image_from_storage(self):
    """Convert an image to another format"""
   
    input_image = os.path.join(IMAGES_FOLDER, IMAGE_FILE_NAME)  
    upload_file_request = requests.UploadFileRequest(os.path.join(CLOUD_FOLDER, IMAGE_FILE_NAME), input_image)
    result = self._imaging_api.upload_file(upload_file_request)
    if result.errors:
        print('Uploading errors count: ' + str(len(result.errors)))

    # Please refer to
    # https://docs.aspose.cloud/imaging/supported-file-formats/#convert
    # for possible output formats
    format = 'jpeg'  # Resulting image format
    folder = CLOUD_FOLDER  # Input file is saved at the desired folder in the storage
    storage = None  # We are using default Cloud Storage

    request = requests.ConvertImageRequest(IMAGE_FILE_NAME, format, folder, storage)
    converted_image = self._imaging_api.convert_image(request)
    
    # Save the image file to output folder
    filename_part, extension = os.path.splitext(IMAGE_FILE_NAME)
    new_file_name = filename_part + '.' + 'jpeg'
    path = os.path.abspath(os.path.join(OUTPUT_FOLDER, new_file_name))
    shutil.copy(converted_image, path) 

def create_converted_image_from_request(self):
    """Convert an image to another format. Image data is passed in a request stream"""

    # Please refer to
    # https://docs.aspose.cloud/imaging/supported-file-formats/#convert
    # for possible output formats
    format = 'jpeg'  # Resulting image format
    storage = None  # We are using default Cloud Storage
    out_path = None  # Path to updated file (if this is empty, response contains streamed image)

    input_stream = os.path.join(IMAGES_FOLDER, IMAGE_FILE_NAME)
    request = requests.CreateConvertedImageRequest(input_stream, format, out_path, storage)
    converted_image = self._imaging_api.create_converted_image(request)

    # Save the image file to output folder
    filename_part, extension = os.path.splitext(IMAGE_FILE_NAME)
    new_file_name = filename_part + '.' + 'jpeg'
    path = os.path.abspath(os.path.join(OUTPUT_FOLDER, new_file_name))
    shutil.copy(converted_image, path)