I am trying to resize an image file on my hard drive with pharo. This is my current way of doing it:
img := ImageReadWriter formFromFileNamed: 'picture.jpg'.
scaledimage := img scaledIntoFormOfSize: 100.
scaledimage writeJPEGfileNamed: 'scaledPicture.jpg'.
Is this a good way to do it? Also is there a way to specify the jpeg compression quality for this image?
Thanks!
Henne
Okay, I found the way to do this and wanted to share my solution. Here is the code:
img := ImageReadWriter formFromFileNamed: 'pic.jpg'.
scaledimage := img magnifyBy: 0.8.
PluginBasedJPEGReadWriter
putForm: scaledimage
quality: 95
progressiveJPEG: false
onFileNamed: 'scaledPicture95.jpg'.
scaledimage writeJPEGfileNamed: 'scaledPictureDfl.jpg'.
This way you can scale an image by a factor, in this case 0.8. Factors greater than 1 also work. Furthermore, you can specify the JPEG quality. I chose 95 in this example. The flag progressive JPEG can create an image, which shows up in a preview quality in a browser when loading and gets better the more data is transmitted. This is not supported by all browsers though! The last line is optional and just saves a second JPEG with default settings as a comparison. I hope this might help some people looking for this in the future.
Henne
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With