I wanted to convert my jpeg image to a tiff image with some criteria, all the factor is met but I am not able to figure out how to decrease the size, currently, the image is becoming 1.2MB after conversion, I want to decrease it to less than or equal 100kb with resolution of 200 dpi. The original jpeg image is 250kb with 456 X 679.
How to do it?
require 'mini_magick'
class FileConversionService
def self.to_tiff
input_path = "/images/1.jpg"
image = MiniMagick::Image.open(input_path)
k = image.format("tiff")
k.write("/images/1_tiff")
tiff_path = "/images/1_tiff"
image_data = MiniMagick::Tool::Convert.new do |img|
img << tiff_path
img << '-resize' << "1600x734"
img << '-colorspace' << "scRGB"
img << '-density' << "700"
img << '-background' << "white"
img << '-verbose'
img << tiff_path
end
end
end
FileConversionService.to_tiff
You can add -compress
option with ZIP
or LZW
option to enable compression for the TIFF. But for most images TIFF will be bigger than JPEG. An in addition you specified the resize
option, which upscales your image to 979x734, making it a bigger image.
As additional comment, your current code converts twice, one time via
input_path = "/images/1.jpg"
image = MiniMagick::Image.open(input_path)
k = image.format("tiff")
k.write("/images/1_tiff")
tiff_path = "/images/1_tiff"
which opens the jpg and writes the tiff and the second time doing
image_data = MiniMagick::Tool::Convert.new do |img|
img << tiff_path
img << '-resize' << "1600x734"
img << '-colorspace' << "scRGB"
img << '-density' << "700"
img << '-background' << "white"
img << '-verbose'
img << tiff_path
end
which reopens the tiff file, adjusts it to the defined parameters and then outputs it again to tiff_path
. This can be done in one go using only convert:
input_path = "/images/1.jpg"
tiff_path = "/images/1.tiff"
MiniMagick::Tool::Convert.new do |img|
img << input_path
img << '-compress' << 'ZIP'
img << '-resize' << "1600x734"
img << '-colorspace' << "scRGB"
img << '-density' << "700"
img << '-background' << "white"
img << tiff_path
end