GraphicsMagick: How to move image content from left side to center - graphicsmagick

Situation: I have a PNG image created from a SVG by exporting the image from Internet Explorer. Although the main motive in SVG is located in the center, in the exported PNG it appears in the left half. I used IE for the export because the quality of the PNG image is far better than results obtained by other tools.
Problem: I would like to use GraphicsMagick to shift the picture to the center. I could use crop to cut off the extra empty space on the right hand side, but I don't know how to add empty transparent space on the right.
SVG image:
PNG image:
What command should I use in Graphicsmagick to transform the PNG to a PNG with the logo located in the center while preserving the original square dimensions (500x500) of the image? (I was not satisfied with quality of PNG generated by GraphicsMagick from SVG directly)

This should do the trick:
gm convert logo.svg -trim -gravity center -extent 500x500 result.png
Setting the colour for the -extent makes it clearer what is happening:
gm convert logo.svg -trim -gravity center -background pink -extent 500x500 result.png

Related

How to remove image margins when plotting and exporting square image with R?

I'm trying to plot a square TIFF image (1024 x 1024), but it displays it as a vertically stretched rectangle. I've tried adjusting margins in plotRGB and utilizing par(mar=c(0,0,0,0)), but I can't get the plot and output to be a square even though R recognizes the raster as a square.
Does anyone have any advice on how to simply fix this issue? I've attached two images, one of the original image and one of the image when plotted/exported in R. The goal of my program is to use histmatch solely to adjust the saturation of the source image to that of a matching reference image. The exported image thus cannot be vertically stretched, and must have no margin.
Screenshot of original image
R display and export of image

Crop image in R

I am trying to crop an image using the magick package in R. My goal is to crop the upper left corner portion of the image. So far my code seems to work for some images but the same dimensions do not work for other images.This is my code:
library(magick)
library(tidyverse)
image_read("http://www.gettyimages.com/gi-resources/images/Plugins/Chrometab.jpg")%>%
image_chop("0x185+190")%>%image_crop("50x55+1")
I am doing this for several images all of which have the dimensions width 320 height 240. This code seems to work for some of the images but crops portions other than the top left corner for some of the images. Is there a way to modify my code such that irrespective of the picture dimensions, it will always crop the top left corner.
I do not know R, but if I use the equivalent imagemagick commands, your chop is cropping approximately, the bottom half of the image and the subsequent crop is cropping the top left corner of the chopped bottom part. Why are you using chop? Also after a crop, the image will have a virtual canvas left if you save to PNG or TIFF that should be removed by using +repage. Not sure what that is in R, but may be repage +0+0. If you want to crop the top left corner, just use -gravity northwest and crop with the size you want WxH+0+0, unless you want an offset in place of +0+0
convert Chrometab.jpg -chop 0x185+190 +write tmp1.png -crop 50x55+1 tmp2.png
tmp1.png
tmp2.png
Whereas:
convert Chrometab.jpg -gravity northwest -crop 50x55+1+0 +repage tmp3.png
tmp3.png

R ggplot, remove white margins in ggsave/ggplot

How can I remove the white margins in ggsave?
My question is exactly the same as Remove white space (i.e., margins) ggplot2 in R. However, the answer there isn't ideal for me. Instead of trial and error for a fixed but unknown aspect ratio, I would like to give ggsave a height and weight and want my plot (ie top of title to bottom of x-label) to automatically expand to that configuration without white margin.
How can I remove the strange white margin around my .png (plotted with r, ggplot)? gives a way to make the margin transparent, but they are still there and the plot is smaller than height and width I set in the saved file.
Found the answer from Remove Plot Margins in ggplot2
theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))
does the job
In this answer linking to this blog post there is a solution which also works for different aspect ratios. You can crop the image on your hard drive, independently of OS:
knitr::plot_crop()
If you're using Unix or Mac OS, another option when the various margin options aren't trimming enough is to use the pdfcrop command available within Unix through R's ability to invoke system commands:
# after saving image, run pdfcrop
system2(command = "pdfcrop",
args = c("name_or_path_of_file_before_crop.pdf",
"name_or_path_of_file_after_crop.pdf")
)
For more, see: https://robjhyndman.com/hyndsight/crop-r-figures/
If pdf and pdfcrop aren't your thing, for example you work in png with a png logo - then see my answer here: How to save a ggplot2 graphic with the proper aspect ratio?
I ended up adding a command like this after ggsave:
system("/usr/local/bin/mogrify -trim -border 8 -bordercolor white output.png")
-trim removes an existing margin and -border 8 -bordercolor white adds a small 8px margin around the plot.
For a plot that had a gray background, a few white pixels were left around the edges of the plot, so I used the -shave option to remove a few extra pixels:
system("/usr/local/bin/mogrify -trim -shave 4x4 output.png")

With Graphicsmagick, how to combine 2 images and text in a certain format?

I need to combine 2 images and a short text into one image, in the format that the first image is at the top spanning a row, and the other image at bottom left, the text at bottom right.
The produced image is 400px * 600px, the input images can be any size, if the first image is less than 400px, it will be centered at the top
Is it possible with Graphicsmagick?
Assuming you already have two images called:
top.png (image at the top spanning a row)
left.png (other image at bottom left)
first of all you have to create the third missing image (called text.png) containing your text:
gm convert -size 200x300 xc:#7F0000 -pointsize 80 -font Arial -fill white -draw "text 10,150 'text'" text.png
then you can compose the two images in the bottom part (creating an image called bottom.png):
gm montage left.png -geometry 200x300+0+0 text.png -tile 2x1 bottom.png
and finally you can compose upper and lower parts with:
gm montage top.png -geometry 400x300+0+0 bottom.png -tile 1x2 result.png
the result will be an image called result.png, size: 400x600
top.png:
left.png:
result.png:
Tested on Windows with GraphicsMagick 1.3.18 2013-03-10 Q8

How to scale an image and kepp the lines thickness?

I have a line art, an animated transparent gif on page, made in photoshop.
#img01{
max-width:79%;
max-height:79%;
margin-top:3vh;
}
On small screen size (mobile), thickness of lines is too thin, and on screens over 1024px the lines are too thick ?
Is there a way to scale only the size of image, and keep the thickness of drawing ?
GIF is a raster-based image format. There's no way to nicely (and automatically) modify an image like that. What you're looking for is a vector-based image/animation format.
There are two obvious candidates for this:
Flash
SVG (Scalable Vector Graphics)

Resources