Skip to content

vips support gif save now? #1167

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
tczf1128 opened this issue Nov 19, 2018 · 26 comments
Closed

vips support gif save now? #1167

tczf1128 opened this issue Nov 19, 2018 · 26 comments
Labels

Comments

@tczf1128
Copy link

No description provided.

@jcupitt
Copy link
Member

jcupitt commented Nov 19, 2018

Yes. There's now magicksave, and that includes GIF support.

$ vips copy horse.gif[n=-1] x.gif

Makes:

x

@tczf1128
Copy link
Author

tczf1128 commented Nov 19, 2018

use which version? and support crop a gif and insert other images in this gif?

@jcupitt
Copy link
Member

jcupitt commented Nov 19, 2018

It's in 8.7, the current stable version.

Cropping would be harder: you'd need to do some coding for that.

@tczf1128
Copy link
Author

tczf1128 commented Nov 20, 2018

you mean I should do some work before crop gif image? Could you give me some hint?

@jcupitt
Copy link
Member

jcupitt commented Nov 20, 2018

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)

@tczf1128
Copy link
Author

tczf1128 commented Nov 21, 2018

which APIs should I use? I'm still confused about this.

@jcupitt
Copy link
Member

jcupitt commented Nov 21, 2018

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:

$ python cropgif.py ~/pics/horse.gif x.gif 10 10 100 100

To make:

x

@tczf1128
Copy link
Author

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.

@jcupitt
Copy link
Member

jcupitt commented Nov 21, 2018

Sure, just add the text before you save again.

@tczf1128
Copy link
Author

also support add images like png or jpg on this gif?

@jcupitt
Copy link
Member

jcupitt commented Nov 21, 2018

Of course, you can do anything.

@tczf1128
Copy link
Author

ok. I will try it. Thanks

@zhaohuxing
Copy link

@jcupitt May I ask, does libvips support GIF format picture format conversion, crop, resize, and rotation processing?

@jcupitt
Copy link
Member

jcupitt commented May 11, 2020

@zhaohuxing yes, it does.

There's an example of crop a few posts back ^^ #1167 (comment)

@zhaohuxing
Copy link

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://jcupitt.github.io/libvips/2017/03/16/What's-new-in-8.5.html#toilet-roll-images

This link is 404

@jcupitt
Copy link
Member

jcupitt commented May 11, 2020

@sethify
Copy link

sethify commented Feb 1, 2021

@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).

@jcupitt
Copy link
Member

jcupitt commented Feb 2, 2021

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, &copy[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 );
}

@sethify
Copy link

sethify commented Feb 2, 2021

@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.

@jcupitt
Copy link
Member

jcupitt commented Feb 2, 2021

There's a chapter in the docs, though it could be expanded:

https://libvips.github.io/libvips/API/current/using-from-c.html

@sethify
Copy link

sethify commented Feb 3, 2021

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 arrays (showcased above) could use some examples to help people unleash libvips :)

@jcupitt
Copy link
Member

jcupitt commented Feb 3, 2021

You mean the local_array thing? That's documented here:

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.

@sergeevabc
Copy link

sergeevabc commented Jun 18, 2021

$ 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

@jcupitt
Copy link
Member

jcupitt commented Jun 18, 2021

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 -web build. For security this is built without imagemagick support, so there is no gifsave. If you download the -all version it ought to work.

@sergeevabc
Copy link

@jcupitt, I used vips-dev-w64-all-8.11.0.zip, the mentioned conversion does not work.

jcupitt added a commit that referenced this issue Jun 18, 2021
@jcupitt
Copy link
Member

jcupitt commented Jun 18, 2021

It's working for me. I see:

User@WinDev2101Eval MINGW64 ~
$ unzip -qq Downloads/vips-dev-w64-all-8.11.0.zip

User@WinDev2101Eval MINGW64 ~
$ cd vips-dev-8.11/bin/

User@WinDev2101Eval MINGW64 ~/vips-dev-8.11/bin
$ ./vips.exe copy ~/Pictures/before.png x.gif

User@WinDev2101Eval MINGW64 ~/vips-dev-8.11/bin
$ file x.gif
x.gif: GIF image data, version 89a, 2560 x 1440

Please open an issue on https://github.com/libvips/build-win64-mxe if it's not working for you.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants