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:
- Take a directory with
dog.jpg (image of a dog)
,cat.jpg (image of a cat)
andhorse.jpg (image of a horse)
- Choose dog.jpg as your source image
- Replace the image of a cat and horse with a dog while keeping their filenames
- Resulting in a directory with
dog.jpg (image of a dog)
,cat.jpg (image of a dog)
andhorse.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"])
精彩评论