Append Images to PNG via Rmagick - ruby

for add text to image I'm doing next-
canvas = Magick::Image.read("init.png").first
gc = Magick::Draw.new
gc.pointsize(12)
gc.text(5, 207, params['property_type'])
gc.draw(canvas)
canvas.write("#tst.png")
How I can add images to exist picture(imposed from above)?

I found solution.
canvas = Magick::Image.read("init.png").first
append_image = Magick::Image.read('another.png').first
canvas.composite!(append_image, 0, 0, Magick::OverCompositeOp)

Related

PDFKit, Not able to open the pdf generated

I am trying to generate a pdf using some texts of my own (and not a html page). I tried using PDFKit. I am able to generate the pdf but then I am not able to open it in Preview (It may be damaged or use a file format that Preview doesn’t recognize.)
Below is the code,
var fs = require('fs');
PDFDocument = require ('pdfkit');
var doc = new PDFDocument
// Embed a font, set the font size, and render some text
doc.text('Some text with an embedded font!', 100, 100)
// Add another page
doc.addPage()
.text('Here is some vector graphics...', 100, 100)
// Draw a triangle
doc.save()
.moveTo(100, 150)
.lineTo(100, 250)
.lineTo(200, 250)
.fill("#FF3300")
// Apply some transforms and render an SVG path with the 'even-odd' fill rule
doc.scale(0.6)
.translate(470, -380)
.path('M 250,75 L 323,301 131,161 369,161 177,301 z')
.fill('red', 'even-odd')
.restore()
// Add some text with annotations
doc.addPage()
.fillColor("blue")
.text('Here is a link!', 100, 100)
//.underline(100, 100, 160, 27, color: "#0000FF")
.link(100, 100, 160, 27, 'http://google.com/')
// Write the PDF file to disk
doc.pipe(fs.createWriteStream('out.pdf') );
doc.end
I might be missing something small. P
It was a silly mistake on my part. I should have used doc.end() instead of using doc.end and due to this the pdf was not getting generated properly.

JavaFx Displaying Images from pixel array

I changed pixel array of an image, and I want to display it.
I tried this code (below), but it's doesn't work.
int[] pixelSrcImage;
PixelGrabber pgSrc =
new PixelGrabber(imageSrc, 0, 0, imageHeight, imageWidth, pixelSrcImage, 0,imageWidth);
pgSrc.grabPixels();
pixelSrcImage[...]=...
PixelWriter pw = null;
WritablePixelFormat<IntBuffer> format = WritablePixelFormat.getIntArgbInstance();
pw.setPixels(0, 0, imageWidth, imageHeight, format, step, 0, imageWidth);
Image imView = new Image (pw.???);
You need to define the destination image first and not set the PixelWriter to null.
WritableImage image = new WritableImage(width, height);
PixelWriter pw = image.getPixelWriter();
All I can see from your bits & pieces is that you'll get a NullPointer Exception.
And please consider what jewelsea said.

Composite manipulated images with RMagick

I'm trying to create a composite image from two original images, a background and an overlay which I manipulate using RMagick like follows:
background = ImageList.new("foo.png")
overlay_original = ImageList.new("bar.png")
overlay_resized = overlay_original.resize_to_fit(400,400)
overlay_cropped = overlay_resized.crop(NorthWestGravity, 400, 200)
new_image = ImageList.new
new_image = new_image.composite_layers(background)
new_image = new_image.composite_layers(overlay_cropped)
When I do this it gives me the following error: ArgumentError: no images in this image list
When I just try to check the length of the background and overlay_cropped image lists (background.length and overlay_cropped.length) it tells me the background image list has 1 image, but that overlay_cropped has no images:
NoMethodError: undefined method length' for bar.png PNG 640x1096=>400x200 400x400+0+0 DirectClass 8-bit:Magick::Image
Any ideas on what I'm doing wrong? I"m guessing the answer is pretty obvious.
Issue resolved by changing it to the following:
marketing_image = marketing_image.composite(background, NorthWestGravity, 0, 0, OverCompositeOp)
marketing_image = marketing_image.composite(overlay_cropped, NorthWestGravity, 327, 126, OverCompositeOp)

Creating a collage from a collection of images in Ruby

I have a collection of images that have been laid out in a rectangle to look like a collage. How can I take those images and create a single image out of them in Ruby?
For example I have three images that I want placed in the image as follows:
Image 1: (0,0) - (300,400)
Image 2: (350, 0) - (500, 200)
Image 3: (350, 220) - (500, 400)
You can try something like this with RMagick:
require 'RMagick'
bg = Image.read('bg.png') # may be a background image...
image1 = Image.read('image1.png')
image2 = Image.read('image2.png')
image3 = Image.read('image3.png')
bg.composite!(image1, 0, 0, OverCompositeOp)
bg.composite!(image2, 350, 0, OverCompositeOp)
bg.composite!(image3, 350, 220, OverCompositeOp)
bg.write('collage.png')
you probably want to use an image library like RMagick ...
http://www.imagemagick.org/RMagick/doc/

How can I crop an image in Qt?

I load a PNG image in a QPixmap/QImage and I want to crop it. Is there a function that does that in Qt, or how should I do it otherwise?
You can use QPixmap::copy:
QRect rect(10, 20, 30, 40);
QPixmap original('image.png');
QPixmap cropped = original.copy(rect);
There is also QImage::copy:
QRect rect(10, 20, 30, 40);
QImage original('image.png');
QImage cropped = original.copy(rect);
Use QImage instead of QPixmap:
QImage image("initial_image.jpg");
QImage copy ;
copy = image.copy( 0, 0, 128, 128);
copy.save("cropped_image.jpg");
This code will save a file cropped to upper left corner 128x128px.
Since you use QPixmap, you can use its copy method and supply it with a QRect to perform the actual crop.
Just use of the QPixmap's copy() functions.
This text is result of reading the first comment on your quiestion:
Sometimes it is better to wrap around an image. That is to have an image that is part of another image or in other words points to a part of another image. This is way the wrapped image does not require additional memory, except for its header. You can display or save the wrapped image without worries. The downside is that the original image must remain valid until you use the wrapped image, also if you are drawing in the wrapped image it will affect the source.

Resources