2

画像のサイズ変更/拡大縮小をしたい。オリジナルは300x200や512x600のような同じ寸法ではありません。画像のサイズを100x100に変更したいのですが、画像から何も切り抜いたり、比率を変更したりしないでください。理想的には、画像は最初に長い方の端を100(アスペクト比)に拡大縮小してから、小さい方の端を白で塗りつぶします。

 .---------.
 |- - - - -|
 |  IMAGE  |
 |- - - - -|
 '---------'

私はペーパークリップやレールを使用せず、RMagickだけを使用します。

4

4 に答える 4

8

サイズ変更された画像を新しい100x100の画像とマージすることでそれを行いました。それは確かに最善の方法ではありませんが、機能します。

img = Magick::Image.read("file.png").first
target = Magick::Image.new(100, 100) do
  self.background_color = 'white'
end
img.resize_to_fit!(100, 100)
target.composite(img, Magick::CenterGravity, Magick::CopyCompositeOp).write("file-small.png)
于 2011-01-13T17:12:49.047 に答える
1

しばらく遊んだ後、Fu86の複合トリックが次のように機能するようになりました。

img = Image.read("some_file").first().resize_to_fit!(width, height)
target = Image.new(width, height) do
    self.background_color = 'white'
end
target.composite(img, CenterGravity, AtopCompositeOp).write("some_new_file")

AtopCompositeOpCopyCompositeOpなんらかの理由で背景の一部が黒くなったよりもうまくいくようです。

于 2013-03-20T02:24:30.833 に答える
1
image = Magick::Image.read("filename").first
resized = image.resize_to_fit(width, height)     # will maintain aspect ratio, so one of the resized dimensions may be less than the specified dimensions
resized.background_color = "#FFFFFF"             # without a default, background color will vary based on the border of your original image
x = (resized.columns - width) / 2                # calculate necessary translation to center image on background
y = (resized.rows - height) / 2
resized = resized.extent(width, height, x, y)    # 'extent' fills out the resized image if necessary, with the background color, to match the full requested dimensions. the x and y parameters calculated in the previous step center the image on the background.
resized.write("new_filename")

注:この投稿の時点でimagemagick 6.5.7-8を使用しているherokuでは、xとyの変換に-1を掛ける(そして正の数を送る)必要がありました。バージョン 6.8.0-10 では、負の数が予期されます。

于 2013-11-20T11:52:13.703 に答える
0

change_geometryを使用したいようです...

于 2011-02-19T23:42:00.693 に答える