New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vips support gif save now? #1167
Comments
use which version? and support crop a gif and insert other images in this gif? |
It's in 8.7, the current stable version. Cropping would be harder: you'd need to do some coding for that. |
you mean I should do some work before crop gif image? Could you give me some hint? |
If you load a GIF in libvips it is a tall, thin image. You'll need to cut each frame out, reassemble them, set the page-height, and save. There are some notes here: https://libvips.github.io/libvips/2017/03/16/What's-new-in-8.5.html (update: fixed link) |
which APIs should I use? I'm still confused about this. |
I made a demo for you: import sys
import pyvips
# load all frames from file using n=-1
image = pyvips.Image.new_from_file(sys.argv[1], n=-1)
outfile = sys.argv[2]
left = int(sys.argv[3])
top = int(sys.argv[4])
width = int(sys.argv[5])
height = int(sys.argv[6])
# the image will be a very tall, thin strip, with "page-height" being the height
# of each frame
page_height = image.get("page-height")
n_frames = image.height / page_height
# make a list of new frames
frames = [image.crop(left, i * page_height + top, width, height)
for i in range(0, n_frames)]
# assemble the frames back into a tall, thin image
new_image = pyvips.Image.arrayjoin(frames, across=1)
# set the new page-height ... you must copy() before modifying
# image metadata
new_image.copy().set("page-height", height)
# and save back again
new_image.write_to_file(outfile) I see:
To make: |
Thanks so much! it support insert other images or text in gif. My service is a image merge service,it insert images or texts into one image. |
Sure, just add the text before you save again. |
also support add images like png or jpg on this gif? |
Of course, you can do anything. |
ok. I will try it. Thanks |
@jcupitt May I ask, does |
@zhaohuxing yes, it does. There's an example of crop a few posts back ^^ #1167 (comment) |
This link is 404 |
@jcupitt You wouldn't have any guidance on how attempt the python crop example above using the C api would you? We are not using thumbnail and have working code (on 1 page images) for resize and crop. At current, we can only perform resize on animated images (with a small 1 pixel tracking issue in the vertical, which must be rounding). |
Here you go: /* compile with:
*
* gcc -g -Wall crop-animated.c `pkg-config vips --cflags --libs`
*
*/
#include <vips/vips.h>
static int
crop_animation( VipsObject *context, VipsImage *image, VipsImage **out,
int left, int top, int width, int height )
{
int page_height = vips_image_get_page_height( image );
int n_pages = image->Ysize / page_height;
VipsImage **page = (VipsImage **)
vips_object_local_array( context, n_pages );
VipsImage **copy = (VipsImage **)
vips_object_local_array( context, 1 );
int i;
/* Split the image into cropped frames.
*/
for( i = 0; i < n_pages; i++ )
if( vips_crop( image, &page[i],
left, page_height * i + top, width, height, NULL ) )
return( -1 );
/* Reassemble the frames.
*/
if( vips_arrayjoin( page, ©[0], n_pages, "across", 1, NULL ) )
return( -1 );
/* Set the page height. You must copy before modifying metadata.
*/
if( vips_copy( copy[0], out, NULL ) )
return( -1 );
vips_image_set_int( *out, "page-height", height );
return( 0 );
}
int
main( int argc, char **argv )
{
VipsImage *image;
VipsObject *context;
VipsImage *x;
if( VIPS_INIT( NULL ) )
vips_error_exit( NULL );
if( !(image = vips_image_new_from_file( argv[1],
"access", VIPS_ACCESS_SEQUENTIAL,
NULL )) )
vips_error_exit( NULL );
context = VIPS_OBJECT( vips_image_new() );
if( crop_animation( context, image, &x, 10, 10, 500, 500 ) ) {
g_object_unref( image );
g_object_unref( context );
vips_error_exit( NULL );
}
g_object_unref( image );
g_object_unref( context );
image = x;
if( vips_image_write_to_file( image, argv[2], NULL ) ) {
g_object_unref( image );
vips_error_exit( NULL );
}
g_object_unref( image );
return( 0 );
} |
@jcupitt that is brilliant. Thank you. I've done an insane amount of reading and googling. Your example provided me with more context than anything I've found so far. Is there a source that explains how to use the C API (or just provides solid examples like yours?). Thank you very much. |
There's a chapter in the docs, though it could be expanded: https://libvips.github.io/libvips/API/current/using-from-c.html |
Thank you. The documentation is pretty good. Features like the |
You mean the https://libvips.github.io/libvips/API/current/VipsObject.html#vips-object-local-array But a note in the C intro page would be good, you're right. |
$ vips.exe --version
vips-8.11.0-Thu Jun 3 10:57:25 UTC 2021
$ vips.exe copy in.png out.gif
VipsForeignSave: "out.gif" is not a known file format |
Hello @sergeevabc, Could you open new issues, please? If issues are separated it makes it easier for other users to find answers. I think you've probably downloaded the |
@jcupitt, I used vips-dev-w64-all-8.11.0.zip, the mentioned conversion does not work. |
It's working for me. I see:
Please open an issue on https://github.com/libvips/build-win64-mxe if it's not working for you. |
No description provided.
The text was updated successfully, but these errors were encountered: