Resize and crop image using graphicsmagick - graphicsmagick

I'm using gm convert -geometry "XxY^>", this does resize the image but it doesn't crop it. Suppose I've got a 100 x 100 image and want to have a 20 x 30 version, the current command will return a 30 x 30 version. How do I crop this to 20 x 30?
Just -crop 20x30 returns the left part, while I'd like to have the center part.

I think you want to set the gravity before cropping:
gm convert image.png -gravity center -crop 20x30+0+0 result.png

Related

how to change the dimensions of a jpg image in R?

I always resized images in CorelDraw from 1219,20 x 914,40mm to 78.36x51.00 to make photo boards. But now it turns out I have a lot of images in different folders and I needed to create an auto-lop to do this for me. If I were to do this in Corel like I used to do, it would take a lot of time.
I have used the the resize function from Magick package, but didn't have obtain sucess.
resize(Image, "78.36x51.00!")
Error in resize("78,36x51,00!") :
Not compatible with requested type: [type=character; target=integer].
So I also tried the image_scale function, in this case the dimensions changed, but the size and resolution of the image was much smaller than expected.
image_scale(Image, "78.36x51.00!")
Demonstration with the generated image after the resize (photo) and the expected size (white square)
Documentation indicates that magick's scaling functions are based on pixels. Scaling based on X and Y dimensions expressed as pixels is shown below.
I'm not sure if this directly addresses the issue because the units in the question seem to change and dots per inch (dpi) is not defined. 1219,20 x 914,40 is in mm, but the units for 78.36 x 51.00 are undefined. The first pair of numbers has an aspect ratio of 1.3 while the second is 1.5.
Scaling by X or Y in pixels will retain the original aspect ratio. Getting the right size involves knowing the desired dpi.
install.packages("magic")
library(magick)
Image <- image_read("https://i.stack.imgur.com/42fvN.png")
print(Image)
# Increase 300 in X dimension
Image_300x <- image_scale(Image, "300")
print(Image_300x)
# Increase 300 in y
Image_300y <- image_scale(Image, "x300")
print(Image_300y)

Assemble/combine images with overlapping in R

I have in a folder a large number of small images 64x64 pixels. I load all of these image in a list.
setwd("C:/Users/PC/Pictures/")
jpeg <- list.files(folder)
I would like to merge/assemble all these images to create one large. I would like to paste the image N+1 on the previous image N with a gap of 8 pixels, from image 1 to k. So the image N+1 will cover partially the previous image N. How to do it?

How to use pnmscale to scale the longer side of an image and still keep the ratio?

I'm writing a GNU Makefile to do some processing on images.
One task is to scale the image (.ppm format) by a SIZE parameter using the pnmscale command. The image should be scaled by the longer side without loosing the ratio and should be saved under .scaled .
I've read the man page of pnmscale but couldn't seem to find the right option.
I've tried the following:
pnmscale -pixels 100 example.ppm > example.scaled
When example.ppm has the size 200 x 100 pixels and I run the pnmscale command with the size of 100 pixels, example.scaled should have the size of 100 x 50 pixels. With my solution the image gets very small.
As the manpage of pnmscale states, the option pixels
specifies a maximum total number of output pixels. pnmscale scales the image down to that number of pixels. If the input image is already no more than that many pixels, pnmscale just copies it as output; pnmscale does not scale up with -pixels.
In other words, by specifying -pixels 100, you're actually scaling down your image to a maximum number of 100 pixels. What you're trying to achieve is to scale down your input image to a size of 100 x 50 pixels = 5000 pixels.
Looking again at the manpage of pnmscale yields the following:
pnmscale [{-xsize=cols | -width=cols | -xscale=factor}] [{-ysize=rows | -height=rows | -yscale=factor}] [pnmfile]
[...]
If you specify one dimension as a pixel size and don't specify the other dimension, pnmscale scales the unspecified dimension to preserve the aspect ratio.
In your case, using
pnmscale -xsize 100 example.ppm > example.scaled
should shrink your input image to a width of 100 pixels.

R ImageMagick. How to fill subset of image a single color

I am trying to take an image, and based on previous data, make a small square (for example top left corner at 100,100 with width,height of 50x50) all white, and the rest of the image black.
The function image_fill only takes a point, so it does not do what I am looking for.
The function image_modulate with brightness levels 0 or 100 and saturation level 0 would work, but it seems that it applies the filter to the whole image.
It seems modifying the image isn't totally necessary. I could create a separate image that is all black with a white square in the appropriate spot.
See attached image as an example I want to turn one of my regular pictures into.
Note, because of faulty alpha values in my original images, the package "imager" does not work and I must use ImageMagick (R).
.
Sorry I do not know Rmagick, but in ImageMagick command line you can do that as follows. Fill the image with black. Then draw a white square where desired.
Input:
convert lena.png -fill black -colorize 100 -fill white -draw "rectangle 100,100 150,150" -alpha off result.png
Result:
The above draws the white rectangle at offset of 100,100 and width=height=50.
Alternately, you can fill the image with black, then create a new white image that is 50x50 and composite it over the black image at 100,100.
convert lena.png -fill black -colorize 100 -size 50x50 xc:white -gravity northwest -geometry +100+100 -compose over -composite result2.png
Result2:

formula for game xy to image (pixel) xy

It's probably quite simple, but i can't find what i need on search engines... (it's like they used to know better what i was looking for)
I need to convert in-game coordinates to "coordinates" on an image so i can add ... say a pixel on the image to represent the location of the in-game coordinates.
The image is a map, the size is 2384x2044 (width x height).
The in-game 0,0 = the middle of the in-game map, this would also be the middle of the image.
So it's easy to find the xy to print a pixel on the middle of image:
2384 : 2 = 1192 and 2044 : 2 = 1022, so the xy for 0,0 in-game on the image is 1192,1022.
Now, for example, if i move up and slightly to the left in-game the coordinates become: -141.56,1108.11 - How can i calculate the correct xy for the image?
image: http://i.imgur.com/yfiwfO7.png?1
To recap, you want to scale game coordinates of -3000 to +3000 in both axes and offset them to centre them on your image; in that case the computations you want are
pixel_x = 1192 + (game_x * 1192 / 3000.0)
pixel_y = 1022 - (game_y * 1022 / 3000.0)
Note the minus on the y line to invert the direction of the offset. Your game coordinates are floating point so I've made the 3000s floating point by adding a .0 - you didn't say what language you were using so this may or may not be the correct syntax.
However you probably ought to avoid putting constants into this in case you ever want to change the size of the playfield or the image. It would be better to
set up constants in your program for the playfield dimensions
set up constants or global variables for the size of the image: you can read this from the image as you load it
pre-compute the values 1192 / 3000.0 and 1022 / 3000.0 (but using your image constants) to save one floating point operation for each scale? probably not worth it nowadays as a speed optimisation, though, and you might sacrifice a tiny bit of floating point accuracy at the end of the mantissa, but that won't matter here.

Resources