JPG for PNG using RMagick

I am trying to convert the extracted image from jpg to png using RMagick, resize it and then save to S3:

thumb = Magick::Image.read("artist.jpg").first thumb.write("artist.png") thumb.crop_resized!(120, 120, Magick::CenterGravity) AWS::S3::S3Object.store("image.png", thumb.to_blob, AWS_BUCKET, :content_type => 'image/png', :access => :public_read) 

The image becomes saved as png, but when I open it in Preview, the document type still says “JPEG image”. In fact, the image will not even open in Photoshop unless I change the extension to ".jpg". Did I miss something?

+4
source share
2 answers

Try setting the format explicitly:

 thumb = Magick::Image.read("artist.jpg").first thumb.format = "PNG" thumb.write("artist.png") thumb.crop_resized!(120, 120, Magick::CenterGravity) AWS::S3::S3Object.store("image.png", thumb.to_blob, AWS_BUCKET, :content_type => 'image/png', :access => :public_read) 
+10
source

For me, doing thumb.format = "PNG" didn't work, however doing thumb.format('png') really works. Most likely due to changes over the past few years.

 thumb = Magick::Image.read("artist.jpg").first thumb.format("png") thumb.crop_resized!(120, 120, Magick::CenterGravity) AWS::S3::S3Object.store("image.png", thumb.to_blob, AWS_BUCKET, :content_type => 'image/png', :access => :public_read) 

Hope this helps.

0
source

All Articles