RMagick posterize and other does not work - ruby

I guy's
i have write this code:
require 'RMagick'
include Magick
img = Image.read("small_img.gif").first
img.posterize
img.display
exit
the result of image don't change
i've tested blur_image, oil_paint, other but don't work
only rotate! work form me, maybe have writed too bad the code?
p.s. sorry for my bad english

This line
img.posterize
returns a new image object, it does not alter the image in place. So if you do the following
new_img = img.posterize
new_img.display
you should see results of your test.
The same applies to a lot of rmagic transformations.

Related

Display image with ruby processing

So this seems simple from all the samples I saw on github and elsewhere but sadly I can't get a simple image to display with ruby processing. Any help would be much appreciated.
My code:
attr_reader :img
def setup
size(600, 600)
#img = loadImage("chess.jpg")
end
def draw
image(#img, 10, 10)
end
The code is in ~/RP-image/scratchpad.rb The jpg is in ~/RP-image/data/chess.jpg
I've tried every permutation of load_image loadimage loadImage etc I get the response in terminal of:
The file "chess.jpg" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
Java::JavaLang::NullPointerException
processing.core.PGraphics.image(processing/core/PGraphics.java:3572)
processing.core.PApplet.image(processing/core/PApplet.java:12773)
java.lang.reflect.Method.invoke(java/lang/reflect/Method.java:483)
RUBY.draw(image-scratchpad.rb:8)
processing.core.PApplet.handleDraw(processing/core/PApplet.java:2386)
processing.core.PGraphicsJava2D.requestDraw(processing/core/PGraphicsJava2D.java:240)
processing.core.PApplet.run(processing/core/PApplet.java:2256)
java.lang.Thread.run(java/lang/Thread.java:745)
As far as I know you dont need to "add an image to the sketch" other than load it with the loadimage command.

Loading an image from a subdirectory in a gem

I used bundler to create a new gem, and I'm playing with rubygame. So far, it's going OK, except I can't get images on the Surface because I can't figure out what the path is to my images directory. I know it's sad, but I'm sorta used to Rails loading my images for me.
The full path to the image is: /usr/local/src/jewel_thief/lib/jewel_thief/images/player.png.
and I'm trying to use it in:
/usr/local/src/jewel_thief/lib/jewel_thief/player.rb (view source)
What am I doing wrong?
Change
#image = Surface.load 'jewel_thief/images/player.png'
To
#image = Surface.load '/jewel_thief/images/player.png'
I think following also work
#image = Surface.load '/jewel_thief/lib/jewel_thief/images/player.png'
The path ended up being this:
#image = Surface.load 'lib/jewel_thief/images/player.png'
Finally, after being stumped for hours. Thanks for the help though! Really.

image() modifies actual image content in MATLAB

I am displaying an image in figure window in MATLAB using following code.
im = imread('Image02.tif');
processAndDisplayImage(im);
hImage = image(im);
set(hImage,'ButtonDownFcn',#clickInImage);
But problem is that the third line above makes the image changed for some reason I don't know. Is there any way to get image handle without the modification?
UPDATE: Resolved the problem. Please refer to my answer below.
The image graphical command cannot change the image. I can only guess that it shows the image in a way you don't want it. Inspect the range of the image -
max(im(:));
and also the type:
class(im);
and try to figure out what is wrong
Perhaps you could modify processAndDisplayImage so that it returns a handle to the displayed image as an output variable?
Instead of
hImage = image(im);
I used following to solve my problem.
[hImage hfig ha] = imhandles(gcf);
But I still don't understand image command does to the actual image displayed on figure.

Properly converting a CMYK image to RGB with RMagick

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.

Convert a .doc or .pdf to an image and display a thumbnail in Ruby?

Convert a .doc or .pdf to an image and display a thumbnail in Ruby?
Does anyone know how to generate document thumbnails in Ruby (or C, python...)
A simple RMagick example to convert a PDF to a PNG would be:
require 'RMagick'
pdf = Magick::ImageList.new("doc.pdf")
thumb = pdf.scale(300, 300)
thumb.write "doc.png"
To convert a MS Word document, it won't be as easy. Your best option may be to first convert it to a PDF before generating the thumbnail. Your options for generating the PDF depend heavily on the OS you're running on. One might be to use OpenOffice and the Python Open Document Converter. There are also online conversion services you could try, including http://Zamzar.com.
Sample code to answer the comment by #aisensiy above :
require 'rmagick'
pdf_path = "/path/to/interesting/file.pdf"
page_index_path = pdf_path + "[0]" # first page in PDF
pdf_page = Magick::Image.read( page_index_path ).first # first item in Magick::ImageList
pdf_page.write( "/tmp/indexed-page.png" ) # implicit conversion based on file extension
Based on the path clue in answer to another question :
https://stackoverflow.com/a/6369524/765063
Not sure about .doc support in any open source library but ImageMagick (and the RMagick gem) can be compiled with pdf support (I think it's on by default)
PDF support is a little buggy in ImageMagick - but it's by far the best OS way for ruby. There's also a google summer of code project for pure Ruby PDF support.
I've read stuff about using OpenOffice without the GUI to transform .doc files - but it'll be complicated at best.
As the 2 previous posters said, ImageMagick's probably the easiest way to generate the thumbnails.
You could exec something like:
´convert -size 300x300 doc.pdf doc.png´
(The backquotes tell Ruby to shell it out).
If you don't want to use exec to do the conversion you could use the RMagick gem to do it for you but it's probably a bit more of code.
If you don't mind paying for Imgix, it handles PDFs too. You get all the benefits of a fast CDN with it.

Resources