mlampros / OpenImageR

Image processing Toolkit in R
https://mlampros.github.io/OpenImageR/
57 stars 10 forks source link

Metadata retention with rotateFixed function? #23

Closed RlongWPZ closed 2 years ago

RlongWPZ commented 2 years ago

Please briefly describe your problem and what output you expect. If you have a question, you also have the option of https://stackoverflow.com/ (but I'm flexible if it's not too complicated)

I'm using the "rotateFixed" function and am wondering if it maintains Jpeg metadata, specifically the "Date/Time created" fields? On rotated images I see correct "Date Created" information when viewing metadata on some platforms (e.g., Lightroom, Bridge), but have had others (e.g., the new Wildlife Insights camera trap platform) not correctly read the original/correct Date/Time metadata.

I'm not necessarily looking for a fix, but just to get confirmation if the code is meant to retain correct metadata after rotation.

Please include a minimal reproducible example

Please give a brief description of the problem

Please add your Operating System (e.g., Windows10, Macintosh, Linux) and the R version that you use (e.g., 3.6.2) I'm running a new installation of R and RStudio on a new MacBook Pro under OSx 12.1 Monterey.

If my package uses Python (via 'reticulate') then please add also the Python version (e.g., Python 3.8) and the 'reticulate' version (e.g., 1.18.0)

mlampros commented 2 years ago

hi @RlongWPZ,

the OpenImageR::readImage uses internally the jpeg R package. In the jpeg::readJPEG(source, native = FALSE) the user has the option to also receive an image raster (with different attributes) by setting the parameter native to TRUE,


# source file downloaded from: https://jpeg.org/jpegxt/

url = 'https://jpeg.org/images/jpegxt-home.jpg'

pth_JPEG = tempfile(pattern = '.jpg')
download.file(url = url, destfile = pth_JPEG)

new_pth_name = glue::glue("{pth_JPEG}.jpg")
file.rename(from = pth_JPEG, to = new_pth_name)

image_raster = OpenImageR::readImage(path = new_pth_name, native = TRUE)
str(image_raster)

attributes(image_raster)

image = OpenImageR::readImage(path = new_pth_name, native = FALSE)
str(image)

attributes(image)

r = OpenImageR::rotateFixed(image, 90)

OpenImageR::imageShow(r)

Therefore, any metadata related to your uploaded image can be retrieved using the OpenImageR::readImage function. However, the OpenImageR::rotateFixed can not hold or return any metadata (it calls internally Rcpp code). Is your intention to attach metadata to the output of the OpenImageR::rotateFixed function (before saving the image to a file)?

RlongWPZ commented 2 years ago

Hi. Thanks for the info. All I need to do is batch rotate a folder of thousands of image files 90 degrees one way or the other. I'm only a rudimentary R user, and had a friend write me a script that uses rotateFixed to rotate a jpeg image 90 degrees one way or the other (I change the code from 90 to 270 for "clockwise" and "counterclockwise," respectively). But, these images need to retain their "Created Date/Time" exif metadata after rotation, or have it written back I suppose. Currently it seems like in Adobe Bridge I can see the original "Created Date/Time" but, in other software it appears to be lacking. Any thoughts would be helpful.

mlampros commented 2 years ago

I think the exifr R package allows a user to retrieve the time created and modified of an image (you need Perl regarding the system requirements),


url = 'https://jpeg.org/images/jpegxt-home.jpg'

pth_JPEG = tempfile(pattern = '.jpg')
download.file(url = url, destfile = pth_JPEG)

new_pth_name = glue::glue("{pth_JPEG}.jpg")
file.rename(from = pth_JPEG, to = new_pth_name)

metadat_jpeg = exifr::read_exif(path = new_pth_name)
metadat_jpeg

# # A tibble: 1 × 24
# SourceFile  ExifToolVersion FileName Directory FileSize FileModifyDate FileAccessDate FileInodeChange… FilePermissions FileType FileTypeExtensi… MIMEType JFIFVersion
# <chr>                 <dbl> <chr>    <chr>        <int> <chr>          <chr>          <chr>                      <int> <chr>    <chr>            <chr>    <chr>      
#   1 /tmp/Rtmpe…            12.2 .jpg43e… /tmp/Rtm…    17489 2022:03:02 08… 2022:03:02 08… 2022:03:02 08:0…          100664 JPEG     JPG              image/j… 1 1        
# # … with 11 more variables: ResolutionUnit <int>, XResolution <int>, YResolution <int>, ImageWidth <int>, ImageHeight <int>, EncodingProcess <int>,
# #   BitsPerSample <int>, ColorComponents <int>, YCbCrSubSampling <chr>, ImageSize <chr>, Megapixels <dbl>

Then to save the output image with the metadata you might have to use the exiftool_call function as described in an issue