-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Evgeniy Sidenko
committed
Jun 5, 2024
1 parent
413826e
commit c6c3351
Showing
5 changed files
with
196 additions
and
3 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
Examples/src/modifyingandconvertingimages/KernelFilters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# GROUP: MODIFYING_AND_CONVERTING_IMAGES | ||
import os, random | ||
import aspose.pycore as aspycore | ||
from aspose.imaging import Image, RasterImage, VectorImage | ||
from aspose.imaging.imagefilters.complexutils import Complex | ||
from aspose.imaging.imagefilters.convolution import * | ||
from aspose.imaging.imagefilters.filteroptions import * | ||
|
||
|
||
# Initialization | ||
def get_data_root_dir_local(): | ||
if 'BASE_DIR' in os.environ: | ||
return os.environ["BASE_DIR"] | ||
return "." | ||
|
||
|
||
if 'get_data_root_dir' not in dir(): | ||
get_data_root_dir = get_data_root_dir_local | ||
if 'get_output_dir' not in dir(): | ||
get_output_dir = get_data_root_dir_local | ||
|
||
delete_output = 'SAVE_OUTPUT' not in os.environ | ||
|
||
|
||
def delete_file(file): | ||
if delete_output: | ||
os.remove(file) | ||
|
||
|
||
# Example code: | ||
def filter(raster, options, output_path): | ||
raster.filter(raster.bounds, options) | ||
raster.save(output_path) | ||
|
||
|
||
def get_random_kernel(cols, rows): | ||
custom_kernel = [0.0] * (cols * rows) | ||
for y in range(rows): | ||
for x in range(cols): | ||
custom_kernel[y*cols + x] = random.random() | ||
|
||
return custom_kernel | ||
|
||
|
||
size: int = 5 | ||
sigma: float = 1.5 | ||
angle: float = 45 | ||
custom_kernel = get_random_kernel(size, 7) | ||
custom_complex = ConvolutionFilter.to_complex(custom_kernel) | ||
kernel_filters = [ | ||
# convolution filters | ||
ConvolutionFilterOptions(ConvolutionFilter.get_emboss_3x3()), | ||
ConvolutionFilterOptions(ConvolutionFilter.get_emboss_5x5()), | ||
ConvolutionFilterOptions(ConvolutionFilter.get_sharpen_3x3()), | ||
ConvolutionFilterOptions(ConvolutionFilter.get_sharpen_5x5()), | ||
ConvolutionFilterOptions(ConvolutionFilter.get_blur_box(size)), | ||
ConvolutionFilterOptions(ConvolutionFilter.get_blur_motion(size, angle)), | ||
ConvolutionFilterOptions(ConvolutionFilter.get_gaussian(size, sigma)), | ||
ConvolutionFilterOptions(custom_kernel), | ||
GaussianBlurFilterOptions(size, sigma), | ||
SharpenFilterOptions(size, sigma), | ||
MedianFilterOptions(size), | ||
# deconvolution filters | ||
DeconvolutionFilterOptions(ConvolutionFilter.get_gaussian(size, sigma)), | ||
DeconvolutionFilterOptions(custom_kernel), | ||
DeconvolutionFilterOptions(custom_complex), | ||
GaussWienerFilterOptions(size, sigma), | ||
MotionWienerFilterOptions(size, sigma, angle) | ||
] | ||
|
||
data_dir = os.path.join(get_data_root_dir(), 'png') | ||
input_paths = [ | ||
os.path.join(data_dir, "template.png") | ||
] | ||
outputs = [] | ||
for input_path in input_paths: | ||
for i, options in enumerate(kernel_filters): | ||
with Image.load(input_path) as image: | ||
output_path = f"{input_path}-{i}.png" | ||
if aspycore.is_assignable(image, RasterImage): | ||
raster = aspycore.as_of(image, RasterImage) | ||
filter(raster, options, output_path) | ||
outputs.append(output_path) | ||
elif aspycore.is_assignable(image, VectorImage): | ||
vector = aspycore.as_of(image, VectorImage) | ||
vector_as_png = input_path + ".png" | ||
if not os.path.exists(vector_as_png): | ||
vector.save(vector_as_png) | ||
outputs.append(vector_as_png) | ||
|
||
with Image.load(vector_as_png) as png: | ||
filter(aspycore.as_of(png, RasterImage), options, output_path) | ||
outputs.append(output_path) | ||
|
||
# Removing all output files | ||
for p in outputs: | ||
delete_file(p) |
37 changes: 37 additions & 0 deletions
37
Examples/src/modifyingandconvertingimages/dicom/ModifyDicomTags.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# GROUP: TEST_FILE_FORMATS | ||
from aspose.pycore import as_of | ||
from aspose.imaging.fileformats.dicom import DicomImage | ||
from aspose.imaging import Image | ||
import os | ||
|
||
|
||
# Initialization | ||
def get_data_root_dir_local(): | ||
if 'BASE_DIR' in os.environ: | ||
return os.environ["BASE_DIR"] | ||
return "." | ||
|
||
|
||
if 'get_data_root_dir' not in dir(): | ||
get_data_root_dir = get_data_root_dir_local | ||
|
||
if 'get_output_dir' not in dir(): | ||
get_output_dir = get_data_root_dir_local | ||
|
||
|
||
# Example code: | ||
print("Running example ModifyDicomTags") | ||
# The path to the documents directory. | ||
data_dir = os.path.join(get_data_root_dir(), 'dicom') | ||
out_file = os.path.join(get_output_dir(), 'output.dcm') | ||
|
||
with as_of(Image.load(dataDir + "file.dcm"), DicomImage) as image: | ||
image.file_info.update_tag_at(33, "Test Patient") # "Patient's Name" | ||
image.file_info.add_tag("Angular View Vector", 234) | ||
image.file_info.remove_tag_at(29) # "Station Name" | ||
image.save(out_file) | ||
|
||
if 'SAVE_OUTPUT' not in os.environ: | ||
os.remove(out_file) | ||
|
||
print("Finished example ModifyDicomTags") |
59 changes: 59 additions & 0 deletions
59
Examples/src/modifyingandconvertingimages/dicom/RemoveAndSaveWithMetadata.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# GROUP: MODIFYING_AND_CONVERTING_IMAGES | ||
from aspose.pycore import as_of, is_assignable | ||
from aspose.imaging.fileformats.dicom import DicomImage | ||
from aspose.imaging import Image | ||
from aspose.imaging.exif import IHasExifData | ||
from aspose.imaging.imageoptions import DicomOptions | ||
import os | ||
|
||
|
||
# Initialization | ||
def get_data_root_dir_local(): | ||
if 'BASE_DIR' in os.environ: | ||
return os.environ["BASE_DIR"] | ||
return "." | ||
|
||
|
||
if 'get_data_root_dir' not in dir(): | ||
get_data_root_dir = get_data_root_dir_local | ||
|
||
if 'get_output_dir' not in dir(): | ||
get_output_dir = get_data_root_dir_local | ||
|
||
|
||
# Example code: | ||
|
||
# The path to the documents directory. | ||
data_dir = os.path.join(get_data_root_dir(), 'dicom') | ||
out_file1 = os.path.join(get_output_dir(), 'output.dcm') | ||
out_file2 = os.path.join(get_output_dir(), 'output_remove.dcm') | ||
out_file3 = os.path.join(get_output_dir(), 'output_modify.dcm') | ||
|
||
def exportWithMetadata(inputPath, outputPath, exportOptions): | ||
with Image.load(inputPath) as image: | ||
exportOptions.keep_metadata = True | ||
image.save(outputPath, exportOptions) | ||
|
||
def removeMetadata(inputPath, outputPath, exportOptions): | ||
with Image.load(inputPath) as image: | ||
image.remove_metadata() | ||
image.save(outputPath, exportOptions) | ||
|
||
def modifyMetada(inputPath, outputPath, exportOptions): | ||
with Image.load(inputPath) as image: | ||
if is_assignable(image, IHasExifData): | ||
hasExif = as_of(image, IHasExifData) | ||
if hasExif.exif_data is not None: | ||
hasExif.exif_data.user_comment = f"Modified at {datetime.now()}" | ||
exportOptions.keep_metadata = True | ||
|
||
image.save(outputPath, exportOptions) | ||
|
||
exportWithMetadata(os.path.join(data_dir, "file.dcm"), out_file1, DicomOptions()) | ||
removeMetadata(os.path.join(data_dir, "file.dcm"), out_file2, DicomOptions()) | ||
modifyMetada(os.path.join(data_dir, "file.dcm"), out_file3, DicomOptions()) | ||
|
||
if 'SAVE_OUTPUT' not in os.environ: | ||
os.remove(out_file1) | ||
os.remove(out_file2) | ||
os.remove(out_file3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters