All, have been using RMagick/ImageMagick for years with great success. One of the tasks I use it for is on-demand gradient generation (pending full support for CSS gradients, oh god I can't wait).
Recently noticed a bug/behavior that I can't fathom at all. Have been trying to even come up with a theory, thought the bright minds of StackOverflow might be able to help.
In short form, generating gradients as PNGs works fine, unless you're using grayscale values, in which case the output is... strange. Try the below script.
require 'RMagick'
include Magick
puts "ImageMagick version: " + Magick_version
puts "RMagick version: " + Version
fill = GradientFill.new(0, 0, 20, 0, '#888888', '#888888')
img = Image.new(20, 20, fill)
img.write('./test.png') { self.format = 'PNG' }
The expected output would be a 20x20 png that's solid mid-gray. What I get instead is a pure white block. If I have a color for the second value, it seems to work fine. Any ideas?
For reference, I'm using:
ImageMagick version: ImageMagick 6.8.0-2 2012-11-28 Q8
RMagick version: RMagick 2.13.2
As so often happens, 5 minutes after posting this, I found the answer. Short answer, there was a short-lived bug in ImageMagick regarding writing PNGs that are pure gray. Bug was present from 6.8.0-1 to 6.8.0-8.
Upgrading should fix it...
Related
I am using Ruby and MiniMagick to do some image manipulation.
I've added a drop shadow to a png, and I'm trying to crop off a side so that I can line it up with another image without the shadow overlapping with it in a very odd looking manner.
I've tried to use chop. This works perfectly, except it will only chop off the left edge instead of the right. I've tried changing the gravity, but this is fully ignored.
page1.gravity 'East'
page1.chop '8x0'
The code above chops 8 pixels off the left, even though everything I've found indicates it should be chopping 8 off the right.
I've tried to use crop. Using just crop with a layered image returns no image at all.
I've tried to use crop and repage:
page1.crop '200x500+0+0'
page1.repage.+
This returns the following error:
/Users/elizabethday/.rbenv-usergems/2.0.0-p247/gems/mini_magick-3.6.0/lib/mini_magick.rb:409:in `run': Command ("mogrify -repage /var/folders/HH/HHJiNl08GyWfBCFvHn2R9++++TU/-Tmp-/jpg20131009-14809-1tjeck0") failed: {:status_code=>1, :output=>"mogrify: invalid argument for option `/var/folders/HH/HHJiNl08GyWfBCFvHn2R9++++TU/-Tmp-/jpg20131009-14809-1tjeck0': -repage # error/mogrify.c/MogrifyImageCommand/5558.\n"} (MiniMagick::Error)
from /Users/elizabethday/.rbenv-usergems/2.0.0-p247/gems/mini_magick-3.6.0/lib/mini_magick.rb:363:in `combine_options'
from /Users/elizabethday/.rbenv-usergems/2.0.0-p247/gems/mini_magick-3.6.0/lib/mini_magick.rb:342:in `method_missing'
from menu_thumbnails.rb:114:in `<main>'
This error indicates that it's using the -repage option, when I specifically told it to use +repage.
I've tried flattening the png, but this just ends up as a black box.
Does anyone have any suggestions on how to get this to work?
EDIT: Ok, I've discovered that, in general, gravity needs to be used in a combine_options block.
However, when using a combine_options block it still doesn't work properly.
The following block:
page1.combine_options do |c|
c.gravity 'East'
c.chop '8x0'
end
does absolutely no chopping of the image at all. So I still seem to have made no progress in getting this image properly cropped.
Put repage.+ inside of your combine_options block as well. I've been having loads of the same type of issues you're having.
page1.combine_options do |c|
c.repage.+
c.gravity 'East'
c.chop '8x0'
end
I am a Mac (10.6.8) user. I have written MATLAB code for plotting calculation results and then saving the plots as pdfs. I use "saveas" (see example below).
I get this error:
??? Error using ==> print at 325
Problem converting PostScript. System returned error: -1.Failed to convert to output format; Ghostscript status: -100.**** Unable to open the initial device, quitting.
Error in ==> saveas at 155
print( h, name, ['-d' dev{i}] )
Error in ==> Results_processor at 1219
saveas(gcf,saveFigTo1, 'pdf')
Here is the relevant piece of code:
calculationResultsPath = '/Me/Projects/ThisProject';
calculationResultsDirectory = strcat( calculationResultsPath,'MATLABProcessedResults' );
mkdir( calculationResultsDirectory );
% ...Code for importing results to be plotted from external files (works fine)...
% ...Code for plotting (works fine)... I get the figures I want.
% The problem is:
saveFigTo1 = strcat(resultsDirectory,'/majorsMgO.pdf')
saveas(gcf,saveFigTo1, 'pdf')
hold off
pause
clf;
Some further information... This worked fine last week when I first wrote it! Since then, I think I updated to Mac OS 10.6.8 from 10.6.7, but nothing else has changed in my code or in the version of Matlab I use (R2009a) (unless I have a very bad memory!).
Also, I came across some older suggestions on similar questions to use "print." I tried using:
print(gcf,'filename'). I did get a pdf, but it would not open in any pdf viewing programme. I assume (but don't know for sure) that this could be related to the fact that I use a Mac. I have noticed that there are a few things (especially related to external file manipulation) that don't run on Mac.
If anyone could help, I would be very grateful.
Update:
I found a GhostScript for Mac and installed it as Chris suggested. Unfortunately, that didn't work. I read on a forum that many Mac users are having problems with MATLAB plots at present, possibly related to java. There was an operating system update (to OS X 10.6.8) last week and that is when the problems began. My code worked before then.
I still have not found a solution to this and I don't think the MATLAB people have either, so if anyone has suggestions about how to save plots WITHOUT using saveas, I would love to hear them. The "print" command does not work for me either--it produces PDFs that I can't open.
I think the problem here is GhostScript dying and not so much Matlab. A Google for that GS error turns up many pages such as this. Is this at all applicable? If you use GS outside of Matlab does it work?
As an aside, you might take a look at this FEX submission export_fig. It has treated me well. Worst case you could output to png and convert to PDF later.
One recommendation I can make is to use a different format that OS X understands and simply convert the result to pdf with a system call
See if the following works:
% Load a test image
im = imread('cameraman.tif');
imshow(im); % display the image
saveas(gcf,'test.tif','tif');
% convert to pdf using a syscall to cupsfilter
!cupsfilter test.tif > test.pdf 2> /dev/null
% open the file with your default pdf viewer
!open test.pdf
If the above is not working, yet another approach is to get the bitmap from the figure window and write it using imwrite. Note, this method doesn't benefit from the nice font scaling features of the saveas and print. This approach assumes the im variable above still exists.
imagesc(im); colormap gray;
% Set the border color to white
set(h,'Color',[1 1 1]);
% Get the image in the figure
frame = getframe(gcf);
imout = frame.cdata;
% on OS X, the stretch window image
% appears in the bottom right corner
% of the image. Remove it.
imout = imout(10:end-9,10:end-9,:);
% Write the image out to a lossless tif
imwrite(imout,'test.tif','tif','Compression','none')
You could then convert the tifs to pdf files as above. The quality of the figure will depend on the figure size. In most cases, I wouldn't use this second approach as saveas handles the fonts nicely. Using getframe is meant only as a workaround to the true problem with saveas.
I have a page with color and black & white images, when I use wkhtmltopdf to generate a pdf from the html file,
I get only the color images, how can I get it to print all the images, I have tried the advance options, but no success.
If I print in grayscale, then all the images are coming, but I don't want grayscale.
Any help?
I figured out how to solve this problem, I tried a trial-and-error method and figured out that if we provided the following parameters all the images irrespective of color or B&W, it would come up
wkhtmltopdf --image-quality 75
NOTE: Any value <= 75 works, higher value print a blank space for B&W images only
This question is old, but I'll add this in case someone has a similar issue and is unable to upgrade.
I found that applying this CSS solved the problem of non-color image rendering for me.
img {opacity: 0.99;}
To follow on from my comment above:
Changing the image quality only helps some cases
I contacted the developer (antialize) and he has now fixed this issue on his staging branch.
There isn't a release yet with this fix, so you will have to compile it yourself.
If you compile using the latest staging branch, all images now work as expected. Remember to compile following the "Installing QT yourself" instructions.
Furthermore, if you want to compile statically, see my note (on the compilation wiki page, made on Dec 19, 2011) about editing two files to allow static compiling of webkit (the 2 files that need to change are /tools/configure/configureapp.cpp and configure).
I have been using the below to do a color conversion
if #image.colorspace == Magick::CMYKColorspace
# #image.colorspace #=> CMYKColorspace=12
#image.colorspace = Magick::RGBColorspace
#image = #image.negate
end
It works, approximately, but the color luminosity is off. The fact that I need to negate the image leaves a very bad smell.
The documentation mentions using color_profiles, but beyond that I can not find much.
I am now trying
#image = #image.quantize(16777216, Magick::RGBColorspace)
And the colors are better, but still off.
Thanks Pekka, you tipped me off to the answer (+1).
You must have ImageMagick compiled with the Little Color Management System (LCMS) installed. This may already be the case if an installer or package was used. But I was compiling from source. It was as simple as installing LCMS from source and rebuilding ImageMagick (./configure; make; make install).
In ImageMagick the below works well to reproduce accurate color:
convert FILENAME -profile /PATH_TO_PROFILE/sRGB.icm OUT.jpg
So in RMagick I use the below:
if #image.colorspace == Magick::CMYKColorspace
# Adjust the path as necessary
#image.color_profile ="/usr/local/share/ImageMagick-6.5.4/config/sRGB.icm"
end
#image.write("out.jpg") { self.quality = 85 }
I've spent a long time trying to go from a CMYK EPS to a RGB PNG using RMagick and Rails. Hopefully this will be of use to someone:
def convert_image_from_cmyk_to_rgb( image )
#puts image.alpha?
if image.colorspace == Magick::CMYKColorspace
image.strip!
image.add_profile("#{Rails.root}/lib/USWebCoatedSWOP.icc")
image.colorspace == Magick::SRGBColorspace
image.add_profile("#{Rails.root}/lib/sRGB.icc")
end
image
end
You can download the ICC files direct from Adobe at http://www.adobe.com/support/downloads/iccprofiles/iccprofiles_win.html
The only thing I haven't been able to suss is how to maintain transparency. The EPS I want to use has a transparent background which is being turned into white. Unfortunately I can't do something like image.transparent( "white" ) as I have white in the image that I want to keep as white.
If I uncomment the puts image.alpha? in the above code it returns false.
Does anyone know if what I'm trying to do is possible with the current version of RMagick, as I'm beginning to wonder if importing CMYK EPSs with transparency isn't supported.
Thanks!
The incoming files, in this case, do
have a profile. I will investigate
some more. I got lost with the color
profiles (like where do I download
them? the ICC site was not much help)
You are not the only one confused; I was too. There are discussions on the ImageMagick site that might be worth siftirng through: Here As far as I understood back then, properly working with profiles is possible when the profile used can be identified (e.g. a monitor profile) or is embedded in the file (which can be done at least for TIFF and JPG in Photoshop, I think). Check e.g. this: Here. Good luck.
I found that The Who's command line solution worked beautifully, but the RMagick solution did not work for me.
To get it to work in RMagick, I instead I had to use the Magick::Image#add_format method, which, according to the docs, will allow you to specify an source and destination profile. It looks like this:
if img.colorspace == Magick::CMYKColorspace
img.add_profile(RGB_COLOR_PROFILE)
end
RE: LCMS on Centos 5.5, be sure to download and build the latest LCMS from source (vs. yum install). Otherwise, IM won't find LCMS at build and you'll be scratching your head, like me, wondering why LCMS isn't included in IM delegate libs.
We have some code that draws things using RVG in RMagick. We scale them at various sizes. What I'm trying to do is use a file that's saved as an SVG as a template.
The problem is, is when I load an SVG using Magick::Image.read, and then 'use' it, it rasterizes it, and then scales it, instead of producing pretty vectors.
Is there a way one might go about doing this properly?
Thanks!
Thoughts:
Imagemagick is fundamentally raster oriented. If you want to keep your image in vector format, don't use Imagemagick, as the author will tell you. That said, even though imagemagick may by default convert your image to raster, you can convert it back from raster into vector format (sometimes).
SVuGy may help. It doesn't appear to have a lot of support, but may work. I haven't used it.
The rmagick developer claims to have a SVG to RVG parser, you might try contacting him at rmagick AT rubyforge DOT org.
If all else fails, punt. You could write an xslt to convert your svg to rvg. The two formats are close enough that this might not be too painful. Else a tool like genshi might be a faster conversion path for you if you know more python than xlst.