|
| 1 | +# GROUP: MODIFYING_AND_CONVERTING_IMAGES |
| 2 | + |
| 3 | +from aspose.pycore import type_of |
| 4 | +from aspose.imaging import Image |
| 5 | +from aspose.imaging.imageoptions import PngOptions |
| 6 | +from aspose.imaging.xmp import XmpPacketWrapper |
| 7 | +from aspose.imaging.xmp.schemas.xmpbaseschema import XmpBasicPackage |
| 8 | +from aspose.imaging.exif.enums import ExifOrientation |
| 9 | +from aspose.imaging.exif import ExifData |
| 10 | +import os |
| 11 | + |
| 12 | + |
| 13 | +# Initialization |
| 14 | +def get_data_root_dir_local(): |
| 15 | + if 'BASE_DIR' in os.environ: |
| 16 | + return os.environ["BASE_DIR"] |
| 17 | + return "." |
| 18 | + |
| 19 | + |
| 20 | +if 'get_data_root_dir' not in dir(): |
| 21 | + get_data_root_dir = get_data_root_dir_local |
| 22 | + |
| 23 | +if 'get_output_dir' not in dir(): |
| 24 | + get_output_dir = get_data_root_dir_local |
| 25 | + |
| 26 | + |
| 27 | +def edit_source_image_metadata(input_path, output_path): |
| 28 | + with Image.load(input_path) as image: |
| 29 | + if image.xmp_data is not None: |
| 30 | + new_package = XmpBasicPackage() |
| 31 | + new_package.add_value("New key", "New value") |
| 32 | + image.xmp_data.add_package(new_package) |
| 33 | + |
| 34 | + if image.exif_data is not None: |
| 35 | + image.exif_data.orientation = ExifOrientation.RIGHT_TOP |
| 36 | + |
| 37 | + image.save(output_path) |
| 38 | + |
| 39 | + if 'SAVE_OUTPUT' not in os.environ: |
| 40 | + os.remove(output_path) |
| 41 | + |
| 42 | + |
| 43 | +def export_source_image_metadata(input_path, output_path, output_options): |
| 44 | + with Image.load(input_path) as input_image: |
| 45 | + # Set KeepMetadata to true to export inputImage metadata profiles, if outputOptions instance does not contain ones. |
| 46 | + output_options.keep_metadata = True |
| 47 | + input_image.save(output_path, output_options) |
| 48 | + |
| 49 | + if 'SAVE_OUTPUT' not in os.environ: |
| 50 | + os.remove(output_path) |
| 51 | + |
| 52 | + |
| 53 | +def overwrite_source_image_metadata(input_path, output_path, output_options): |
| 54 | + with Image.load(input_path) as image: |
| 55 | + new_metadata = get_new_metadata() |
| 56 | + # Try to set metadata, if the image format support metadata format type. |
| 57 | + for metadata in new_metadata: |
| 58 | + if output_options.try_set_metadata(metadata): |
| 59 | + print(f"{output_options.get_type().full_name} image supports {metadata.get_type().full_name}") |
| 60 | + |
| 61 | + # Or set metadata directly without image and metadata format compatibility check. |
| 62 | + output_options.exif_data = [it for it in new_metadata if it.get_type() == type_of(ExifData)][0] |
| 63 | + output_options.xmp_data = [it for it in new_metadata if it.get_type() == type_of(XmpPacketWrapper)][0] |
| 64 | + |
| 65 | + image.save(output_path, output_options) |
| 66 | + |
| 67 | + if 'SAVE_OUTPUT' not in os.environ: |
| 68 | + os.remove(output_path) |
| 69 | + |
| 70 | + |
| 71 | +def get_new_metadata(): |
| 72 | + xmpData = XmpPacketWrapper() |
| 73 | + xmpPackage = XmpBasicPackage() |
| 74 | + xmpPackage.add_value("User key", "User value") |
| 75 | + xmpData.add_package(xmpPackage) |
| 76 | + |
| 77 | + exif_data = ExifData() |
| 78 | + exif_data.orientation = ExifOrientation.RIGHT_TOP |
| 79 | + |
| 80 | + return (xmpData, exif_data) |
| 81 | + |
| 82 | + |
| 83 | +# Example code: |
| 84 | +# The path to the documents directory. |
| 85 | +data_dir = os.path.join(get_data_root_dir(), 'png') |
| 86 | +file_path = os.path.join(data_dir, "00020.png") |
| 87 | +output_path = os.path.join(get_output_dir(), "00020.edited.png") |
| 88 | + |
| 89 | +edit_source_image_metadata(file_path, output_path) |
| 90 | +export_source_image_metadata(file_path, output_path + "_2.png", PngOptions()) |
| 91 | +overwrite_source_image_metadata(file_path, output_path + "_3.png", PngOptions()) |
0 commit comments