首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在画布上自动调整大小和排列图片

问在画布上自动调整大小和排列图片
EN

Ask Ubuntu用户
提问于 2014-11-11 15:48:19
回答 1查看 1.8K关注 0票数 3

我想做一个模板,以打印9张卡的63×88毫米的DIN A4纸。

我想加载图像,以便它们自己调整大小,我可以直接打印它们。

我怎么能这么做?

EN

回答 1

Ask Ubuntu用户

回答已采纳

发布于 2014-11-15 07:34:05

下面的脚本可以根据多个图像创建一个文件。脚本使用imagemagick。

关于imagemagick

来自man imagemagick:

ImageMagick是一个用于创建、编辑和合成位图图像的软件套件。它可以读取、转换和写入各种格式的图像(大约100),包括GIF、JPEG、JPEG-2000、PNG、PDF、PhotoCD、TIFF和DPX。使用ImageMagick转换、翻转、镜像、旋转、缩放、剪切和转换图像,调整图像颜色,应用各种特效,或绘制文本、线条、多边形、椭圆和贝塞尔曲线。

因为Imagemagick是一个命令行工具,所以它特别适合在脚本中使用;例如,要自动化您在问题中提到的作业。

安装Imagemagick

您可能必须先安装imagemagick:

代码语言:javascript
复制
sudo apt-get install imagemagick

脚本

该脚本将多个图像组合并排列到一个文件中。这样做,它使用的最大空间(在px中),由您定义,但不改变图像比例。工作完成后,输出将在Gimp中自动打开(因此应该安装Gimp )。如果由于某种原因,该文件没有显示在Gimp中,则它位于图像的文件夹中,名为:out_file.jpg。

示例

在下面的示例中,您想要的输出是一个由三个图像(列)x三个图像(行)组成的表,而图像需要为1500 In (水平的)。您将需要一个4500 in (宽度)的区域,同时确保这三行适合该区域。在脚本的head部分,您设置:

代码语言:javascript
复制
colrows = [3, 3]            # set the number of colums/rows
max_area = [4500, 6500]     # set the area (hxv in px) the script can use

由于我们的图片高度“过大”,脚本将填充最大宽度(4500/3 =1500 per每幅图像)

产出:

脚本假定:

  • 所有的图片都是相同的比例和方向(但可能是不同的大小)
  • 文件夹中的图片数量(见上文)等于(或至少)行x列(在您的示例中为9行)。如果文件夹中有更多的图像,则只使用所需的编号。

选项/设置

脚本有许多选项。在脚本的head部分,您需要定义:

代码语言:javascript
复制
img_dir = "/path/to/files"      # path to the images
output_extension = ".jpg"       # desired output
colrows = [3, 3]                # columns  / rows of pictures
max_area = [4500, 6500]         # the (max) size of your sheet
border = 10                     # whitespace around the images

脚本

代码语言:javascript
复制
#!/usr/bin/env python3

import os
import subprocess
import shutil

# set the input below
###############################################################################
img_dir = "/path/to/files"      # path to the images
output_extension = ".jpg"       # desired output
colrows = [3, 3]                # columns  / rows of pictures
max_area = [4500, 6500]         # the (max) size of your sheet
border = 10                     # whitespace around the images 
###############################################################################
cd_topics = "cd "+"'"+img_dir+"'; "
# current picture size
images = sorted([item for item in os.listdir(img_dir)])[:colrows[0]*colrows[1]]
for image in images:
    shutil.copyfile(img_dir+"/"+image, img_dir+"/fixed_"+image.replace(" ", "_"))
images = sorted([item for item in os.listdir(img_dir) if item.startswith("fixed_")]) ##             
# a few functions                    
def get_picsize(image):
    command = cd_topics+'identify -format "%wx%h" '+image
    return subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8").strip().split("x")

def resize(img_in, img_out, size, modus=None):
    modus = "^ " if modus == "outsize" else " "    
    newsize = str(size)+"x"+str(size)
    resize_command = cd_topics+"convert "+img_in+" -resize "+newsize+modus+img_out
    subprocess.call(["/bin/bash", "-c", resize_command])

def add_border(img_in, img_out, bd):
    if bd != None and str(bd) != "0":
        addborder_command = cd_topics+"convert "+img_in+" -bordercolor white -border "+bd+"x"+bd+" "+img_out
        subprocess.call(["/bin/bash", "-c", addborder_command])
# make all pictures of equal size
standard_size = get_picsize(images[0])
for pic in images:
    if not get_picsize(pic) == standard_size:
        resize(pic, pic, max(standard_size))   
# calculate resize
picture_orientation = int(standard_size[0])/int(standard_size[1])
area_ratio = float(max_area[0]/max_area[1])
combi_ratio = (colrows[0]*int(standard_size[0]))/(colrows[1]*int(standard_size[1]))
fit_in = area_ratio/combi_ratio
if fit_in > 1:
    newsize = int(max_area[1]/colrows[1])-2*border
    modus = "outsize" if picture_orientation > 1 else None
else:
    newsize = int(max_area[0]/colrows[0])-2*border
    modus = "outsize" if picture_orientation < 1 else None
# resize images
for image in images:
    resize(image, "rs_"+image, newsize, modus)
    add_border("rs_"+image, "rs_"+image, bd=str(border))
# combining into rows
images = sorted([pic for pic in os.listdir(img_dir) if pic.startswith("rs_")])
rows = [images[i:i+colrows[0]] for i in range(len(images)-colrows[0]+1) if i % colrows[0]== 0]
i = 0
while i in range(len(rows)):
    row = rows[i]; images = [im for im in row]
    command = cd_topics+"convert "+" ".join(images)+" +append "+"row_"+str(i+1)+output_extension
    subprocess.call(["/bin/bash", "-c", command])
    i = i+1
# combining rows
images = sorted([pic for pic in os.listdir(img_dir) if pic.startswith("row")])
output_file = "out_file"+output_extension
command = cd_topics+"convert "+(" ").join(images)+" -append "+output_file
subprocess.call(["/bin/bash", "-c", command])
# opening image with Gimp, cleaning up
for image in sum([[img for img in os.listdir(img_dir) if img.startswith(string)] \
              for string in ["fixed_", "row_", "rs_"]], []):
    os.remove(img_dir+"/"+image)
command = cd_topics+"gimp "+output_file
subprocess.Popen(["/bin/bash", "-c", command])

如何使用

  • 将脚本复制到一个空文件中,保存为combine.py。在脚本的头部分设置所需的信息(请参阅上面的Options / settings )。
  • 将图片的数量复制到文件夹中
  • 通过命令运行脚本: python3 /path/to/comine.py

几个音符

  • 脚本创建一个新映像(outfile)。如果您对相同的图片(-directory)进行了另一个组合,则首先删除它,否则它可能会出现在输出文件中。
  • 脚本按名称对图像进行排序,因此,如果需要特定的顺序,只需将它们命名为a.jpg、b.jpg等。
票数 3
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/548355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档