开发者

Replace images with Ruby?

If I had a folder full of thousands of images that were all the same size, could I take 1 image and replace all the others with that image (but retain the file names) with Ruby?

If so, how would you do 开发者_运维百科that exactly?


First off, if I understand your question you wish to do this:

  1. Take a directory with dog.jpg (image of a dog), cat.jpg (image of a cat) and horse.jpg (image of a horse)
  2. Choose dog.jpg as your source image
  3. Replace the image of a cat and horse with a dog while keeping their filenames
  4. Resulting in a directory with dog.jpg (image of a dog), cat.jpg (image of a dog) and horse.jpg (image of a dog)

You could use a function like this,

require 'FileUtils'

def operate_on_directory(source_image, extensions)
  Dir.glob("*.{#{extensions.join(',')}}") do |file|
    FileUtils.cp(source_image, file) unless file == source_image
  end
end

operate_on_directory("dog.jpg", ["jpg", "png"])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜