lua-vips icon indicating copy to clipboard operation
lua-vips copied to clipboard

Using VIPs library and Lua to process pictures

Open hzm199822 opened this issue 5 years ago • 82 comments

To get an original picture from the file, what I want to do is to select a position, cut a rectangle, and make a concave or convex semicircle on the edge of the rectangle, and make this area transparent. Is there an appropriate API or solution?

hzm199822 avatar Nov 02 '20 09:11 hzm199822

Hi @hzm199822, could you post a sample image showing what you want?

jcupitt avatar Nov 03 '20 13:11 jcupitt

For example, this:

#!/usr/bin/luajit

local vips = require "vips"

local im = vips.Image.new_from_file(arg[1], {access = "sequential"})

-- make a transparent square ... just RGBA all 0 (black)
local square = vips.Image.black(64, 64, {bands = 4})

-- insert into the main image
im = im:insert(square, 100, 100)

im:write_to_file(arg[2])

makes a square in an image transparent, eg.:

$ ./try320.lua ~/pics/lion.png x.png

x

Where do you need the semicircle? Do you need a line, or a segment?

jcupitt avatar Nov 03 '20 13:11 jcupitt

Uploading 1111.png…

hzm199822 avatar Nov 04 '20 01:11 hzm199822

What I want is not all white, but translucency. Or that piece becomes a watermark

hzm199822 avatar Nov 04 '20 01:11 hzm199822

What I want is that this square is just a benchmark. I want to add a semicircle or subtract a semicircle on any two sides of the four sides of the square. Of course, the diameter of the semicircle must be smaller than the side length

hzm199822 avatar Nov 04 '20 02:11 hzm199822

What I want is that this square is just a benchmark. I want to add a semicircle or subtract a semicircle on any three sides of the four sides of the square. Of course, the diameter of the semicircle must be smaller than the side length. The translucency on the right side of the image can also be a fuzzy watermark. Then the block on the left side, I hope, is the same as the missing block in the original image, which is equivalent to providing a picture. I need to make two images, one is with watermark, the other is a small piece taken out separately I don't know if luavips can do this for images (add or subtract semicircles on the edges of rectangles)

hzm199822 avatar Nov 04 '20 02:11 hzm199822

The square in the lion only looks white because the page background is white. Try opening the image in a new tab.

You can overlay complex shapes with composite. You give it an RGBA image, one of the PDF blending modes (eg. over) and a position.

Could you upload some test images? It would save me making them. Just drag the images into the text box.

jcupitt avatar Nov 04 '20 07:11 jcupitt

huangzhiming [email protected]

签名由 网易邮箱大师 定制

On 11/4/2020 15:58,John Cupitt[email protected] wrote:

The square in the lion only looks white because the page background is white. Try opening the image in a new tab. You can overlay complex shapes with composite. You give it an RGBA image and one of the PDF blending modes (eg. over) and a position. Could you upload some test images? It would save me making them. Just drag the images into the text box. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

hzm199822 avatar Nov 04 '20 08:11 hzm199822

Composite example:

#!/usr/bin/luajit

vips = require 'vips'

local image = vips.Image.new_from_file(arg[1], {access = "sequential"})
local overlay = vips.Image.new_from_file(arg[2], {access = "sequential"})

-- composite the overlay at 50, 20 using 'over' blend mode
image = image:composite(overlay, "over", {x = 50, y = 20})

image:write_to_file(arg[3])

Generates:

x

jcupitt avatar Nov 04 '20 08:11 jcupitt

I mainly want to extract a figure from the original image, then save it, and change the extracted part of the original image into translucent or other

hzm199822 avatar Nov 04 '20 08:11 hzm199822

It's important to extract this step, but it seems to me that the API can only extract simple rectangles

hzm199822 avatar Nov 04 '20 08:11 hzm199822

You can apply masks if you want more complex shapes.

Post a set of sample images and I'll have a go.

jcupitt avatar Nov 04 '20 09:11 jcupitt

like this?

huangzhiming [email protected]

签名由 网易邮箱大师 定制

On 11/4/2020 17:07,John Cupitt[email protected] wrote:

You can apply masks if you want more complex shapes. Post a set of sample images and I'll have a go. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

hzm199822 avatar Nov 04 '20 09:11 hzm199822

Use the github website and drag images into the text box.

jcupitt avatar Nov 04 '20 09:11 jcupitt

Uploading 图片.png…

hzm199822 avatar Nov 04 '20 09:11 hzm199822

You need to wait for the upload to finish before pressing Comment.

jcupitt avatar Nov 04 '20 09:11 jcupitt

图片

hzm199822 avatar Nov 04 '20 09:11 hzm199822

OK, now it's OK. My goal is very simple, that is to get such a shape from the original image rather than just a simple rectangle. After saving this shape, I will do some operations on the original image, such as translucency, or other things. I can now extract rectangles and darken them myself, but I don't know if there is such an API to support my goal.

hzm199822 avatar Nov 04 '20 09:11 hzm199822

Ah, like a jigsaw, I wondered.

Yes, it should be easy. Do you have that shape as a PNG?

jcupitt avatar Nov 04 '20 09:11 jcupitt

8 square_back background

hzm199822 avatar Nov 04 '20 09:11 hzm199822

What I want to do is JPG, the encoding format of JPG is simpler than PNG, and the operation time should be shorter

hzm199822 avatar Nov 04 '20 09:11 hzm199822

I mean, do you have a PNG for the jigsaw piece? It would save me drawing it.

jcupitt avatar Nov 04 '20 10:11 jcupitt

图片

hzm199822 avatar Nov 04 '20 10:11 hzm199822

图片

hzm199822 avatar Nov 04 '20 10:11 hzm199822

like this?

hzm199822 avatar Nov 04 '20 10:11 hzm199822

I found this sample:

jigsaw

Example code:

#!/usr/bin/luajit

vips = require 'vips'

local image = vips.Image.new_from_file(arg[1], {access = "sequential"})
local mask = vips.Image.new_from_file(arg[2], {access = "sequential"})

-- the position we place the piece
left = 200
top = 100

-- get just the alpha from the mask
alpha = mask:extract_band(3)

-- image pixels
rgb = image:crop(left, top, mask:width(), mask:height())

-- brighten 50%
rgb = rgb * 1.5

-- attach the mask as the alpha channel
overlay = rgb .. alpha

-- composite back
image = image:composite(overlay, "over", {x = left, y = top})

image:write_to_file(arg[3])

I can run like this:

$ ./try320.lua ~/pics/Gugg_coloured.jpg ~/pics/jigsaw.png x.jpg

To make this:

x

jcupitt avatar Nov 04 '20 10:11 jcupitt

no...

hzm199822 avatar Nov 04 '20 10:11 hzm199822

What I want to do is to get a jigsaw block from the original image, not a rectangle, because I need this puzzle block to do some operations. This jigsaw block comes from the original picture, and the position obtained from the original image is different each time

hzm199822 avatar Nov 04 '20 10:11 hzm199822

Sorry, I still don't understand what you want to do.

Are you trying to hide or remove the watermark?

jcupitt avatar Nov 04 '20 10:11 jcupitt

4fe01a4819e02fbfce70f3cd0dbbe491

hzm199822 avatar Nov 04 '20 10:11 hzm199822