在自定义对焦点附近裁剪图像并调整其大小 [英] Crop and resize image around a custom focus point

查看:79
本文介绍了在自定义对焦点附近裁剪图像并调整其大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要完成的工作的概述:我的照片需要裁剪和调整大小,一次用于缩略图视图(大约295x195),一次用于标题图像"样式的视图(大约1400x560).原始照片的尺寸各不相同,最大为2440x1600.这不是要解决的问题.

An overview of what I want to accomplish: I have photos that need to be cropped and resized, once for a thumbnail view (around 295x195), and once for a "header-image" style view (around 1400x560). The original photos vary in size, the biggest are around 2440x1600. This would not be a problem to solve.

但是,图像不应使用北"或中心"引力进行裁剪,而应具有从此处开始的定义中心位置 图像应被裁剪.为此,图像具有xy值,以百分比表示中心点; x=50,y=50表示 图像的焦点实际上在中间.

However, the image should not be cropped with a "North" or "Center" gravity but should have a defined center position from where the image should be cropped. For this, an image has x and y values that represent the center point in percentage; x=50,y=50 would mean that the focus point of the image is actually in the middle.

我在这里和imagemagick论坛上进行了搜索,但是找不到任何合适的内容,除了剪切图像的特定部分然后调整大小". 我不确定这是否可以在一个imagemagick命令中完成,或者我是否需要自己计算所有内容并基本上创建一个空画布,将其放在图像上,移动图像并调整其大小,然后将其他所有图像剃光实现这一目标.

I searched here and on the imagemagick forums but could not find anything suitable, except for "cutting off a certain part of an image and then resize it". I'm not sure if this can be done in one imagemagick command or if I need to calculate everything on my own and basically create an empty canvas, put the image on it, move and resize the image, and then shave off everything else to achieve this.

我需要一个imagemagick命令来

I need one imagemagick command to

  • 拍摄一张图像作为输入
  • 根据我拥有的焦点(以百分比或像素为单位)将其调整大小并裁剪为给定尺寸(例如295x195)
  • 以给定尺寸输出图像,焦点在中间

我将针对每个图像和所需的大小使用一个命令.

I will be using one command per image and per size needed.

这是我在草图中制作的简短可视化效果,并带有演示图片,希望可以使其更易于理解:

Here's a short visualization I made in sketch with a demo picture in hope to make it more understandable what I am trying to do:

左图是我将焦点放在282,282上的基本图像.右边的是我想要的295x195px尺寸的所需裁切(没有线条,它们只是用于问题演示).如您所见,裁剪的中心点就是原始图像上的焦点.

The image on the left is the base image on which I put on a focus point at 282,282. The one on the right is the desired crop that I want in 295x195px dimensions (without the lines on it, they're just for the problem demonstration). As you can see, the crop has its center point where the focus point on the original image is.

我的第一个尝试是使用-crop的偏移值来设置图像的焦点:

My first attempt was to play around with the offset values for -crop to set the focus point of the image:

convert photo.jpg -resize 1440x560^ -crop "1440x560+25%+25%" result.jpg

但是我很快了解到,这不是它的工作方式,因为它会导致图像太小(例如,坐标位于图像的左下角),并且通常会被截断 在焦点上可见的图像部分,在阅读文档中的内容后才有意义.

But I learned quickly that this is not the way it works as it results in images being too small (when the coordinates are e. g. in the bottom left of an image) and in general cuts off parts of the image that would be visible with the focus point, which makes sense after reading about it in the docs.

然后我看到了-region选项,并尝试使用它的图像尺寸为2400x1600并从我的图像中切出了各种切口:

Then I saw the -region option and tried to play around with it having an image size of 2400x1600 and getting various cuts out of my image:

convert photo.jpg -region 610x400 -resize 1440x560^ -crop 1440x560+0+0 result.jpg

region选项的唯一作用是无论我输入什么值,我都会得到相同的图像剪切,并且始终在图像上放置扭曲的白色水平条纹.

The only effect of the region option is that I get the same cut of the image regardless of what values I put into, and it always puts distorted white horizontal stripes on the image.

任何朝着正确方向(带有或不带有代码示例)的提示都将有所帮助,所以我知道在哪里进行更深入的研究.

Any hints towards the right direction (with or without code examples) would be helpful so I know where to dig deeper.

  • 用于开发的macOS Catalina
  • 包含代码的应用将在debian buster上运行
  • 本地imagemagick版本为7.0.8-66
  • server imagemagick的版本是6.9.10-23(我可以通过手动构建来更新它,不过,有一种仅在IM7中可用的解决方案是可以的)

推荐答案

ImageMagick中的"-distort SRT"运算符可以缩放图像,调整画布大小,以及将一点从一组坐标移动到另一组坐标中的所有步骤

The "-distort SRT" operator in ImageMagick can scale an image, resize the canvas, and move a point from one set of coordinates to another all in one move.

此命令将读取您的输入图像,并将输出视口设置为295x195.然后,它使用扭曲SRT"将输入图像缩放到50%,将+ 282 + 282的起始中心点移至新的+ 147.5 + 97.5的输出中心点,并将视口大小更改为295x195.

This command would read your input image and set an output viewport of 295x195. Then it uses "-distort SRT" to scale the input image to 50%, move that starting center point of +282+282 to the new output center point of +147.5+97.5, and change the viewport size to 295x195.

convert input.png -set option:distort:viewport 295x195 \
   -distort SRT "282,282 0.5 0 147.5,97.5" result.png

distort参数以要成为操作中心的点"+ 282 + 282"开头.然后比例尺"0.5"将尺寸减小到50%. "0"参数用于旋转多少度,但是您不旋转图像,因此"0".最后一个参数是着陆点"+ 147.5 + 97.5",因为它是起点,因此它成为295x195输出画布的新中心.

The distort arguments start with the point you want to be the center of the operation, "+282+282". Then the scale "0.5" reduces the size to 50%. The "0" argument is for how many degrees of rotation, but you're not rotating the image, so "0". And the last argument is the landing spot, "+147.5+97.5", for what was the starting point, so it becomes the new center of your 295x195 output canvas.

当然,如果您使用各种大小的输入,则必须手动插入和调整参数.

Of course you'll have to manually insert and adjust your arguments if you're using various sized inputs.

已编辑以添加...

ImageMagick可以使用FX表达式计算缩放图像的数量.这是同一命令,其中FX表达式将X偏移量或输出视口的中心除以从输入图像使用的X偏移量.

ImageMagick can calculate the amount to scale the image using an FX expression. Here is the same command where the FX expression divides the X offset, or the center of the output viewport, by the X offset used from the input image.

convert input.png -set option:distort:viewport 295x195 \
   -distort SRT "282,282 %[fx:147.5/282] 0 147.5,97.5" result.png

将原始282 X坐标点设置为147.5,并将图像缩放到该比例.

That puts the original 282 X coordinate point at 147.5, and scales the image to that ratio.

这篇关于在自定义对焦点附近裁剪图像并调整其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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