I want to import an image into blender and quote some parts according to row and cols as icon displayed in template_icon_view(). BUT template_icon_view() could only import icon from file path. Is there any alternative way? When it is done, it should be like fig3.Thanks! enter image description here

  • 2
    $\begingroup$ Not sure what you mean by "quote" in this context. $\endgroup$ Apr 19 '20 at 12:37
  • 1
    $\begingroup$ If I get some pixels using bpy.data.images['1.png'].pixels[], could I generate an icon with these pixels? $\endgroup$
    – ttrtty5
    Apr 19 '20 at 13:39

Although I have solved this problem for a long time, I see better answers in other places, so I sent them out
The first is the imbuf library, but the documents are not very detailed and haven't been updated for a long time
And then this
How to merge external images together in script?
Then my method is that the PIL library cuts the pictures, saves them to Blender's cache directory, and loads them through this path

from . PIL_library64 import Image
import sys

def cut_image(image,x,y):
    # x指横上切的刀数
    width, height = image.size
    item_width = int(width / x)
    item_height = int(width / y)
    box_list = []
    # (left, upper, right, lower)
    for i in range(0,y):
        for j in range(0,x):
            box = (j*item_width,i*item_height,(j+1)*item_width,(i+1)*item_height)
    image_list = [image.crop(box) for box in box_list]
    return image_list

# 保存
def save_images(image_list):
    from os.path import exists
    from os import makedirs
    import bpy
    if not exists(temppath+"mi2bl"): # 判断文件夹存在
        makedirs(temppath+"mi2bl") # 创建文件夹
    index = 1
    for image in image_list:
        image.save(temppath+"mi2bl\\"+str(index) + '.png', 'PNG')
        file_list.append(temppath+"mi2bl\\"+str(index) + '.png')
        index += 1
    return file_list

def image_crop(file_path, x, y):
    image = Image.open(file_path)
    image_list = cut_image(image,x,y)
    return file_list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.