6
$\begingroup$

I have manged to turn an image into a RawBoxes expression. How do I turn in back into something I can use in Graphics? In particular, I want to rotate it.

Here is a minimum working example of how I messed up. Starting with

Graphics[{Green, Triangle[]}]

bitmap

I copied as bitmap into PowerPoint. Later I coped from PowerPoint into Mathematica using

a = paste;

Where "paste" is where I pasted the image.
If I do

Head[a]

it returns: RawBoxes

Expressions like

Graphics[Rotate[a, π]]

Do not work (no surprise). How do I convert an expression with Head RawBoxes back into a graphics that I can use again? Thanks

$\endgroup$
4
  • 1
    $\begingroup$ When you converted into a bitmap you lost resolution independent Graphics quality. You can't recover Graphics object from that. At most, you may convert your RawBoxes into an Image object $\endgroup$
    – Somos
    Commented Oct 13, 2020 at 22:13
  • $\begingroup$ Thanks for this warning. I am aware that I will have to live with a bitmap. My target is to perform operations like rotating and resizing. $\endgroup$
    – Hugh
    Commented Oct 14, 2020 at 5:44
  • $\begingroup$ You write "operations like rotating and resizing" which is exactly what Graphics objects are capable of but not bitmapped Image objects. I strongly advise to stay away from bitmaps as long as possible and only for rasterization for viewing pusposes. $\endgroup$
    – Somos
    Commented Oct 14, 2020 at 13:26
  • $\begingroup$ @Somos Of course I agree with you that working with Graphics objects is best. However, if you have got into the position of having RawBoxs perhaps because the image was never created in Mathematica what should you do? May I also point out that if you can convert to an Image then there is ImageRotate and ImageResize etc that enables you to to the same operation on images as you can on Graphics.. $\endgroup$
    – Hugh
    Commented Oct 14, 2020 at 21:11

2 Answers 2

9
$\begingroup$

You can use ToExpression to turn a box expression into a normal expression. However, ToExpression doesn't work with RawBoxes, Cell or BoxData. So, the following might work for you:

rawBoxesToExpression[RawBoxes[a_]] := rawBoxesToExpression[a]
rawBoxesToExpression[Cell[BoxData[b_], ___]] := rawBoxesToExpression[b]
rawBoxesToExpression[a_] := ToExpression[a]

For example:

rawBoxesToExpression[
    RawBoxes[Cell[BoxData@GraphicsBox[{RectangleBox[{0,0}],CircleBox[{0,0}]}]]]
]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank you. However, it does not work in my case. Following your approach I have found that the images are contained in RawBoxes[Cell[GraphhicsData[Metafile, CF5dJ6E]HGAYHf4PEfU^I6mgLb....]]]' Can this be unpacked to make an Image`? $\endgroup$
    – Hugh
    Commented Oct 14, 2020 at 21:00
  • $\begingroup$ @Hugh Possibly, but I wold need the actual RawBoxes expression to play around with to find out. $\endgroup$
    – Carl Woll
    Commented Oct 14, 2020 at 21:10
  • $\begingroup$ I have been playing around more and I think I have found a solution. What I need to do is to Rasterisze the image and then I have an Image object. This can be worked with using ImageRotate , `ImageResize etc. I will post that as a solution. $\endgroup$
    – Hugh
    Commented Oct 14, 2020 at 21:15
1
$\begingroup$

Thanks to those that looked at this question and tried to find a solution. I have been working at the problem and found that I can Rasterize the RawBoxes to give me an Image. I can now do ImageResize and ImageRotate

Example:

Continuing with the item that has been pasted and is called a from original post.

b = Rasterize[a];
ImageCompose[ImageRotate[b, π/2], ImageResize[b, 100]]

use of ImageCompose

Hope that helps anyone who ends up with an item that has Head RawBoxes.

$\endgroup$

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.