libvips / lua-vips

Lua binding for the libvips image processing library
MIT License
125 stars 10 forks source link

Interpolation and Rotate #57

Closed superjaja05 closed 5 months ago

superjaja05 commented 8 months ago

Greetings! I'm trying to rotate an image 45° without any interpolation (Well, GIMP calls it "None" so i suppose its without interpolation) But i can't figure out how to change it, anything i type returns unsupported gtype for set VipsInterpolate

This is my code:

local vips = require("lua-vips")

local base = vips.Image.new_from_file("thumbnail_base.png")
print("Enter Path of Sprite:")
local sprite = io.read()

local sprite_img = vips.Image.new_from_file(sprite)

local sprite_height = sprite_img:height()
local sprite_width = sprite_img:width()

local biggest_size = ((sprite_height>sprite_width) and sprite_height) or ((sprite_width>sprite_height) and sprite_width) --Gives me the biggest size (if image is 20x10, gives me 20)
if sprite_height == sprite_width then --If both sizes are the same (like 10x10) it just gives me 10
    biggest_size = sprite_height
end
print(biggest_size)

sprite_img = sprite_img:gravity("centre", biggest_size*1, biggest_size*1) --Adds empty space around the sprite
sprite_img:pngsave("sprite_step1_cropped.png")

sprite_img = sprite_img:resize(2, {kernel="VIPS_KERNEL_NEAREST"}) --Scales the sprite up A LOT
sprite_img:pngsave("sprite_step2_resized.png")

if (sprite_img:height() % 2) == 0 then
    sprite_img = sprite_img:gravity("centre", sprite_img:height()+1, sprite_img:height()+1) --If the sprite is not odd, adds 1 to the height and width
    sprite_img:pngsave("sprite_step3_resized2.png")
end

sprite_img = sprite_img:rotate(45, {interpolate="No idea what to put here"}) --Rotates the sprite 45° <<<<<< THIS IS WHERE I'M CONFUSED
sprite_img:pngsave("sprite_step4_rotated.png")

local sprite_width2 = sprite_img:width() --Gets the sprite's width again

sprite_img = sprite_img:resize(256/sprite_width2) --Returns the sprite to 256x256 resolution
sprite_img:pngsave("sprite_step5_resized3.png")

base = base:add(sprite_img) --Adds the sprite on top of the thumbnail_base

base:pngsave("x.png") --Exports the final result

Any idea how i can change/disable interpolation? Or if its even possible?

jcupitt commented 8 months ago

Hi @superjaja05,

affine uses a different system -- you create an interpolation object (you need nearest) and pass a reference to that.

Unfortunately I never got around to binding this bit of libvips, so it'd need adding. It's not much code, the python equivalent is here:

https://github.com/libvips/pyvips/blob/master/pyvips/vinterpolate.py

it'd just need translating to lua.

jcupitt commented 8 months ago

I had a very quick go: https://github.com/libvips/lua-vips/commit/5afc0046499f8117180a50c80af27d3ebe202288

But I've not tested it, and I've not touched Lua for a few years, so it's probably all wrong :( I have a deadline this week, but I might try fixing it up and merging it some time early next year.

jcupitt commented 8 months ago

... so you'd use it like this (if it works):

interpolator = vips.Interpolator.new_from_name("nearest")
sprite_img = sprite_img:rotate(45, {interpolate=interpolator})

You can test the underlying libvips operation from the command-line with eg.:

$ vips rotate k2.jpg x.jpg 45 --interpolate nearest
rolandlo commented 5 months ago

I had a very quick go: 5afc004

But I've not tested it, and I've not touched Lua for a few years, so it's probably all wrong :( I have a deadline this week, but I might try fixing it up and merging it some time early next year.

@jcupitt I hope you don't mind if I finish your work in the linked commit. It all works, except of this issue, which I discovered while working on it and which also appears with pyvips.

jcupitt commented 5 months ago

Sure, go ahead! And thank you.