Rewrite image generation in python

This commit is contained in:
Matúš Púll 2025-08-04 20:14:02 +02:00
parent 8755949180
commit 8a7687d3e4
2 changed files with 45 additions and 91 deletions

View file

@ -1,91 +0,0 @@
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <nlohmann/json.hpp>
#include <iostream>
#include <fstream>
#include <string>
using namespace cv;
using nlohmann::json;
using std::string;
#define H 800
#define W 300
#define M 30
#define R 60
Mat source_image = Mat::zeros(Size(H,W),CV_8UC1);
void render(int i, int j, int y_s, int x_s, Mat *img) {
if(i >= y_s && i <= y_s+R-1 && j >= x_s && j <= x_s+R-1) {
source_image.at<uchar>(i,j)=img->at<uchar>(i-y_s,j-x_s);
}
}
int main(int argc, char** argv) {
// FACTION
int fac;
if(argc <= 1) {
std::cout << "0:U,1:SK,2:M,3:N,4:S";
std::cin >> fac;
}
else fac = std::atoi(argv[1]);
std::ifstream ff("cards/factions.json");
json factions = json::parse(ff);
string faction = factions[fac];
// NAME
string name;
if(argc <= 2) std::cin >> name;
else name = argv[2];
std::ifstream f("cards/"+faction+"/"+name+".json");
json data = json::parse(f);
// LOAD IMAGES
Mat i_look = imread("cards/"+faction+"/images/"+name+".png",IMREAD_GRAYSCALE);
Mat i_faction = imread("cards/images/factions/"+faction+".png",IMREAD_GRAYSCALE);
Mat i_line = imread("cards/images/lines/"+string(data["line"])+".png",IMREAD_GRAYSCALE);
// RENDER IMAGES
std::vector<Mat> i_traits;
for(auto trait : data["traits"]) {
i_traits.push_back(imread("cards/images/traits/"+string(trait)+".png",IMREAD_GRAYSCALE));
}
for(int i=0;i<source_image.rows;i++)
for(int j=0;j<source_image.cols;j++)
source_image.at<uchar>(i,j)=i_look.at<uchar>(i,j);
// RENDER POWER AND TRAITS
string bruh = std::to_string((int)data["power"]);
for(int i=0;i<source_image.rows;i++) {
for(int j=0;j<source_image.cols;j++) {
// Vlevo nahoře sílu
putText(
source_image,
bruh,
Point(M,M),
FONT_HERSHEY_COMPLEX_SMALL,
1,
Scalar(0,0,0),
2
);
render(i,j, M,W/2-R/2, &i_faction);
render(i,j, M,W-M-R, &i_line);
// Traits
for(int k = 0; k < i_traits.size(); k++)
render(i,j, H-M-R,W-M-R*(k+1), &i_traits[k]);
}
}
imshow("Result",source_image);
waitKey(0);
return 0;
}

45
image.py Executable file
View file

@ -0,0 +1,45 @@
#!/usr/bin/python3
from PIL import Image, ImageDraw, ImageFont
import json
H = 800
W = 300
M = 30
R = 60
font_path = "" # TODO gwent font
small_size = 10
big_size = 100
small_font = ImageFont.truetype(font_path, small_size)
big_font = ImageFont.truetype(font_path, big_size)
with open("cards/config.json") as f:
j = json.loads(f.read())
traits = { trait: Image.open("cards/images/traits/"+trait+".png") for trait in j["traits"] }
lines = { line: Image.open("cards/images/lines/"+line+".png") for line in j["lines"] }
factions = { faction: Image.open("cards/images/factions/"+faction+".png") for faction in j["factions"] }
def make_image(data):
image = Image.new("RGB", (W, H), (255, 255, 255))
draw = ImageDraw.Draw(image)
draw.text((M, M), str(data["power"]), font=big_font)
image.paste(factions[data["faction"]], ((W-R)//2, M), mask=factions[data["faction"]])
image.paste(lines[data["line"]], (W-M-R, M), mask=lines[data["line"]])
draw.text(((W-len(data["name"]*small_size))/2, (H-small_size)/2), str(data["name"]), font=small_font)
for i in range(len(data["traits"])):
trait = data["traits"][i]
image.paste(traits[trait], (H-M-R,W-M-R*(i+1)), mask=lines[trait])
image.save(data["name"]+".png", "PNG")
with open("cards/cards.json") as f:
cards = json.loads(f.read())
print(cards)
for card in cards:
make_image(card)