@@ -37,35 +37,33 @@ def self.extract_multiple_images_from_source(input_source, page_id, polygons)
3737 new_stream = load_input_source_pdf_page_as_stringio ( input_source , page_id )
3838 new_stream . seek ( 0 )
3939
40- extract_images_from_polygons ( input_source , new_stream , page_id , polygons )
40+ extract_images_from_polygons ( input_source , page_id , polygons )
4141 end
4242
4343 # Extracts images from their positions on a file (as polygons).
4444 #
4545 # @param [Input::Source::LocalInputSource] input_source Local input source.
46- # @param [StringIO] pdf_stream Buffer of the PDF.
4746 # @param [Integer] page_id Page ID.
4847 # @param [Array<Geometry::Point, Geometry::Polygon, Geometry::Quadrilateral>] polygons
4948 # @return [Array<Image::ExtractedImage>] Extracted Images.
50- def self . extract_images_from_polygons ( input_source , pdf_stream , page_id , polygons )
49+ def self . extract_images_from_polygons ( input_source , page_id , polygons )
5150 extracted_elements = [ ] # @type var extracted_elements: Array[Image::ExtractedImage]
5251
52+ input_source . io_stream . rewind
53+ pdf_stream = StringIO . new ( input_source . io_stream . read . to_s )
54+ input_source . io_stream . rewind
5355 polygons . each_with_index do |polygon , element_id |
5456 polygon = ImageUtils . normalize_polygon ( polygon )
5557 page_content = ImageUtils . read_page_content ( pdf_stream )
58+ points = [
59+ polygon . top_left ,
60+ polygon . bottom_right ,
61+ polygon . top_right ,
62+ polygon . bottom_left ,
63+ ]
5664
57- min_max_x = Geometry . get_min_max_x ( [
58- polygon . top_left ,
59- polygon . bottom_right ,
60- polygon . top_right ,
61- polygon . bottom_left ,
62- ] )
63- min_max_y = Geometry . get_min_max_y ( [
64- polygon . top_left ,
65- polygon . bottom_right ,
66- polygon . top_right ,
67- polygon . bottom_left ,
68- ] )
65+ min_max_x = Geometry . get_min_max_x ( points )
66+ min_max_y = Geometry . get_min_max_y ( points )
6967 file_extension = ImageUtils . determine_file_extension ( input_source )
7068 cropped_image = ImageUtils . crop_image ( page_content , min_max_x , min_max_y )
7169 if file_extension == 'pdf'
0 commit comments