TransWikia.com

How to import and cut n images in 6 parts hopefully proportional to the written and export them to a directory?

Mathematica Asked by juan muñoz on June 8, 2021

I tried to get it well formatted but I won it

I hope the question is understood
I’m using the google form which allows you to do multiple choice tests, if they were language no problem, the problem they are math exercises and the only way they look decent is by inserting pictures both in the problem header with in the alternatives, but cutting each picture of each problem means 6 cuts and if they are 30 questions is a long time.
The idea is to have "x" amount of images in a directory and that mathematician import them one by one P01.png,P02.png…. and identify the black color of the writing and cut the image in 6 parts if you can of the same size of the writing and name them of the form p01. png, p01_a.png,p01_b.png,p01_c.png ,p01_d.png,p01_e.png,p02.png, p02_a.png,p02_b.png,p02_c.png ,p02_d.png,p02_e.png……. and save them in a subdirectory "y" inside "x
Someone could help me please

Image from P01.png
Image from P01.png

 Image from p01.png
Image from p01.png

Image from p01_a.png
Image from p01_a.png

  Image from p01_b.png
Image from p01_b.png

Image from p01_c.png
Image from p01_c.png]

Image from p01_d.png
Image from p01_d.png

Image from p01_d.png
Image from p01_e.png

One Answer

Update 2

This is a better implementation of the parts function that does not assume that the spacings are the same. It finds groups of rows in the image that are not composed of entirely white pixels and splits by those groups.

ClearAll@parts

parts[image_] := Module[{width, height, imageData},
  {width, height} = ImageDimensions@image;
  imageData = ColorConvert[image, "Grayscale"] // ImageData;

  imageData // 
  Position[#, ConstantArray[1., width]] & //       
  Flatten //
  Complement[Range[height], #] & //     
  Split[#, (#2 - #1 == 1 &)] & //
  Map[MinMax] //   
  Map[Take[imageData, #] & /* Image]]

The rest of the code in the update below is unchanged.

Update

To process a directory of images

importPath = "path to import directory";
exportPath = "path to export directory"; (* Must already exist *)

ClearAll[parts, exporter];

parts[image_] := ImagePartition[image, Scaled[{1, 1/6}]] // Flatten

exporter[baseName_, parts_] := 
 MapThread[
  Export[baseName <> "_" <> #1 <> ".png", #2] &, {{"a", "b", "c", "d", "e", "f"}, parts}]

importedImages = AssociationMap[Import, FileNames["*.png", importPath]];

importedImages // 
  KeyValueMap[exporter[FileNameJoin[{exportPath, FileBaseName@#1}], parts[#2]] &];

If the spacing between the text is the same then you can partition it

img = Import["~/Downloads/21ttc.png"];
parts = ImagePartition[img, Scaled[{1, 1/6}]] // Flatten

enter image description here

Each image in the list can be exported to a different file.

MapThread[Export["P01_" <> #1 <> ".png", #2] &, {{"a", "b", "c", "d", "e", "f"}, parts}]

Correct answer by Rohit Namjoshi on June 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP