在R中裁剪图像 [英] Crop image in R

查看:173
本文介绍了在R中裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R中的magick包来裁剪图像.我的目标是裁剪图像的左上角部分.到目前为止,我的代码似乎适用于某些图像,但是相同尺寸的图像不适用于其他图像.这是我的代码:

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")

我正在对所有尺寸为宽度320高度240的图像执行此操作.此代码似乎适用于某些图像,但裁剪某些图像的左上角以外的部分.有没有办法修改我的代码,使得无论图片尺寸如何,它始终会裁剪左上角.

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.

推荐答案

我不知道R,但是如果我使用等效的imagemagick命令,则您的印章将被裁剪,图像的下半部分将被裁剪.切碎的底部的左上角.你为什么要用排骨?同样,在裁剪之后,如果将图像保存到PNG或TIFF,则图像上还会保留虚拟画布,应使用+ repage删除该画布.不确定R中的内容,但可能是+ 0 + 0.如果要裁剪左上角,只需使用-gravity northwest并以所需的尺寸WxH + 0 + 0进行裁剪,除非您希望将偏移量替换为+ 0 + 0

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

位置:

convert Chrometab.jpg -gravity northwest -crop 50x55+1+0 +repage tmp3.png

tmp3.png

这篇关于在R中裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆