将numpy数组保存到图像会将图像部分缩小到错误的大小

2024-04-29 18:39:03 发布

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

在了解如何在blender脚本中导出外部图像时,我遇到了这个问题。但我想这和blender不再直接相关了,更多的是关于numpy和如何处理数组Here is post about first problem.

所以问题是,当保存numpy数组到图像时,它会失真,并且有多个相同的图像。请看下图,以便更好地理解

我们的目标是试图找出如何使用numpy和python使用blender自己的像素数据来实现这一点。因此,避免使用blender python中不包含的库,如PIL或cv2

enter image description here

当保存数据时,所有最终大小的图像都正确工作。当尝试将4个较小的片段合并到最终的较大图像时,它无法正确导出

我在blender中使用python编写了示例脚本来演示该问题:

# Example script to show how to merge external images in Blender
# using numpy. In this example we use 4 images (2x2) that should
# be merged to one actual final image. 
# Regular (not cropped render borders) seems to work fine but
# how to merge cropped images properly???
#
# Usage: Just run script and it will export image named "MERGED_IMAGE"
# to root of this project folder and you'll see what's the problem.

import bpy, os
import numpy as np

ctx = bpy.context
scn = ctx.scene

print('START')

# Get all image files
def get_files_in_folder(path):
    path = bpy.path.abspath(path)
    render_files = []
    for root, dirs, files in os.walk(path):
        for file in files:
            if (file.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif'))):
                render_files.append(file)
    return render_files

def merge_images(image_files, image_cropped = True):

    image_pixels = []
    final_image_pixels = 0

    print(image_files)

    for file in image_files:
        if image_cropped is True:
            filepath = bpy.path.abspath('//Cropped\\' + file)
        else:
            filepath = bpy.path.abspath('//Regular\\' + file)
        loaded_pixels = bpy.data.images.load(filepath, check_existing=True).pixels
        image_pixels.append(loaded_pixels)

    np_array = np.array(image_pixels)

    # Merge images
    if image_cropped:
        final_image_pixels = np_array
        # HOW MERGE PROPERLY WHEN USING CROPPED IMAGES???
    else:
        for arr in np_array:
            final_image_pixels += arr

    # Save output image
    output_image = bpy.data.images.new('MERGED_IMAGE', alpha=True, width=256, height=256)
    output_image.file_format = 'PNG'
    output_image.alpha_mode = 'STRAIGHT'
    output_image.pixels = final_image_pixels.ravel()
    output_image.filepath_raw = bpy.path.abspath("//MERGED_IMAGE.png")
    output_image.save()   

images_cropped = get_files_in_folder("//Cropped")
images_regular = get_files_in_folder('//Regular')

# Change between these to get different example
merge_images(images_cropped)
#merge_images(images_regular, False)

print('END')

因此,我想问题与如何使用numpy处理图像像素数据和阵列有关

下面是zip文件中的项目文件夹,其中包含工作测试脚本示例,您可以在其中测试blender中的工作方式https://drive.google.com/file/d/1R4G_fubEzFWbHZMLtAAES-QsRhKyLKWb/view?usp=sharing


Tags: topathin图像imagenumpyoutputfiles
1条回答
网友
1楼 · 发布于 2024-04-29 18:39:03

由于您的所有图像都是128x128的相同维度,并且由于OpenCV图像是Numpy数组,因此有三种方法。您可以使用^{}保存图像

输入图像:

enter image description hereenter image description hereenter image description hereenter image description here

方法#1:^{}+^{}

hstack1 = np.hstack((image1, image2))
hstack2 = np.hstack((image3, image4))
hstack_result = np.vstack((hstack1, hstack2))

方法#2:^{}

concatenate1 = np.concatenate((image1, image2), axis=1)
concatenate2 = np.concatenate((image3, image4), axis=1)
concatenate_result = np.concatenate((concatenate1, concatenate2), axis=0) 

方法#3:^{}+^{}

hconcat1 = cv2.hconcat([image1, image2])
hconcat2 = cv2.hconcat([image3, image4])
hconcat_result = cv2.vconcat([hconcat1, hconcat2])

所有方法的结果应相同

enter image description here

完整代码

import cv2
import numpy as np

# Load images
image1 = cv2.imread('Fart_1_2.png')
image2 = cv2.imread('Fart_2_2.png')
image3 = cv2.imread('Fart_1_1.png')
image4 = cv2.imread('Fart_2_1.png')

# Method #1
hstack1 = np.hstack((image1, image2))
hstack2 = np.hstack((image3, image4))
hstack_result = np.vstack((hstack1, hstack2))

# Method #2
concatenate1 = np.concatenate((image1, image2), axis=1)
concatenate2 = np.concatenate((image3, image4), axis=1)
concatenate_result = np.concatenate((concatenate1, concatenate2), axis=0) 

# Method #3
hconcat1 = cv2.hconcat([image1, image2])
hconcat2 = cv2.hconcat([image3, image4])
hconcat_result = cv2.vconcat([hconcat1, hconcat2])

# Display
cv2.imshow('concatenate_result', concatenate_result)
cv2.imshow('hstack_result', hstack_result)
cv2.imshow('hconcat_result', hconcat_result)
cv2.waitKey()

相关问题 更多 >