如何根据另一个参考点调整图像大小 [英] How to resize an image according to a reference point in another

查看:57
本文介绍了如何根据另一个参考点调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有两个像素尺寸为 (1725, 1580) 的图像(im1 和 im2).然而,im2 周围有一个大边框,我必须创建它以确保图像大小相同.im2 最初是 (1152, 864).

I currently have two images (im1 and im2) with pixel dimensions of (1725, 1580). im2 however possesses a large border around it that i had to create to ensure that the images were of the same size. im2 was originally (1152, 864).

如此.当我使用 PIL.Image.blend 将 im2 覆盖在 im1 之上时,im2 似乎覆盖在 im1 上,但要小得多.我在图像上有 2 个不同的参考点,我认为我可以使用(出现在 im1 和 im2 上)来重新缩放 im2(以某种方式放大它?)以将 im2 覆盖在 im1 上.

As such. when i overlay im2 ontop of im1 using PIL.Image.blend, im2 appears overlayed onto im1, but is a lot smaller. I have 2 distinct reference points on the images i think i could use (present on im1 and im2) to rescale im2 (zoom it in somehow?) to overlay im2 ontop of im1.

我的问题是我一直在查看各种 Python 模块(PIL、scipy、matplotlib 等),但似乎无法真正找到任何地方或找到可以解决此问题的解决方案.

My issue is that i have been looking through various python modules (PIL, scipy, matplotlib etc) but cant seem to really be getting anywhere or find a solution with which i could approach this issue.

我认为我可以使用 2 个参考点(出现在 im1 和 im2 上)来重新缩放 im2(以某种方式放大它?)以将 im2 覆盖在 im1 之上.

I have 2 reference points i think i could use (present on im1 and im2) to rescale im2 (zoom it in somehow?) to overlay im2 ontop of im1.

我查看了各种模块,但似乎找不到任何可能有效的模块(scipy、PIL、matplotlib)

i have looked at various modules but cant to seem to find anything that might work (scipy, PIL, matplotlib)

#im1 https://i.imgur.com/dF8uyPw.jpg
#im2 https://i.imgur.com/o4RAhOQ.png
#im2_resized https://i.imgur.com/jfWz1LE.png

im1 = Image.open("pit5Film/Pit_5_5mm_inf.tif")
im2 = Image.open("pit5Overlay/overlay_132.png")

old_size = im2.size
new_size = im1.size
im2_resized = Image.new("RGB", new_size) 
im2_resized.paste(im2,((round((new_size[0]-old_size[0])/2)),round(((new_size[1]-old_size[1])/2))))

Image.blend(im1,im2_resized,0.2)

推荐答案

这是在基于 Imagemagick 的 Python Wand 中的方法.我使用 Mark Setchell 的图像和 Python Wand 等效命令.根据文档,扭曲命令需要 Imagemagick 7.使用 Python Wand 0.5.5,当前版本.

This is how to do it in Python Wand, which is based upon Imagemagick. I use Mark Setchell's images and the Python Wand equivalent command. The distort command needs Imagemagick 7, according to the documentation. Using Python Wand 0.5.5, the current version.

脚本:

#!/bin/python3.7

from wand.image import Image
from wand.color import Color
from wand.display import display

with Image(filename='imageA.jpg') as Aimg:
    with Image(filename='imageB.jpg') as Bimg:
        Aimg.virtual_pixel = 'background'
        Aimg.background_color = Color('white')
        arguments = (422, 775, 514, 426, 1246, 799, 668, 426)
        Aimg.distort('affine', arguments)
        Aimg.composite(Bimg, 0, 0, 'overlay')
        Aimg.save(filename='image_BoverlayA_composite.png')
        display(Aimg)


调用命令:

python3.7 wand_affine_overlay.py


结果:

补充:

如果要将图像修剪到其最小边界框,请按如下方式在命令中添加修剪,其中修剪值在 0 到量子范围内.

If you want to trim the image to its minimum bounding box, then add trim to the command as follows, where the trim value is in the range 0 to quantum range.

#!/bin/python3.7

from wand.image import Image
from wand.color import Color
from wand.display import display

with Image(filename='imageA.jpg') as Aimg:
    with Image(filename='imageB.jpg') as Bimg:
        Aimg.virtual_pixel = 'background'
        Aimg.background_color = Color('white')
        arguments = (422, 775, 514, 426, 1246, 799, 668, 426)
        Aimg.distort('affine', arguments)
        Aimg.composite(Bimg, 0, 0, 'overlay')
        Aimg.trim(fuzz=10000)
        Aimg.save(filename='image_BoverlayA_composite.png')
        display(Aimg)


这篇关于如何根据另一个参考点调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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