I'm using ImageMagick to create a a thumb of a PDF.
/opt/local/bin/convert \
"$inputFile[0]" \
-geometry 157x200 \
-colorspace RGB \
"$ThumbFile"
But lastly we got PDF with "crop marks" (for bleeding).
Is there an option that I can use to don't take those?
How can I do this?
Example:
I would like to have only the content of the second image.
I ended by indicating to image magick to grab only the trim box:
/opt/local/bin/convert \
-geometry 157x200 \
-colorspace RGB \
-define pdf:use-trimbox=true \
"$inputFile[0]" \
"$ThumbFile"
The very simple to use -shave N parameter will remove N pixels depth from each edge of the image.
For example:
convert \
original.gif \
-shave 30 \
result.gif
This will shave of the 30 outmost rows/columns of pixels from each edge of the original image.
This also works for PDF input pages (and PNG output thumbnails, for example):
convert \
some.pdf[0] \
-scale 25% \
-shave 30 \
result.png
Related
I'm trying to cut out a shape from an image based on a mask I have. I tried a number of ways but I'm really struggling to get the result I need.
I'm using ImageMagick 7.1.0-51 Q16-HDRI on Windows.
The "base" image:
https://imgur.com/a/j1kioWu
The mask i'm using:
https://imgur.com/a/OcArICa
Expected result(photoshoped):
https://imgur.com/a/GjRoxyS
Edit: Reuploaded direct on imgur to maintain transparent background
In Imagemagick, you want to invert (negate) the polarity of black and white, then do a -compose difference -composite, then invert again.
Hair:
Face:
magick hair.png face.png -alpha off -colorspace gray -negate -compose difference -composite -negate result.png
Result:
If you want pure white in the face area, threshold the two images after the conversion to grayscale
magick hair.png face.png -alpha off -colorspace gray -auto-threshold otsu -negate -compose difference -composite -negate result2.png
Result 2:
ADDITION
If your input images have transparent backgrounds, then you have to extract the alpha channels, combine them to do the difference and then put that alpha channel back on the input.
Unix Syntax:
magick hair.webp \
\( +clone face_mask.webp -alpha extract -compose difference -composite \) \
-alpha off -compose copy_opacity -composite result.png
For Windows,remove the \ in front of the parentheses and change the end of line \ to ^.
The basic idea of these commands is to create a compare, (A compare has defined a jpeg from past and one from the present, combine such as they will slide on each other and show before after images.)
e.g. https://media.evercam.io/v1/cameras/1lowe-scnoe/compares/lower-jreyh.gif
All the commands are written below, doing these operations
Resize before after image.
create a Gif using both images.
Add a log to Gif.
Create an MP4 file of from GIF.
Create a thumbnail from mp4 file.
the logo is:
we are making animation and mp4 files using FFmpeg and ImageMagick commands such as
ffmpeg -i before_image.jpg -s 1280x720 before_image_resize.jpg
ffmpeg -i after_image.jpg -s 1280x720 after_image_resize.jpg
The above commands are first to resize both images which are going to be used in animation.
This command is being used for creating a gif.
convert after_image_resize.jpg before_image_resize.jpg -write mpr:stack -delete 0--1 mpr:stack'[1]' \\( mpr:stack'[0]' -set delay 25 -crop 15x0 -reverse \\) mpr:stack'[0]' \\( mpr:stack'[1]' -set delay 27 -crop 15x0 \\) -set delay 2 -loop 0 temp.gif
This command to add a logo to the animation.
convert temp.gif -gravity SouthEast -geometry +15+15 null: evercam-logo.png -layers Composite compa-efxfphu.gif
Then to create an mp4 file as
ffmpeg -f gif -i compa-efxfphu.gif -pix_fmt yuv420p -c:v h264_nvenc -movflags +faststart -filter:v crop='floor(in_w/2)*2:floor(in_h/2)*2' compa-efxfphu.mp4
then to create a thumbnail from this mp4.
ffmpeg -i compa-efxfphu.mp4 -vframes 1 -vf scale=640:-1 -y thumb-compa-efxfphu.jpg
Is there any possibility to reduce any of these steps? This all takes a lot of time, I am merely interested in both convert commands, can we make them into one command?
Or do you see any chance to reduce these all 4 in one? any input will be so thankful.
Updated Answer
Here is my best shot at this, it runs in about 50% of the time of the original answer...
convert -depth 8 -gravity southeast -define jpeg:size=1280x720 \
logo.png -write MPR:logo +delete \
\( before.jpg -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:before \) \
\( after.jpg -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:after \) \
+append -quantize transparent -colors 250 -unique-colors +repage -write MPR:commonmap +delete \
MPR:after -map MPR:commonmap +repage -write MPR:after +delete \
MPR:before -map MPR:commonmap +repage -write MPR:before \
\( MPR:after -set delay 25 -crop 15x0 -reverse \) \
MPR:after \( MPR:before -set delay 27 -crop 15x0 \) \
-set delay 2 -loop 0 -write anim.gif \
-delete 1--1 -resize 640x thumb.jpg
Here's my thinking...
set the depth to 8 as we are using JPEG, set gravity and initialise libjpeg's "shrink-on-load" feature,
load the logo and save to MPR as we will be using it twice,
start some "aside processing" inside (...) to load the before image, resize it, paste on the logo and save to MPR for later use,
repeat previous step for the after image,
append the before and after images together and calculate a combined colormap suitable for both and save it,
map both before and after images to new, combined colormap,
delete all junk,
animate one way,
animate the other,
make animation endless, and save animation to GIF,
delete all except first frame, resize that and save as thumbnail.
Original Answer
Still a work in progress, but you can avoid the first two invocations of ffmpeg and do the resizing within ImageMagick, and also paste the logos on top in one go like this:
convert -gravity southeast logo.png -write MPR:logo \
\( before.jpg -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:before \) \
\( after.jpg -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:after \) \
-delete 0--1 \
MPR:before \( MPR:after -set delay 25 -crop 15x0 -reverse \) \
MPR:after \( MPR:before -set delay 27 -crop 15x0 \) \
-set delay 2 -loop 0 temp.gif
You can probably also create the thumbnail with that command too, if you change the last line from:
-set delay 2 -loop 0 temp.gif
to
-set delay 2 -loop 0 -write temp.gif \
-delete 1--1 -resize 640x thumbnail.jpg
You can also speed things up and reduce memory usage by using libjpeg's "shrink-on-load" feature. Basically, you tell it before reading the disk how big a file you need and it only reads a subset of the file thereby reducing I/O time and memory pressure:
convert -gravity southeast logo.png -write MPR:logo \
\( -define jpeg:size=1280x720 before.jpg -resize ... \
\( -define jpeg:size=1280x720 after.jpg -resize ...
That reduces the time by around 25-30% on my machine.
Here's my thinking...
set the gravity first as it is a setting and remains set till changed,
load the logo and save to an MPR because we will need to paste it twice and reading from disk twice is slow,
start some "aside processing" inside (...) to load the before image, resize it, paste on the logo and save to MPR for later use,
repeat previous step for the after image,
delete all junk,
animate one way,
animate the other,
make animation endless, and save animation to GIF,
delete all except first frame, resize that and save as thumbnail.
Keywords: animation, wiper effect, animated GIF
How can I draw a grid, 1px black borders no fill, over an image?
Each section of the grid should be 480x360px.
E.g., given this 1440x1080px solid white input:
It should have a 3x3 grid drawn onto it (because 9 480x360px rectangles fit into it) to make an output kind of like this:
That's not as accurate as I'd like the command to be (I was just drawing rectangles by eye), but I hope it illustrates what I'm after.
Here is a command that will read an input image, create a 480x360 transparent cell with a black border, create a grid of those cells the size of the input image, and composite that grid over the input...
infile="MyPic.png"
convert "$infile" -size 480x360 -set option:distort:viewport "%[w]x%[h]" \
\( xc:none -shave 1x1 -compose copy -bordercolor black -border 1x1 \) \
-virtual-pixel tile -distort SRT 0 -compose over -composite output.png
That will make the lines of the grid 2 pixels thick. If the lines must be 1 pixel thick, the same sort of thing can be done with a command like this...
convert "$infile" -size 480x360 -set option:distort:viewport "%[w]x%[h]" \
\( xc:none -chop 1x1 -background black -splice 1x1 \) \
-virtual-pixel tile -distort SRT 0 -compose over -composite \
-bordercolor black -shave 1x1 -compose copy -border 1x1 output.png
EDITED TO ADD: If the objective is to divide the grid into a particular number of cells rather than cells of specified dimensions, a command like this should work...
convert "$infile" \( +clone -channel A -evaluate set 0 +channel \
-crop 3x4# -chop 1x1 -background black -splice 1x1 \) -background none \
-flatten -shave 1x1 -bordercolor black -border 1x1 output.png
That creates a clone of the input image and makes it transparent, uses "-crop 3x4#" to crop it into a grid of 3x4 cells, puts a black border on the top and left edges of the cells, then reassembles them into a grid by flattening them onto the input image. It finishes by adding a border to the right and bottom edges while preserving the input image's original dimensions.
Obviously using this method the cells may not all have the exact same dimensions if the the input image can't be divided evenly by the number of cells.
EDITED AGAIN: If you don't actually need the grid to be an overlay, you can directly crop the image into tiles, put black lines around the tiles, and reassemble them to create the required output image. That can be done with a simple command like this to make 480x360 sized cells...
convert "$infile" -background black -bordercolor black \
-crop 480x360 -chop 1x1 -splice 1x1 -flatten -shave 1x1 -border 1x1 output.png
Or it can be done with "-crop 3x3#" like this to make a grid of 3 rows and 3 columns letting ImageMagick calculate the sizes as nearly as possible...
convert "$infile" -background black -bordercolor black \
-crop 3x3# -chop 1x1 -splice 1x1 -flatten -shave 1x1 -border 1x1 output.png
Again, if the image size isn't evenly divisible by the number of cells, there will be one pixel difference in the sizes of some of the cells.
I had a little attempt at this and got kind of stymied at every turn. Here is the best I got so far.
convert xc:white[1440x1080\!] -colorspace gray -fx "(i==0||i==479||i==959||i==1439||j==0||j==359||j==719||j==1079)?0:1" grid.png
So, for practical use, you would take your snow scene and clone it. Then go to greyscale (to reduce the processing time by a factor of 3) and set all the lines black and the other pixels white. Then overlay onto the snow scene choosing the darkest pixel at each location:
convert scene.jpg \( +clone -colorspace gray \
-fx "(i==0||i==479||i==959||i==1439||j==0||j==359||j==719||j==1079)?0:1" \) -compose darken -composite result.png
If you wanted it marginally smarter and adaptive to different sized images, you could calculate the third-points as a function of image size:
magick scene.jpg \( +clone -colorspace gray \
-fx "(i==0||i==int(w/3)||i==2*int(w/3)||i==w-1||j==0||j==int(h/3)||j==2*int(h/3)||j==h-1)?0:1" \) \
-compose darken -composite result.png
The best I came up with so far:
convert blank.png -fill black -draw 'line 480,0 480,1080 line 960,0 960,1080 line 0,360 1440,360 line 0,720 1440,720' level1.png
But this is all manual.
Is there a way to automate this?
Here's another variant that is fast and easy to understand - not 100% identical to your requirements but maybe good enough.
Create a starter box with a black border, duplicate twice and append across the page, duplicate twice and append down the page, make minor correction to size:
convert xc:red[478x358\!] -bordercolor black -border 1 \
-duplicate 2,0 +smush -1 \
-duplicate 2,0 -smush -1 -transparent red -scale 1440x1080\! result.png
Or you could make your initial box, duplicate it 8 times and let montage lay the resulting 9 boxes out:
convert xc:red[478x358\!] -bordercolor black -border 1 \
-transparent red -duplicate 8,0 miff:- | montage -background none -geometry +0+0 miff:- result.png
I am using ImageMagick to generate small size JPEG versions of large TIFF images. For each TIFF image I must generate two smaller JPEG versions.
I am currently using two convert commands:
convert.exe 4096-by-3072px-120mb.tif -resize "1024x>" -strip -interlace Plane 1024px-wide-for-web.jpg
convert.exe 4096-by-3072px-120mb.tif -resize "1600x>" -strip -interlace Plane 1600px-wide-for-web.jpg
Converting TIFF to JPEG one-by-one is taking too much time. This approach is inefficient as each image is loaded across the network and processed twice. It will get worse as I plan to create more sizes for each TIFF (think 10,000 TIFFs x 5 sizes).
So, is it possible to generate two or more output files of different sizes using a single ImageMagick command?
Yes, it is possible, using the -write option:
convert 4096-by-3072px-120mb.tif -resize "1600x>" -strip -interlace Plane \
-write 1600px-wide-for-web.jpg -resize "1024x>" 1024px-wide-for-web.jpg
which rescales the input image to 1600 pixels wide, writes it out, then rescales the result to 1024 pixels wide and writes that. It's important to write the images in descending order of size, to avoid loss of quality due to scaling to a small size and then back up to a larger one.
If you prefer to rescale both images from the input image, use the +clone
option:
convert 4096-by-3072px-120mb.tif -strip -interlace Plane \
\( +clone -resize "1024x>" -write 1024px-wide-for-web.jpg +delete \) \
-resize "1600x>" 1600px-wide-for-web.jpg
In this case the order of writing images does not matter.
Here is an alternate command that uses memory program register:
magick.exe 4096-by-3072px-120mb.tif -write mpr:main +delete ^
mpr:main -resize "1024x>" -quality 80 -interlace Plane -strip -write 1024px-wide-for-web.jpg +delete ^
mpr:main -resize "1280x>" -quality 80 -interlace Plane -strip -write 1280px-wide-for-web.jpg +delete ^
mpr:main -resize "1600x>" -quality 80 -interlace Plane -strip -write 1600px-wide-for-web.jpg +delete ^
mpr:main -resize "2048x>" -quality 80 -interlace Plane -strip 2048px-wide-for-web.jpg
When tested:
the files generated with this command were identical to those created by separate convert commands
this command was twice as fast compared to separate commands
Note: ^ is the line continuation character on Windows.
I am on a Windows 7 machine using imagemagick from the command line.
The following code works to add watermarks to images:
composite \
-watermark 30% \
-gravity south \
the_watermark.png \
pic_to_be_watermaked.png \
watermark_completed.png
Problems are:
It doesn't work when I try to watermark an animated GIF
How to use this watermark command in conjunction with the command below?
This command takes all the PNGs inside the folder "static_images" and combines them into an animated GIF. It also makes sure that the image is 350px in width and the height is kept to the proper aspect ratio. It also gives the image some fuzz and sets the speed of the animation to 4 and makes sure it loops infinitely:
convert \
-resize 350x350 \
-fuzz 1.6% \
-delay 4 \
-loop 0 \
static_images/*.png \
-layers OptimizePlus \
-layers OptimizeTransparency \
animated_gif.gif
Questions:
How can I combine the watermark command with the one above?
How can I get the watermark commmand to work with animated GIFs?
Is this the result you'd like to see?
The trick is to watermark each PNG source file first, then create the GIF from the watermarked PNGs.
So it's the result of these two commands. First, create the watermarked PNGs:
for i in sample_images/000*.png; do
convert \
"${i}" \
sample_images/the_watermark.png \
-gravity south \
-composite \
sample_images/$(basename "${i}")_wm.png;
done
Note, I didn't use your exact composite command because it didn't create a readable watermark, only some blurring on the meant area...
Second, convert the watermarked PNGs to an animated GIF:
convert \
-resize 350x350 \
-fuzz 1.6% \
-delay 4 \
-loop 0 \
sample_images/*_wm.png \
-layers OptimizePlus \
-layers OptimizeTransparency \
animated_gif.gif
Is this what you meant to do?