patchGNU Octave - Patches: patch #10316, Add Resolution and ResolutionUnit...

 
 

patch #10316: Add Resolution and ResolutionUnit parameters to imwrite.

Submitter:  None
Submitted:  Sat 18 Feb 2023 04:37:02 AM UTC
   
 
Category:  Core : new feature Priority:  5 - Normal
Status:  None Privacy:  Public
Assigned to:  None Originator Email:  -email is unavailable-
Open/Closed:  Open
* Mandatory Fields

Add a New Comment Rich Markup
   

Sun 19 Feb 2023 05:53:41 AM UTC, comment #1: 

I just noticed that imfinfo returns the resolution in the struct as XResolution and YResolution; rather than as a single Resolution vector. Let me know if I should adjust my patch to use separate XResolution and YResolution params on this basis -- although I think I prefer the way it currently is (the MATLAB behaviour of saying YResolution will be the same as XResolution unless specifically overridden seems messy to me; it's non-obvious what happens if only "XResolution" is set without checking the documentation.)

Also, would it make sense for me to add tests to imwrite.m that write the file out with custom resolution settings, and then validate that they have been correctly set by reading them back in with imfinfo?

Anonymous
Sat 18 Feb 2023 04:37:02 AM UTC, original submission:  

To clarify, this patch is all about setting the DPI/resolution typically stored in the image's EXIF tags and measured in, e.g. pixels per inch -- as opposed to the more common meaning of "resolution" as measured in pixels.

For context, the follow are how DPI/resolution can be set on TIFF and PNG images in MATLAB (MATLAB does not support setting resolution for any other formats):


imwrite(img, "tiff.tif", "Resolution",  300);  % Generates a 300 pixels-per-inch TIFF file.
imwrite(img, "png.png",  "XResolution", 300, "ResolutionUnit", "meter");    % Generates a 300 pixels-per-meter PNG file.
imwrite(img, "png.png",  "XResolution", 300, "ResolutionUnit", "unknown");  % Generates a 300 pixels-per-<unknown> PNG file.
imwrite(img, "png.png",  "XResolution", 300);  % Generates a 300 pixels-per-<unknown> PNG file.


Given that Octave's philosophy appears to be to not copy MATLAB's inconsistencies, I propose that Octave's resolution setting should be as follows:


% filename = *.tif, *.png, *.jpg, *.bmp.
imwrite(img, filename, "Resolution", 300);                                 % Generates a 300 pixels-per-inch image file.
imwrite(img, filename, "Resolution", 300, "ResolutionUnit", "inch");       % Generates a 300 pixels-per-inch image file.
imwrite(img, filename, "Resolution", 300, "ResolutionUnit", "centimeter"); % Generates a 300 pixels-per-cm image file.
imwrite(img, filename, "Resolution", 300, "ResolutionUnit", "meter");      % Generates a 300 pixels-per-meter image file.
imwrite(img, filename, "Resolution", [300 600], "ResolutionUnit", "inch"); % Generates a non-unity PAR - 300 ppi x 600 ppi.


This will work perfectly with existing MATLAB code that writes TIFFs, but will be incompatible with existing MATLAB code that writes PNGs. In my opinion this is a worthwhile tradeoff, as the code to simultaneously support Resolution, XResolution nad YResolution tags (and have a different default resolution unit depending on the file format) is complex, and the resulting documentation ends up super repetitive and confusing.

I've attached a diff that implements my proposal above, happy to implement any suggestions in code review.

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #54375:  imwrite_resolution.diff added by None (6KiB - application/octet-stream)

 

Depends on the following items: None found

Items that depend on this one: None found

 

CC list is empty

 

There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

Only logged-in users can vote.

 

Follows 1 latest change.

Date Changed by Updated Field Previous Value => Replaced by
2023-02-18 None Attached File- Added imwrite_resolution.diff, #54375

Back to the top

Powered by Savane 3.13-02a9.
Corresponding source code