JPG to PNG using RMagick

那年仲夏 提交于 2019-12-05 00:51:46

问题


I'm trying to convert a fetched image from JPG to PNG using RMagick, resize it and then store it on 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 does get saved as a png but when I open it in Preview, document type still says "JPEG image". In fact, the image won't even open in Photoshop unless I change the extension back to ".jpg". Am I missing something?


回答1:


Try explicitly setting the format:

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)



回答2:


For me, doing thumb.format = "PNG" didn't work, however doing thumb.format('png') does work. Most likely due to changes in 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 that helps.



来源:https://stackoverflow.com/questions/6419388/jpg-to-png-using-rmagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!