Why does cropping behave like that? - imagemagick

I've got an image with dimensions 126x75:
(rdb:1) p image.width
126
(rdb:1) p image.height
57
Now I want to crop it down to 50x50, beginning at the north west (top left corner). So I tried that (which should be similar to IMs command line geometry parameter 50x50+0+0):
image2 = image.process(:crop, :width => 50, :height => 50, :x => 0, :y => 0)
But the result is somewhat surprising to me:
(rdb:1) p image2.width
24
(rdb:1) p image2.height
16
Why does it happen like that? What's the right way?

It sounds like the image you're using has existing page geometry that is causing your (0,0) location to be offset. You can tell IM to repage the image to remove the existing page geometry so that (0,0) is the "true" (0,0). Check the IM documenation: http://www.imagemagick.org/Usage/crop/#crop_page

Related

Strange border when inserting image to PDF generated by Prawn

I have an image here at this link: http://i711.photobucket.com/albums/ww116/xuanchienuit/1.png
I am trying to insert this image as background to the pdf using the following method:
image_path = "..." #path to the image
pdf = Prawn::Document.new
pdf.image image_path, width: pdf.bounds.width, height: pdf.bounds.height
pdf.render_file "test.png"
And the output PDF is at this link: https://drive.google.com/file/d/0B5iZPOjOn1osUzJSUTdDZGROZzQ/view?usp=sharing
The problem is in the PDF, you can see a gray border on top and bottom of the image (if you don't see the border, try zooming in/out and you would see the border).
What is going on here? I think there could be something wrong with the image but I am not sure. I hope you guys will have a better thought.
Thanks in advance!
Rather than setting height and width, try setting the position, and letting the image scale to best fit the available space (match height, match width, match both)? You might -- all conjecture here -- be seeing some artifact of the image getting stretched to the pdf region (if the image dimensions don't match the pdf precisely).
pdf.image image_path,
:position => :center,
:vposition => :center,
:fit => [pdf.bounds.width, pdf.bounds.height]

Prawn : adding background image of larger resolution

I'm creating a pdf book where I need to put a background image for each page.
The size of the page is (576 x 576) and the size of the background image is 2700 x 2700 (300 dpi.) (These sizes are the requirements so cannot be adjusted).
My problem is - the background image appears out of proportion in the page. How can I fix this ?
Here's my code:
Prawn::Document.generate("#{Rails.root.to_s}/public/#{filename}.pdf", :page_size => [576,576], :left_margin => 50,
:right_margin => 50, :page_layout => :portrait, :skip_page_creation => true, :skip_encoding => true,
:background => "#{Rails.root.to_s}/public/images/pdf/bg_blank_low.jpg" ) do |pdf|
....
....
....
)
Is there any other way by which I can place the image of 300 dpi as a background image.
I even tried adding pdf template as a background, but still no luck.
Any suggestion or hint will be much appreciated.
Thanks.
What you're trying to do can't be done using the :background option of Prawn::Document.generate since the :background option can't scale the image. If you want to use the :background option, you need to make sure your image is the same dpi as the PDF (normally 72dpi).
You can however simply imbed the image in the page, like you would a normal image and then float the text over the top of it. This works since when you embed an image you're able to scale it. The code might look something like this:
Prawn::Document.generate("#{Rails.root.to_s}/public/#{filename}.pdf", :page_size => [576,576], :left_margin => 50, :right_margin => 50, :page_layout => :portrait, :skip_page_creation => true, :skip_encoding => true) do |pdf|
bg_image = "#{Rails.root.to_s}/public/images/pdf/bg_blank_low.jpg"
pdf.image bg_image, :scale => 0.2311
pdf.move_up 576
end
This will make your 'background' fully cover the page (since we manually calculated the scale 2700/576), if you want to respect your margins (this is probably a better way in general), you might alter to something like:
pdf.image bg_image, :width => pdf.bounds.width
This should automatically scale the image based on the width of the bounding box of the page. Of course you would need to change your move_up as well, something like:
pdf.move_up pdf.bounds.height
After you did this you can start putting your text in and it should appear over the top of your image and so we get our simulated scaled background.
Update
This is an update with regards to the comment. If you have pages being automatically created and you want them to have the same background then you're out of luck if you're using the current prawn release the way it is. If you really need this functionality, then you have to patch prawn.
Grab the prawn source (from https://github.com/sandal/prawn) and have a look at it. What you're after is lib/document.rb, on line 244 there is a method start_new_page, this is the one you're after. Within this method on line 280, you can see where the background is being set. Unfortunately it is using canvas which means, your image already has to be of the right size. This is why the background image doesn't scale automatically.
You will need to override this behaviour. Since this is Ruby, all you have to do is reopen the class within your project and then copy paste this method in (if you need more info on how to do this, there is plenty around on monkey-patching Ruby classes). Now you edit this method to your hearts content. The easiest way is probably to remove the canvas all together and then use our image trick from above. So the line ends up as:
image(#background, :width => bounds.width) if #background
move_up bounds.height
You can now go back to using the standard way of setting the background and everything should work.
Infact, you may even be able to get away with changing line 280 to this:
canvas { image(#background, :width => bounds.width) } if #background
And everything should work fine, saving you having to type an extra line :). Using image with the :width option should automatically scale the image, while using the :at option as prawn does won't scale the image.
Note: I haven't actually done this so you may need to work the kinks out.
If you only have one PDF page, a solution is to just don't use the background property at all, but just place the image at the first position of each page and define width as the full width with
pdf.bounds.width
If someone needs to add the picture after pages have been generated or just adding a transparent picture you can use :
pdf.transparent(0.5) do
pdf.image(file, options = {
:at => [200, 220],
:scale => 0.2
})
end

RMagick changing image extents with gravity

I've got an image that i'd like to 'pad' with white space and centre.
In most cases I need to resize the image from 16 or 32 pixels up to 32 pixels.
If the image is 16 pixels, I want to add 8px of white space on each side, making it a 32 pixel image (with the original floating in the middle).
If it's a 32 pixel image, then nothing changes.
I'm using RMagick to do the conversion:
image.change_geometry!("#{size}x#{size}") { |cols, rows, img|
newimg = img.extent(cols, rows)
newimg.write("#{RAILS_ROOT}#{path}/#{name}.png")
}
Which is working OK, but the smaller images are in the top left of the new image, not centred.
I was looking at the gravity setting, it seems to be what I need, but I can't work out how to specify it in the call?
Thanks in advance.
Check the implementation of the following carrierwave function
http://rubydoc.info/gems/carrierwave/0.5.1/CarrierWave/RMagick#resize_and_pad-instance_method
This is a version of the above method by using only RMagick dependency
require 'RMagick'
include Magick
module Converter
def self.resize_and_pad(img, new_img_path, width, height, background=:transparent, gravity=::Magick::CenterGravity)
img.resize_to_fit!(width, height)
new_img = ::Magick::Image.new(width, height)
if background == :transparent
filled = new_img.matte_floodfill(1, 1)
else
filled = new_img.color_floodfill(1, 1, ::Magick::Pixel.from_color(background))
end
# destroy_image(new_img)
filled.composite!(img, gravity, ::Magick::OverCompositeOp)
# destroy_image(img)
# filled = yield(filled) if block_given?
# filled
filled.write new_img_path
end
end
The extent() method takes two more parameters, x & y offsets, which is where the image will be placed within the extent. If you're asking extent for a 100x100 image, for example, and your original is only 50x50, you'd do img.extent(100, 100, 25, 25) -- which would set the image to start at offset 25,25 (thus centering it).
NOTE: There's some issue with extent expecting to use negative offset values (in which case you'd want to do -25, -25) -- check this:
why is the behavior of extent (imagemagick) not uniform across my machines?

Prawn: css like overflow: hidden for bounding boxes with images

I want to clip an image if it goes beyond the dimensions of a bounding box. Just like how CSS overflow: hidden would do it. Eg.
pdf.grid([0, 0], [3, 27]).bounding_box do
pdf.image image_file
end
Now currently, this image would overflow outside the bounding box if its larger than it. Is there any way to clip the image when it goes beyond the bounding box. ? I know this is possible for text when using text_box.
you can set the size of the image or get the image to scale so it fits within a certain area while maintaining proportions, do not believe you can crop an image.
If your page is not dynamic, that is the image area will always be the same this should be OK.
pdf.image "image_file_path_&_name", :position => :center, :fit => [100,450];
This is based on v0.8.4.
Unfortunately, there seems to exist no proper way to crop an image to a bounding box at the moment. Faced with this problem I figured out this beauty:
class SamplePdf
include Prawn::View
def initialize
crop_width = 100 # your width here
crop_height = 50 # your height here
image_path = '/path/to/your_image.jpg'
bounding_box [0, 0], width: crop_width, height: crop_height do
pdf_obj, _ = build_image_object(image_path)
x, y = document.send(:image_position, crop_width, crop_height, {})
document.send(:move_text_position, crop_height)
label = "I#{document.send(:next_image_id)}"
document.state.page.xobjects.merge!(label => pdf_obj)
cm_params = PDF::Core.real_params([crop_width, 0, 0, crop_height, x, y - crop_height])
document.renderer.add_content("\nq\n#{cm_params} cm\n/#{label} Do\nQ")
end
end
end
It basically adapts the Prawn::Images#image method, but skips the calculation of the image's dimensions and the scaling respectively.
It's not exactly a clean solution. Please keep me posted if you find a better one.
You should keep in mind though that this snippet leverages some implementation details which are not part of Prawn's public API and can change anytime.
At the time of writing Prawn 2.0.1 was the most recent version.

ruby prawn how to wrap text around an aligned right image?

s it posible to align an image to the right and wrap text around the image like it is in html and css using the float:right property ?
If so how do you do this ?
I can align an image but dont't know how to wrap the text around it. The text is dynamic text therefore varies alot in length.
Thanks alot
Rick
One suggestion is to try nested bounding boxes. The main bounding box would have the text inside it. with at some point another bounding box for the image. Something along the lines of
bounding_box([x,y], :width => bounds.width, :height => 400) do
text "blah"
text "blah"
# image
bounding_box([bounds.right - image_width, 0], :width => image_width) do
image("path_to_file", :at => [0,0], :width => bounds)
text "more blah"
end
You may be able to simply use the image without the bounding box, but the bounding box would ensure that the text flows around it.

Resources