Reputation: 2332
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.
Upvotes: 1
Views: 5838
Reputation: 53081
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
Upvotes: 1