I have an image and would like to apply a watermark (another image) over it using Node.js. The requirement is watermark shouldn't be a solid picture (what I can do with gm library's draw()) but half transparent. Can you please advise any tool to use?
Use ImageMagick from the command line by forking a child-process
// Require our module dependencies
var exec = require('child_process').exec;
// Create command array to invoke ImageMagick composite where
// -dissolve is the amount of transparency for the watermark
// -gravity tells how to align images of varying size
// -quality is the image quality of the JPEG (not required if producing PNG)
var command = [
'composite',
'-dissolve', '50%',
'-gravity', 'center',
'-quality', 100,
'/path/to/watermark.png',
'/path/to/image.jpg',
'/path/to/save/result.jpg';
];
// Join command array by a space character and then execute command
exec(command.join(' '), function(err, stdout, stderr) {
// Do stuff with result here
});
If you need an API abstraction, there is a great module found here https://github.com/rsms/node-imagemagick
You could make watermark image half-transparent already and use it with gm:
gm('/path/to/input-image.jpg')
.draw(['image Over 0,0 0,0 /path/to/half-transparent-watermark-image.png'])
.write('/path/to/output-image.jpg', function(e){
console.log(e||'done'); // What would you like to do here?
});
I use code
gm('/path/to/input-image.jpg')
.draw(['image Over 0,0 0,0 /path/to/half-transparent-watermark-image.png'])
.write('/path/to/output-image.jpg', function(e){
console.log(e||'done'); // What would you like to do here?
});
Error: Command failed: convert: Non-conforming drawing primitive definition `/path/to/half-transparent-watermark-image.png''# draw.c / DrawImage / 3124
Related
I want to crop an image with Google App Script if an image outside the page frame, but as far as I checked in Google App Script documentation and I could not find a way to crop the image.
pageElements.asImage().replace (imgBlob, true); it is not allowed to pass cropping dimensions as parameters in .replace() to crop a image.
i know this can be achieved using a custom API , passing the image blob and crop area that will call cropping method on another server.
But how it will be possible to work with Google App Script, looking for expert advice.
How about this answer?
Issue:
I think that in the current stage, replace(blobSource, crop) has the limitation. The official document says as follows.
crop Boolean: If true, crops the image to fit the existing image's size. Otherwise, the image is scaled and centered.
I confirmed that when the image is cropped using replace(blobSource, crop), the center of image is left. It seems that this is the current specification. And although there is the "cropProperties" of "UpdateImagePropertiesRequest" in Slides API, unfortunately, in the current stage, this cannot be still used. This has already been reported. Ref
Sample script:
If you use replace(blobSource, crop) under the current specification, how about the following sample script? As the sample situation, 2 images of "image1" and "image2" are prepared in the 1st slide, and "image1" is cropped using "image2".
The flow of this script is as follows.
Flow:
Retrieve 2 images from a slide on Google Slides.
Crop "image1" using "image2". By this, "image2" is replaced with "image1".
Move the cropped image to "image1".
Remove the original "image1".
Script:
function myFunction() {
// 1. Retrieve 2 images from a slide on Google Slides.
var slide = SlidesApp.getActivePresentation().getSlides()[0];
var images = slide.getImages();
var image1 = images[0]; // Red image.
var image2 = images[1]; // Blue image.
// 2. Crop "image1" using "image2". By this, "image2" is replaced with "image1".
var replacedImage = image2.replace(image1.getBlob(), true);
// 3. Move the cropped image to "image1".
replacedImage.setTop(image1.getTop()).setLeft(image1.getLeft());
// 4. Remove the original "image1".
image1.remove();
}
Result:
When the script is run, "image1" is cropped. But it is found that in the current stage, the center of "image1" is left by the crop.
Note:
Slides API and Slides Service are growing now. So I think that this situation might be changed by the future update. But if you want this soon, how about requesting this to the issue tracker as the future request? Ref
References:
replace(blobSource, crop)
CropProperties
Added:
At an additional sample script for using replace(blobSource, crop), I would like to propose the method for using the self image. In this sample script, when the image is sticked out, the image of out of page is removed by cropping. The basic method is the same with above sample script.
Sample script:
function myFunction() {
var s = SlidesApp.getActivePresentation();
var slide = s.getSlides()[0];
var images = slide.getImages();
var image = images[0];
var pageWidth = s.getPageWidth();
var imagePosition = image.getLeft();
var imageWidth = image.getWidth();
var check = imagePosition + imageWidth - pageWidth;
if (check > 0 && check < imageWidth) {
image
.duplicate()
.setWidth(pageWidth - imagePosition)
.asImage()
.replace(image.getBlob(), true);
image.remove();
}
}
Result:
Note:
In this sample script, as a simple sample, I prepared only the right side of the horizontal direction. So when you want to remove the vertical direction, please modify the script for your actual situation.
I'm building a Flutter application which needs to add waermark on videos and images.
Is there any way to achieve this with firebase (or any other service)? I could do this on client-side but also there i can't find any flutter/Dart package related to video/image processing.
Kindly guide me how to do it.
Thanks
For videos:
https://github.com/tanersener/flutter-ffmpeg, though I couldn’t find anything that explicitly documents a way to edit the video, such as a watermark. But it should be possible according to others https://stackoverflow.com/a/57847155/6668797.
https://pub.dev/packages/video_manipulation
Adding still frames to an existing video, e.g. watermarks
.generateVideo(List<String> paths, String filename, int fps, double
speed).
Parameters paths list of input file paths. Can be images (.jpg or
.png) or video files (.mp4) that are used to generate the new video.
E.g.: ["documents/input.mp4", "documents/watermark.jpg]
For images:
https://pub.dev/packages/image
load, save and manipulate images in a variety of different file formats.
https://github.com/brendan-duncan/image/wiki/Examples
drawString(image, arial_24, 0, 0, 'Hello World');
As for other services, I don't know, but Firebase does not offer this service.
Otherwise, client/app-side, there’s currently not much else for videos, but there's more available for images, you can search https://pub.dev/flutter/packages?q=image+editor. However, for more options, you’ll have to seek out native Android/iOS libraries and custom integrate them through the platform channels.
For watermark images:
import 'package:extended_image/extended_image.dart';
import 'package:image/image.dart' as ui;
import 'package:path_provider/path_provider.dart';
Future<File> watermarkPicture( File picture, File watermark, String fileName) async {
ui.Image originalImage = ui.decodeImage(picture.readAsBytesSync());
ui.Image watermarkImage = ui.decodeImage((watermark.readAsBytesSync()));
ui.Image image = ui.Image(originalImage.width, originalImage.height);
ui.drawImage(image, watermarkImage);
// Easy customisation for the position of the watermark in the next two lines
final int positionX = (originalImage.width / 2 - watermarkImage.width / 2).toInt();
final int positionY = (originalImage.height - watermarkImage.height * 1.15).toInt();
ui.copyInto(
originalImage,
image,
dstX: positionX,
dstY: positionY,
);
final File watermarkedFile = File('${(await getTemporaryDirectory()).path}/$fileName');
await watermarkedFile.writeAsBytes(ui.encodeJpg(originalImage));
return watermarkedFile;
}
This is an customisation of this medium article. Because the original solution was not working for me.
For Image you can use this pacakge
https://pub.dev/packages/image_watermark
based on Image pacakge.
As parameter you have to pass image bytes and return output image bytes
and you can use Image.memory(imgBytes) widget.
Add text watermark:
var watermarkedImg = await image_watermark.addTextWatermarkCentered(imgBytes,'watermarkText');
you can customize position ,color of text
var watermarkedImg = await image_watermark.addTextWatermark(
imgBytes, ///image bytes
'watermarkText', ///watermark text
20, ///position of watermark x coordinate
30, ///y coordinate
color: Colors.green, ///default : Colors.white
)
Image watermark:
watermarkedImgBytes = await image_watermark.addImageWatermark(
imgBytes, //image bytes
imgBytes2,//watermark img bytes
imgHeight: 200, //watermark img height
imgWidth: 200, //watermark img width
dstY: 400,
dstX: 400);
For video:
https://pub.dev/packages/video_manipulation/example
_outputFilepath =
await VideoManipulation.generateVideo([inputVideopath,watermarkimgPath], outputFilename, framerate, speed);
To add image, Try this latest updated package video_watermark
VideoWatermark videoWatermark = VideoWatermark(
sourceVideoPath: videoPath,
watermark: Watermark(image: WatermarkSource.file(imagepath)),
onSave: (path){
// Get the output file path
},
);
videoWatermark.generateVideo();
I am not sure about adding watermark over video. But to help people who are looking for watermark over the image can refer to the simple article written by me.
Add Watermark over Image in Flutter
In this article, I have used Image package to apply watermark text or image over Image.There is well explained example program also written about this topic.
Good day.
How to impose white_rectangle.jpg on logo.jpg in the image below
using Imagemagic.
And a bonus question: what's Ruby's method can make the task.
def (path_to_image)
# impose white_rectangle.jpg on logo
end
This can easily be accomplished using RMagick:
require 'RMagick'
logo = Magick::Image.read("logo.jpg").first
rect = Magick::Image.read("white_rectangle.jpg").first
result = logo.composite(rect, x, y, Magick::CopyCompositeOp)
result.write "result.jpg"
An alternative is to just draw a white rectangle without using a composite image:
image = Magick::Image.read("logo.jpg").first
gc = Magick::Draw.new
gc.stroke = 'white'
gc.fill = 'white'
gc.rectangle x_start, y_start, x_end, y_end
gc.draw(image)
image.write "result.jpg"
Using ImageMagick command line tools, you can overlay one image with another like this:
$ composite white_rectangle.jpg logo.jpg -geometry +x+y result.jpg
I want to cut a circle out of an image using rmagick.
Here's an example of what I'd like to be able to accomplish:
-->
It seems like I want to use http://studio.imagemagick.org/RMagick/doc/draw.html#circle to cut a circle, and then clip_path to mask it, but the docs aren't very clear. Would anyone be able to point me in the right direction?
require 'rmagick'
im = Magick::Image.read('walter.jpg').first
circle = Magick::Image.new 200, 200
gc = Magick::Draw.new
gc.fill 'black'
gc.circle 100, 100, 100, 1
gc.draw circle
mask = circle.blur_image(0,1).negate
mask.matte = false
im.matte = true
im.composite!(mask, Magick::CenterGravity, Magick::CopyOpacityCompositeOp)
im.write 'walter_circle.png'
This is how I would do it with Imagemagick and php:
// Canvas the same size as the final image
exec("convert -size 800x533 xc:white white.jpg");
// The mask
exec("convert -size 800x533 xc:none -draw \"fill black circle 400,265 400,50\" write_mask.png");
// Cut the whole out of the canvas
exec("composite -compose Dst_Out write_mask.png white.jpg -matte step.png");
// Put the canvas over the image and trim off excess white background
exec("convert IMG_5745.jpg step.png -composite -trim final.jpg");
You should be able to follow the process?
Cleanup tempory images afterwards - I tend to save the tempory images in a .miff format and then write a loop to delete all .miff images afterwards. Alternativly just leave them and if you use the same name for the tempory images they will be overwritten every time the code is run.
I try to convert SVG to PNG. Result picture has a white background I need transparent.
Sample of code:
wand = NewMagickWand()
MagickReadImage(wand,tmp_file_name)
transparentColor = NewPixelWand()
PixelSetMagickColor(transparentColor, 'none')
MagickSetBackgroundColor(wand, transparentColor)
MagickWriteImage(wand,new_filename)
if I do in command-line:
convert -background 'transparent' ./media/2222222.svg ./media/2222222.png
I've got a transparent picture.
I used subprocess and I got what I want
args = ['convert', '-background', 'transparent', './media/2222222.svg', './media/2222222.png',]
result = subprocess.call(args)