light-and-ray / resynthesizer-python-lib

Python library which wraps 61315's pure C implementation of resynthesizer (open-source Content-Aware Fill equivalent)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Resynthesizer python lib

https://github.com/61315/resynthesizer wrapped into python library

Resynthesizer is a very old (2000 year) open-source equivalent to Adobe Photoshop's "Content-Aware Fill" feature

Installation:

pip install resynthesizer

Usage:

from resynthesizer import resynthesize
from PIL import Image

source = Image.open('source.jpg')
# mask must have black background, white foreground
mask = Image.open('mask.png')
result = resynthesize(source, mask)
result.save('result.jpg')

You can adjust resynthesizer's params if you want:

from resynthesizer import TImageSynthParameters

# defaults
params = TImageSynthParameters()
params.isMakeSeamlesslyTileableHorizontally = 1
params.isMakeSeamlesslyTileableVertically = 1
params.matchContextType = 1
params.mapWeight = 0.5
params.sensitivityToOutliers = 0.117
params.patchSize = 30
params.maxProbeCount = 200

...

result = resynthesize(source, mask, parameters=params)

try

Install Gradio 3 demo requirements and run:

python -m venv venv
. venv/bin/activate
pip install gradio==3.41.2 pillow
python ./demo.py

Windows:

python -m venv venv
venv\Script\activate
pip install gradio==3.41.2 pillow
pip install msvc-runtime setuptools
# you also need to have installed https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170#latest-microsoft-visual-c-redistributable-version
python ./demo.py

resynthesizer.so build

  1. git clone https://github.com/61315/resynthesizer

  2. install gcc, make

  3. Modify Makefile (added -fPIC, use gcc instead of clag, remove all examples):

Makefile
.POSIX:
CC        = gcc -std=c99
CPPFLAGS  = -MMD -MP -DSYNTH_LIB_ALONE -fPIC
CFLAGS    = -Wall -Wextra -pedantic -O3
LDFLAGS   = -lm
LDLIBS    =
# PREFIX = /usr/local

LIB_DIR := lib
BUILD_DIR := build
SRC_DIR := resynthesizer

# Collect resynthesizer sources and headers, then create object files out of the sources.
SRCS := $(shell find $(SRC_DIR) -name '*.c')
OBJS := $(SRCS:%.c=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)

INC_DIRS := $(shell find $(SRC_DIR) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))

STATIC_LIB := $(LIB_DIR)/libresynthesizer.a

ASSET_DIR := assets
EXAMPLE_DIR := examples
EXAMPLES := $(EXAMPLE_DIR)/hello $(EXAMPLE_DIR)/ppm $(EXAMPLE_DIR)/painter

# -g -Wall -Wextra -Werror -std=c99 -pedantic-errors
# TODO: Try both -Werror and -pedantic-errors after all the chores are done.

all: $(STATIC_LIB) test
	@echo "\033[1;92mDone!\033[0m"

# Build resynthesizer as static library.
$(STATIC_LIB): $(OBJS)
	@echo "\033[1;92mBuilding $@\033[0m"
	mkdir -p $(dir $@)
	ar rvs $@ $^

$(BUILD_DIR)/%.o: %.c
	@echo "\033[1;92mBuilding $@\033[0m"
	mkdir -p $(dir $@)
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

.PHONY: clean test all

clean:
	$(RM) -r $(BUILD_DIR) $(LIB_DIR) $(EXAMPLES)


-include $(DEPS)
  1. Link:
gcc -shared -o lib.so build/resynthesizer/*.o
  1. For .dll I've used x86_64-w64-mingw32-gcc from sudo apt-get install gcc-mingw-w64 instead of gcc. It shows strange worse result on my virtual machine, I don't know why. They are the same if I build in VM using clang, or if I build it on Linux

  2. For Max OS .dylib was built by the author: 61315/resynthesizer#6 (comment)

About

Python library which wraps 61315's pure C implementation of resynthesizer (open-source Content-Aware Fill equivalent)


Languages

Language:Python 100.0%