@@ -583,45 +583,33 @@ def write_to_buffer(self, format_string, **kwargs):
583
583
"""
584
584
format_string = _to_bytes (format_string )
585
585
586
+ filename = vips_lib .vips_filename_get_filename (format_string )
587
+
586
588
pointer = vips_lib .vips_filename_get_options (format_string )
587
589
options = _to_string_copy (pointer )
588
590
589
- pointer = vips_lib .vips_foreign_find_save_buffer (format_string )
590
- if pointer == ffi .NULL :
591
- raise Error ('unable to write to buffer' )
592
- name = _to_string (pointer )
593
-
594
- return pyvips .Operation .call (name , self ,
595
- string_options = options , ** kwargs )
596
-
597
- def write_to_memory (self ):
598
- """Write the image to a large memory array.
599
-
600
- A large area of memory is allocated, the image is rendered to that
601
- memory array, and the array is returned as a buffer.
602
-
603
- For example, if you have a 2x2 uchar image containing the bytes 1, 2,
604
- 3, 4, read left-to-right, top-to-bottom, then::
605
-
606
- buf = image.write_to_memory()
607
-
608
- will return a four byte buffer containing the values 1, 2, 3, 4.
609
-
610
- Returns:
611
- buffer
612
-
613
- Raises:
614
- :class:`.Error`
615
-
616
- """
591
+ pointer = ffi .NULL
592
+ if at_least_libvips (8 , 9 ):
593
+ vips_lib .vips_error_freeze ()
594
+ pointer = vips_lib .vips_foreign_find_save_target (filename )
595
+ vips_lib .vips_error_thaw ()
596
+
597
+ if pointer != ffi .NULL :
598
+ name = _to_string (pointer )
599
+ target = pyvips .Target .new_to_memory ()
600
+ pyvips .Operation .call (name , self , target ,
601
+ string_options = options , ** kwargs )
602
+ buffer = target .get ("blob" )
603
+ else :
604
+ pointer = vips_lib .vips_foreign_find_save_buffer (filename )
605
+ if pointer == ffi .NULL :
606
+ raise Error ('unable to write to buffer' )
617
607
618
- psize = ffi .new ('size_t *' )
619
- pointer = vips_lib .vips_image_write_to_memory (self .pointer , psize )
620
- if pointer == ffi .NULL :
621
- raise Error ('unable to write to memory' )
622
- pointer = ffi .gc (pointer , glib_lib .g_free )
608
+ name = _to_string (pointer )
609
+ buffer = pyvips .Operation .call (name , self ,
610
+ string_options = options , ** kwargs )
623
611
624
- return ffi . buffer ( pointer , psize [ 0 ])
612
+ return buffer
625
613
626
614
def write_to_target (self , target , format_string , ** kwargs ):
627
615
"""Write an image to a target.
@@ -672,6 +660,35 @@ def write_to_target(self, target, format_string, **kwargs):
672
660
return pyvips .Operation .call (name , self , target ,
673
661
string_options = options , ** kwargs )
674
662
663
+ def write_to_memory (self ):
664
+ """Write the image to a large memory array.
665
+
666
+ A large area of memory is allocated, the image is rendered to that
667
+ memory array, and the array is returned as a buffer.
668
+
669
+ For example, if you have a 2x2 uchar image containing the bytes 1, 2,
670
+ 3, 4, read left-to-right, top-to-bottom, then::
671
+
672
+ buf = image.write_to_memory()
673
+
674
+ will return a four byte buffer containing the values 1, 2, 3, 4.
675
+
676
+ Returns:
677
+ buffer
678
+
679
+ Raises:
680
+ :class:`.Error`
681
+
682
+ """
683
+
684
+ psize = ffi .new ('size_t *' )
685
+ pointer = vips_lib .vips_image_write_to_memory (self .pointer , psize )
686
+ if pointer == ffi .NULL :
687
+ raise Error ('unable to write to memory' )
688
+ pointer = ffi .gc (pointer , glib_lib .g_free )
689
+
690
+ return ffi .buffer (pointer , psize [0 ])
691
+
675
692
def write (self , other ):
676
693
"""Write an image to another image.
677
694
0 commit comments