[GIMP] [Python-Fu] Divide the image into swords

version

GIMP 2.10.12

Introduction

In rare cases, you want to individually separate sprite animation materials and facial expression difference materials that are often seen in image materials such as Tsukuru. This time, I will introduce how to divide such a set image into a sword with GIMP.

goal.png

The material for this time was borrowed from Pipoya Warehouse.

Before getting into the main subject ...

Depending on the tool you are trying to use the material for, you may want to check once ** whether you need to split the image file itself ** in the first place. For example, Unity originally supports such materials and can be treated as a split image in the image import settings. (This article etc. will be helpful.)

procedure

As a flow

  1. Create a grid-like guide
  2. Generate a slice image according to the guide
  3. Save each image It will be.

[Reference] Create a guide (manual work)

You can make a grid-like guide by repeating the following and adding guides. However, it is troublesome to repeat it manually, so let's do it all at once using Python-Fu as described below **. (Explaned in the next item)

--Select "Image"-> "Guide"-> "New Guide" from the menu.

guide01.png

--Specify the direction and the position of the guide and press "OK".

guide02.png

--You can guide.

guide03.png

Create a grid-like guide (Python-Fu)

GIMP has a feature called Python-Fu that can be manipulated with Python scripts. (Convenient!)

--Select "Filter"-> "Python-Fu"-> "Console" from the menu.

guide04.png

--The Python console screen will open, so continue with the script. For what each line is doing, see the Library Reference Manual along with the sites and books that deal with Python courses. I think it's quick to get it.

guide05.png

Script example to execute


column = 6 #Number of columns you want to isolate
row = 12 #Number of lines you want to separate
image = gimp.image_list()[0]
step = image.width / column
x = step
while x < image.width:
	pdb.gimp_image_add_vguide(image, x)
	x = x + step

step = image.height / row
y = step
while y < image.height:
	pdb.gimp_image_add_hguide(image, y)
	y = y + step

――It's done.

guide06.png

Generate slice images according to the guide

--From the menu, select "Image"-> "Slice Using Guides".

slice01.png

--Divided according to the guide. You can see that each image is displayed above as a tab.

slice02.png

Save each image

You can save one by one by selecting "File"-> "Export" from the menu, but it seems that there is no batch save, so Python-Fu is also useful here.

--Run the following script on the Python console screen. (The original image does not need to be saved, so close it first.)

Script example to execute


import os
path = "F:\\balloon" #Destination
count = 0
for image in gimp.image_list():
	count += 1
	file = os.path.join(path, str(count).zfill(2) + ".png ")
	pdb.gimp_file_save(image, image.active_layer, file, "")

――It will be spit out to the specified folder more and more. I was able to divide it happily.

result.png

in conclusion

GIMP is famous for being free and highly functional, but I'm very happy to be able to write scripts in the popular Python. I wanted to be able to use it more.

reference

Recommended Posts

[GIMP] [Python-Fu] Divide the image into swords
Divide the string into the specified number of characters
[Python] Mask the image into a circle using Pillow
Divide the dataset (ndarray) into arbitrary proportions with NumPy
Paste the image into an excel file using Python's openpyxl
I tried to divide the file into folders with Python