Skip to main content
Participant
February 15, 2023
Answered

SDK: photo:requestJpegThumbnail generates an invalid file (Windows)

  • February 15, 2023
  • 1 reply
  • 603 views

Hello,

I need to export thumbnails of photos I've selected with the photo:requestJpegThumbnail method. While it works great on MacOS, on Windows the exported image is not valid (no preview + error while opening the file):

 

Tested on:

- Mac OS 13.0.1 + Lr Classic 12.1 & 12.2: works well

- Windows 10 21H1 + Lr Classic 12.1 & 12.2 : FAIL

 

Steps to reproduce:

- Create a c:/temp folder (or change the hardcoded export folder in the source code)

- Install the plugin in Lr

- Select at least one photo to export in the Library module

- Select Library > Plug-in extras > Test export thumbnails

- Check the file(s) in the c:/temp folder

 

The plugin can be downloaded here: https://pierrepichot.com/wp-content/uploads/2023/02/test.lrdevplugin.zip

 

The code is the following:

local LrTasks = import 'LrTasks'
local LrApplication = import 'LrApplication'
local catalog = LrApplication.activeCatalog()

LrTasks.startAsyncTask(function()

    LrTasks.startAsyncTask(function()
        local selectedPhotos = catalog.targetPhotos
        for _, photo in ipairs( selectedPhotos ) do
            local uuid = photo:getRawMetadata("uuid")
            local previewname =  uuid .. ".jpg"
            local folder = "c:\\temp\\"
            --local folder = "/Users/Me/Documents/"
            local holdRefObj = photo:requestJpegThumbnail(1600, 1000, function( success, failure )
                local f = io.open(folder .. previewname,"w")
                f:write(success)
                f:close()
            end )	
            LrTasks.sleep(.5)			
        end
    end )
    
end )

 

Thanks!

This topic has been closed for replies.
Correct answer johnrellis

Use "wb" rather than "w" as the mode for io.open(). I think "b", binary mode, prevents the translation of "\n" to the old-fashioned "\r\n" on Windows systems.

1 reply

johnrellis
johnrellisCorrect answer
Genius
February 16, 2023

Use "wb" rather than "w" as the mode for io.open(). I think "b", binary mode, prevents the translation of "\n" to the old-fashioned "\r\n" on Windows systems.

ReiepAuthor
Participant
February 16, 2023

That's it! The solution works on both Windows & MacOS. Thanks a lot, not a bug then!

johnrellis
Genius
February 16, 2023

I always use "b". Modern Windows software is fine with Unix-style line endings ("\n").