从 Magick::Image 创建 ruby​​ File 对象

发布于 2024-11-15 10:33:45 字数 556 浏览 3 评论 0原文

我正在尝试使用 rmagick 和rest-client 将带水印的图像发布到网址。当我生成合成图像时,我将其保存 (.write),使用 File.new 将其读回,然后发布该 File 对象。但理想情况下,我想绕过写入操作,因为我再也不需要这张照片了。有什么方法可以将 Magick::Image 对象转换为 File 对象,以便我可以使用rest-client 发布它?

require 'rmagick'
require 'rest-client'

photo = Magick::Image.read('myphoto.jpg').first
water_mark = Magick::Image.read('watermark.png').first

result = photo.composite(water_mark, 0, 0, Magick::OverCompositeOp)
result.write('result.jpg')

file = File.new('result.jpg', 'rb')
RestClient.post("http://example.com", :source => file)

I'm trying to post watermarked images to a url using rmagick and rest-client. When I generate the composite image, I save it (.write), read it back in with File.new, and then post that File object. Ideally though, I'd like to bypass the write operation because I'll never need this photo again. Is there any way to convert a Magick::Image object into a File object so that I can post it with rest-client?

require 'rmagick'
require 'rest-client'

photo = Magick::Image.read('myphoto.jpg').first
water_mark = Magick::Image.read('watermark.png').first

result = photo.composite(water_mark, 0, 0, Magick::OverCompositeOp)
result.write('result.jpg')

file = File.new('result.jpg', 'rb')
RestClient.post("http://example.com", :source => file)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永言不败 2024-11-22 10:33:45

我终于使用 StringIO 和 Koala gem(Facebook API 的 ruby​​ 包装器)弄清楚了。代码如下所示:

access_token = "asdfasdfasdfasdf"
graph = Koala::Facebook::API.new(access_token)
photo = Magick::Image.read("my_photo.jpg").first
watermark = Magick::Image.read("watermark.png").first
watermarked = photo.composite(watermark, 5, 5, Magick::OverCompositeOp)
photo_graph_id = StringIO.open(watermarked.to_blob) do |strio|
  response = graph.put_picture(strio, "image/jpeg", { "message" => "hi" })
  response['id']
end

关键是调用 Magick::Image 上的 to_blob,然后从该字符串创建 StringIO。当前版本的 Koala gem 有一个 StringIO 故障,但我已在我的 fork 中修复了它并提交了拉取请求:

https://github.com/arsduo/koala/pull/122

I finally figured it out using StringIO and the Koala gem (ruby wrapper for the Facebook API). The code looks like this:

access_token = "asdfasdfasdfasdf"
graph = Koala::Facebook::API.new(access_token)
photo = Magick::Image.read("my_photo.jpg").first
watermark = Magick::Image.read("watermark.png").first
watermarked = photo.composite(watermark, 5, 5, Magick::OverCompositeOp)
photo_graph_id = StringIO.open(watermarked.to_blob) do |strio|
  response = graph.put_picture(strio, "image/jpeg", { "message" => "hi" })
  response['id']
end

The key was to call to_blob on the Magick::Image and then create a StringIO from that string. The current version of the Koala gem has a glitch with StringIO but I've fixed it in my fork and have submitted a pull request:

https://github.com/arsduo/koala/pull/122

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文