I'm using ruby 2.1.0, rmagick 2.15.4, ImageMagick 6.7.7-10
I'd like to load a JPEG file and then save in the GIF format.
x = Magick::Image.read("a.jpg").first
puts "Start write..."
x.format = "GIF"
x.write("a.gif")
puts "Done."
Gives me this:
Start write...
cli.rb:104:in `exit': no implicit conversion from nil to integer (TypeError)
The stack trace includes foreman and thor gems, but no step in my code.
The filesystem has a.gif defined, but the filesize is zero.
UPDATE
I think I have a problem with ImageMagick itself. Here's what happens on the command line:
$ convert -debug a.jpg a.gif
convert.im6: unrecognized event type `a.jpg' #error/convert.c/ConvertImageCommand/1135.
It looks like this question was answered here:
JPG to PNG using RMagick
Just changing the file name will not convert he file format.
Yes it will. Something else is going on here.
"This makes it easy to convert an image file to another format. Simply write the image file using a name that has either a prefix or a suffix corresponding to the format you want."
https://rmagick.github.io/imusage.html
Related
I run the below code for pdf to png conversion using grim
pdf = Grim.reap(File.dirname(__FILE__)<<"/pdf.pdf")
count = pdf.count
pdf[3].save('like.png')
text = pdf[3].text
pdf.each do |page|
puts page.text
end
It shows following error:
No such file or directory - gs -dNODISPLAY -q -sFile=./pdf.pdf
C:/Ruby200/lib/ruby/gems/2.0.0/gems/grim-1.3.0/lib/pdf_info.ps (Errno::ENOENT)
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/grim-1.3.0/lib/grim/image_magic
k_processor.rb:21:in count'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/grim-1.3.0/lib/grim/pdf.rb:35:i
ncount'
from pdfpng.rb:18:in `'"
It looks like the Ghostscript dependency is missing as that provides the gs binary, or you do have that installed but it's not present in your PATH.
I wrote a ruby script to convert psds to jpgs using Imagemagick.
When i do:
convert test.psd[0] -scale 300x300 test.jpg
This conversion works fine. But if I have a list of psds and if i get them in a loop, How to achieve the same command.
When i do, i get an error - convert: no decode delegate for this image format
File.open("#{file_name}", "wb") do |f|
f.write(data) #create a psd file
jpg_file = file_name.gsub(".psd",".jpg")
psd_file = file_name.gsub(".psd",".psd[0]")
system('convert "#{psd_file}" -scale 302x302 "#{jpg_file}" ')
end
Am i missing anything? Do i have to use rmagick?
Thanks in Advance :)
Ruby only interpolates double-quoted strings. Try this instead:
system("convert '#{psd_file}' -scale 302x302 '#{jpg_file}' ")
You might also want to move the end to just after the f.write.
Also, please note that "#{file_name}" is exactly equivalent to file_name.
process_file = abc.psd[0]
jpg_file = abc.jpg
system( "convert #{process_file} -profile '#{Rails.root}/lib/folder_name/AdobeRGB1998.icc' -profile '#{Rails.root}/lib/folder_name/sRGB_IEC61966-2-1_black_scaled.icc' -scale 3072x3072 -quiet #{jpg_file} ")
The path to the required colorprofiles are specifed by '#{Rails.root}/lib/folder_name/AdobeRGB1998.icc' and '#{Rails.root}/lib/folder_name/sRGB_IEC61966-2-1_black_scaled.icc'
Some options of ImageMagick are not supported by RMagick. Also sometimes it is in fact more convenient to use ImageMagick. Is there a method (of the Image object) that allows you to send commands directly to ImageMagick using the command line interface?
Would it be ok for you to use system or system call via backticks (or similar)?
You may also extend Magick::Image to do it easier.
My example code add a method convert to call the convert-command of imagemagick for the actual image. The action to be performed must be added.
require 'rmagick'
module Magick
class Image
def convert( cmd )
`convert #{self.filename} #{cmd}`
end
end
end
picname = Dir['*.jpg'].first
img = Magick::Image.read(picname).first
img.convert('x.png')
Title says everything.
I'm using MiniExiftool, a ruby interface to Perl's Exiftool.
https://github.com/janfri/mini_exiftool
http://www.sno.phy.queensu.ca/~phil/exiftool/
Usage:
exif = MiniExiftool.new(file_path)
exif.date_time_original = Time.now
exif["captionextract"] = "This is my new caption"
exif.save
There is also: https://github.com/janfri/multi_exiftool
Have you tried exifr?
EXIF Reader is a module to read EXIF from JPEG and TIFF images.
Try ruby-libexif: http://raa.ruby-lang.org/project/ruby-libexif/
Usage: http://74.125.113.132/search?q=cache:yjOeG4UnD38J:tach.arege.net/trac/browser/debian/libexif-ruby/trunk/exif.rd.en%3Frev%3D51%26format%3Draw+ruby+set+exif+tag&cd=10&hl=en&ct=clnk&gl=us&client=firefox-a
I am writing a program in Ruby which will search for strings in text files within a directory - similar to Grep.
I don't want it to attempt to search in binary files but I can't find a way in Ruby to determine whether a file is binary or text.
The program needs to work on both Windows and Linux.
If anyone could point me in the right direction that would be great.
Thanks,
Xanthalas
libmagic is a library which detects filetypes. For this solution I assume, that all mimetype's which start with text/ represent text files. Eveything else is a binary file. This assumption is not correct for all mime types (eg. application/x-latex, application/json), but libmagic detect's these as text/plain.
require "filemagic"
def binary?(filename)
begin
fm= FileMagic.new(FileMagic::MAGIC_MIME)
!(fm.file(filename)=~ /^text\//)
ensure
fm.close
end
end
gem install ptools
require 'ptools'
File.binary?(file)
An alternative to using the ruby-filemagic gem is to rely on the file command that ships with most Unix-like operating systems. I believe it uses the same libmagic library under the hood but you don't need the development files required to compile the ruby-filemagic gem. This is helpful if you're in an environment where it's a bit of work to install additional libraries (e.g. Heroku).
According to man file, text files will usually contain the word text in their description:
$ file Gemfile
Gemfile: ASCII text
You can run the file command through Ruby can capture the output:
require "open3"
def text_file?(filename)
file_type, status = Open3.capture2e("file", filename)
status.success? && file_type.include?("text")
end
Updating above answer with such example, when file name includes "text":
file /tmp/ball-texture.png
/tmp/ball-texture.png: PNG image data, 11 x 18, 8-bit/color RGBA, non-interlaced
So updated code will be like:
def text_file?(filename)
file_type, status = Open3.capture2e('file', filename)
status.success? && file_type.split(':').last.include?('text')
end