Search code examples
rubyimagemacrubyscripting-bridge

Converting images with MacRuby


Does anybody know how to convert images with MacRuby (or normal ruby)? I would imagine that the 'ScriptingBridge' framework would be able to handle it.

In addition, I would prefer not to use ImageMagick or the like (something lighter would be fine, however).

Thanks!


Solution

  • you can do this with MacRuby:

     framework 'AppKit'
    
     input_file_path = "/Users/username/Desktop/input_file.png"
     img_data = NSData.dataWithContentsOfFile(input_file_path)
     bitmap = NSBitmapImageRep.imageRepWithData(img_data)
    
     new_data = bitmap.representationUsingType(NSJPEGFileType, properties: nil)  # NSBMPFileType, NSGIFFileType, NSJPEGFileType, NSPNGFileType, or NSTIFFFileType.
    
     output_file_path = "/Users/username/Desktop/output_file.jpeg"
     new_data.writeToFile(output_file_path, atomically: true)
    

    and you're done, NSBitmapImageRep#representationUsingType:properties: can takes NSBMPFileType, NSGIFFileType, NSJPEGFileType, NSPNGFileType or NSTIFFFileType for bmp, gif, jpeg, png or tiff