将图像分解为固定大小的平铺

2024-05-16 13:36:45 发布

您现在位置:Python中文网/ 问答频道 /正文

[从超级用户那里移植了这个问题,因为它可能太复杂了]

假设您有一个高分辨率的x1x y1图像(可能是40000x20000),并且您希望在一个嵌入式系统的x2x x2(可能是800x600)屏幕上显示它,这里不需要缩放/放大。您可以将其调整为目标分辨率,但您希望保留微小的细节。所以你想让它通过原始图像的y2n块来显示x2。你知道吗

This solution展示了如何划分成一些任意的n块,但我实际上寻求划分成动态数量的块,其中

n = max(math.ceil(x1/x2), math.ceil(y1/y2))

注意这里,x1/x2不一定等于y1/y2,它们的结果也不一定是整数。因此,在划分为瓷砖之前,我们需要添加一些填充物,比如说,必须在右侧和底部都添加填充物

有人能推荐一些能做到这一点的软件/脚本吗?或许如此,它可以很容易地实现自动化。我相信ImageMagick可能有用,尽管我不太确定。你知道吗

干杯


Tags: 用户图像目标屏幕系统mathx1x2
3条回答

你可以用ImageMagick来做这个。检查他们的-crop选项,该选项允许指定偏移量和大小,然后您可以使用它来裁剪所需的每个瓷砖。你知道吗

例如,使用以下命令:

magick original.png -crop 330x330+400+220 result.png

要打开此图像:

enter image description here

在这个问题上:

enter image description here

GDAL的gdal2tiles可能就是您要寻找的:link。你知道吗

其手册页摘要:

This utility generates a directory with small tiles and metadata, following the OSGeo Tile Map Service Specification. Simple web pages with viewers based on Google Maps, OpenLayers and Leaflet are generated as well - so anybody can comfortably explore your maps on-line and you do not need to install or configure any special software (like MapServer) and the map displays very fast in the web browser. You only need to upload the generated directory onto a web server.

这将是超级容易与Python“枕头”。你知道吗

从它的doc pagecrop()

from PIL import Image

im = Image.open("hopper.jpg")

# The crop method from the Image module takes four coordinates as input.
# The right can also be represented as (left+width)
# and lower can be represented as (upper+height).
(left, upper, right, lower) = (20, 20, 100, 100)

# Here the image "im" is cropped and assigned to new variable im_crop
im_crop = im.crop((left, upper, right, lower))

只需在x和y上分别设置一个循环,然后用dy切出dx块

相关问题 更多 >