1
$\begingroup$

Im learning Mathematica and the Wolfram language for shot time by myself. Its a pain for me that many settings are somehow hidden. Now I spent hours to find solution for something I already did with success, but now I can not remeber. When I disassemble a scanned text by use of TextRecognize up to the level of Character (but the same is for Word) then the cropped sub-images of individual letters have different (automatic) size. For me this is nonsense to see a dot in simillar size like character let say "a". I would like to get the cropped images of letters in original size or in same size. Here example of code:

image = Import["https://i.sstatic.net/8Vb9o.png"];

enter image description here

dsChars = TextRecognize[image, "Character", {"Image", "Text"}] // Dataset

Which brings this result:

resulting_dataset

I would like to see the characters in the first collumn being of simillar size - for example the "e" in 5th row to be same height like the "m" in 6th row ... I will appreciate your time and effort for any advice and help.

EDIT 16.5.18 18:18 CEST For to show what I mean when I say that I want to see the sub-images in proportion, here is a sample what I can do with a list of sub-images:

Image[Normal[dsChars[{1, 2}, 1]], Magnification -> 1]

images_in_proportion here the "M" is in proportion to "a"

However how to change appearance of the sub-images in the first collumn of the "printout" of the dataset dsChars ?

EDIT 2 17.5.18, 17:24 CEST I edited the title - removed unnecessary last part " /and potentially for one column of Dataset / " of the title which was too long

$\endgroup$
4
  • $\begingroup$ Have you checked ImageResize? It would help us help you to have a complete minimal example to work on. $\endgroup$
    – anderstood
    Commented May 16, 2018 at 14:59
  • $\begingroup$ anderstood - ImageResize for the example "scanned_text"? That image is only 463x286 pix. I dont know why the image didnt show. Here is the link which the editor created in the question source: i.sstatic.net/8Vb9o.png $\endgroup$
    – CJoe
    Commented May 16, 2018 at 15:07
  • $\begingroup$ No, for the images that you want to have similar sizes. I'll post an answer. $\endgroup$
    – anderstood
    Commented May 16, 2018 at 15:21
  • $\begingroup$ @anderstood - thank you for editing my question for the input image to be seen; Im totally inexperienced with the interface here on the forum. Thank you. $\endgroup$
    – CJoe
    Commented May 16, 2018 at 16:33

4 Answers 4

3
$\begingroup$

You can simply set ImageSizeMultipliers -> 1 for the Dataset:

Style[Dataset[dsChars2], ImageSizeMultipliers -> 1]

screenshot

$\endgroup$
6
  • $\begingroup$ oh great! that was what I already found somwhere months ago, but wasnt able to find it again; perfect! $\endgroup$
    – CJoe
    Commented May 17, 2018 at 11:12
  • $\begingroup$ again - unfortunately I cannot mark your answer as usefull as the system tells me I dont have enough reputation points; can somebody do it for me on Aleksey Popkov and SquareOne answers, please? $\endgroup$
    – CJoe
    Commented May 17, 2018 at 11:15
  • 1
    $\begingroup$ Aleksey, I tried it now but it does not work on my Raspberry PI3 Mathematica (v. 11.2.0.0) - result of the Style is equal to the previous appearance of the dataset :( ; but Im sure something like that should work $\endgroup$
    – CJoe
    Commented May 17, 2018 at 11:34
  • $\begingroup$ @CJoe It dosen't work for me either (v11.2 or 11.3 with OSX) ... $\endgroup$
    – SquareOne
    Commented May 17, 2018 at 13:01
  • $\begingroup$ @CJoe Please try Style[Dataset[dsChars2], GraphicsBoxOptions -> {BaseStyle -> Magnification -> 1}]. $\endgroup$ Commented May 17, 2018 at 13:39
3
$\begingroup$

In the meantime I found quite acceptable answer (solution 1) to my question and SquareOne tuned it to two additional, perfect answers (solution 2 and 3).

For this case:

image = Import["https://i.sstatic.net/8Vb9o.png"];
dsChars = TextRecognize[image, "Character", {"Image", "Text"}]

my solution is (solution 1):

Transpose[{Image[dsChars[[All, 1]], Magnification -> 1], dsChars[[All, 2]]}] // Dataset

But @SquareOne simplified this solution very nice to (solution 2):

dsChars /. x_Image :> Image[x, Magnification -> 1]// Dataset

which seems to me much better solution as it does not need Transpositions.

Also @SquareOne draw my attention to the fact that if the "image" which I let TextRecognized was already provided with property of Magnification->1, then the resulting sub-images of individual characters will come out from the Textrecognize properly.

Therefore this is an equally perfect solution too (solution 3):

image = Image[Import["https://i.sstatic.net/8Vb9o.png"], Magnification -> 1];
dsChars = TextRecognize[image, "Character", {"Image", "Text"}]// Dataset

All those solutions are giving following, for me perfectly reasonable output: enter image description here

If @SquareOne created the Solution 2 and Solution 3 as his separate answers, I would be happy marking them both as perfect answers for my question.

$\endgroup$
2
$\begingroup$

If I understand well, maybe this:

given

image = Import["https://i.sstatic.net/8Vb9o.png"]
dsChars = TextRecognize[image, "Character", {"Image", "Text"}]

then

 MapAt[ImagePad[#, 
    Transpose@
     Through[{Floor, Ceiling}[({30, 30} - ImageDimensions[#])/2]]] &, 
  dsChars, {All, 1}] // Dataset

Blockquote

$\endgroup$
9
  • $\begingroup$ yes, such appearance of sub-images I was searching for $\endgroup$
    – CJoe
    Commented May 17, 2018 at 10:42
  • 1
    $\begingroup$ In the meantime I have found following solution: Transpose[{Image[dsChars[[All, 1]], Magnification -> 1],dsChars[[All, 2]]}] // Dataset , but both your and mine solutions seem to me too complicated; there must be something to say directly to TextRecognize how the result should be presented or to say to the dataset table (printout) what properties should have the images in first row. $\endgroup$
    – CJoe
    Commented May 17, 2018 at 10:48
  • $\begingroup$ anyway thank you SquareOne $\endgroup$
    – CJoe
    Commented May 17, 2018 at 10:50
  • $\begingroup$ unfortunately I cannot mark your answer as usefull as it tells me that I dont have enough reputation point $\endgroup$
    – CJoe
    Commented May 17, 2018 at 10:57
  • $\begingroup$ @CJoe You actually found the solution ! In Image the option Magnification is set to Automatic by default. You should self answer to your own post (it's OK to do so). A more simple way to do it : dsChars /. x_Image :> Image[x, Magnification -> 1] $\endgroup$
    – SquareOne
    Commented May 17, 2018 at 12:26
0
$\begingroup$
image = Import["https://i.sstatic.net/8Vb9o.png"]
dsChars = TextRecognize[image, "Character", {"Image", "Text"}]
dsChars2 = Transpose[{ImageResize[#, {20}] & /@ dsChars[[All, 1]], 
dsChars[[All, 2]]}] // Dataset

enter image description here

This forces each thumbnail to have a maximal width or height of at most 20 pixels. You could also want to set a height of 20 pixels for each e.g.

$\endgroup$
9
  • $\begingroup$ thank you anderstood, but I need the characters to be simillar in meaning like proportional or how to say it - capital "M" should be higher than "a" not the same height as in your result. And I think ImageResize changes the real size of those images. I only need to see the sub-images all in same "resolution", And as I understand those really are in the origiinal resolution. Only they are presented n the dataset table in a strange "automatic" sizes. $\endgroup$
    – CJoe
    Commented May 16, 2018 at 15:48
  • $\begingroup$ Im sure there is some properties setting or what to change appearance of the images in the dataset table, in past I already suceeded to have same sizes of the characters there. But I cannot find this setting again. I also succeeded to change appearance of the character images in a list of them -> Image[TextRecognize[image, "Word", {"Image"}], Magnification -> 1] but Im not able to do this with a list of lists like the dataset above. $\endgroup$
    – CJoe
    Commented May 16, 2018 at 15:48
  • $\begingroup$ Mmh, I see. You can check that TextRecognize actually changes the dimensions of the images, that has nothing to do with Dataset: dsChars = TextRecognize[image, "Character", {"Image", "Text"}] then ImageDimensions /@ dsChars[[All, 1]]. $\endgroup$
    – anderstood
    Commented May 16, 2018 at 15:58
  • $\begingroup$ You can get the bounding boxes with: dsChars = TextRecognize[image, "Character", {"Image", "Text", "BoundingBox"}]. You can check that the boxes have different heights: Show[image, Graphics@dsChars[[All, 3]]]. So, would you like to change the rectangles so that the have all the same heights, and then extract the corresponding images? $\endgroup$
    – anderstood
    Commented May 16, 2018 at 16:01
  • $\begingroup$ yes, sure the boxes have different sizes because for example capital "M" takes more space than small "a". I dont want to lose these real dimensions and I want to see the sub-images in proportions like they were cutted from the original image. $\endgroup$
    – CJoe
    Commented May 16, 2018 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.