Message Boards Message Boards

[NEW-11.2] Torn edge paper effect with semi-transparent drop-down shadow


Some time ago Vitaliy Kaurov asked an interesting question about implementing in Mathematica so called "torn edge" effect in context of conveying a message perfectly formulated by Sjoerd C. de Vries: "There's more of this, but that's not important." As an answer Heike (a Mathematica.StackExchange community member) published an ingenious implementation which deservedly received a huge amount of upvotes. She not only implemented a "torn edge" effect but also provided an option to add a drop-down shadow in order to dramatize the effect. Here is what her function named torn generates from the "Mandrill" example image in Mathematica 8.0.4 (it doesn't work correctly in the recent versions due to an incompatible change in image processing functionality):

img = ExampleData[{"TestImage", "Mandrill"}];

torn[img, {{0, 1}, {1, 0}}, "offset" -> {20, 20}, "gaussianBlur" -> 10]


As Vitaliy recently pointed out, with the release of Mathematica 11.2 we've got a "TornFrame" image effect immediately available via ImageEffect function:

ImageEffect[img, {"TornFrame", Scaled[1/15], {Right, Bottom}, .05}]


But people at StackExchange quickly noticed that this effect doesn't produce a sufficiently irregular ripped-out edge, what means that the message that the image is truncated and "there is more of this, but that's not important" isn't exactly obvious (as opposed to the implementation provided by Heike). By spelunking the evaluation of ImageEffect with Trace I've found a simple hack allowing to generate a "Heike-style" torn edge effect:

tornEdge = Block[{Accumulate = RandomReal[1, Length[#]] &}, 
  ImageEffect[img, {"TornFrame", Scaled[1/15], {Right, Bottom}, .08}]]


Further investigation showed that using new "Frame" effect we can produce even more irregular torn edge using only documented functionality, but at the cost of sufficiently more lengthy code:

tornEdge2 = Module[{step = 10, if, n = 2 Total[ImageDimensions[img]], k = 0},
  if = Interpolation[
    Transpose[{Accumulate[Prepend[RandomInteger[{step, 2 step}, n], 0]], 
      RandomReal[1, n + 1]}],
    InterpolationOrder -> 1];
  ImageEffect[img, {"Frame", if[++k] &, 15, {Right, Bottom}}]]


Here is a simple approach allowing to obtain a semi-transparent shadow based on the new functionality. In addition I've added an option to add a darkened one-pixel wide boundary to the torned image:

shadowOffset = 10;
shadowBlur = 5;
shadowTone = .5;
boundaryLigntness = .4;

 SetAlphaChannel[ColorNegate@#, #] &@
      tornEdge], {{shadowOffset, shadowBlur/2}, {shadowBlur/2, shadowOffset}}], 
    shadowTone], shadowBlur], 
    ImageMultiply[MorphologicalPerimeter@AlphaChannel[tornEdge], boundaryLigntness]], {{0,
     shadowOffset + shadowBlur/2}, {shadowOffset + shadowBlur/2, 0}}]]


Any suggestions and comments are welcome!

POSTED BY: Alexey Popkov
27 days ago

Heike is a female? I always read it as a male name, but can also be a female name now that I think about it…

POSTED BY: Sander Huisman
27 days ago

Dear Sander,

that is an interesting computational problem that Mathematica can readily solve:

![enter image description here][1]


![enter image description here][2]

depending on your assumptions.


Unfortunately MMA does not appear to know the naming frequency:

![enter image description here][3]

Luckily W|A knows more:

WolframAlpha["heike", {{"CurrentEstimates:GivenNameData", 1}, "Content"}, 
InputAssumptions -> {"*C.heike-_*GivenName-", "*DPClash.GivenNameE.heike-_**Heike.UnitedStates.female--"}]


enter image description here

WolframAlpha["heike", {{"BasicInformation:GivenNameData", 1}, "Content"}, 
 InputAssumptions -> {"*C.heike-_*GivenName-", "*DPClash.GivenNameE.heike-_**Heike.UnitedStates.male--"}]

enter image description here

So in the US it's more likely female.

Sorry, too much time to waste...


PS: I am aware that this in the US version of the name. In Germany it would also be female though.

POSTED BY: Marco Thiel
27 days ago

That put a smile on my face! Nice!

In Dutch it can be both genders, but more likely Female. Though in Frisian I think I've heard it more for men. In German indeed female as far as I know…


POSTED BY: Sander Huisman
27 days ago

I also tried to use Mathematica for determining the gender of names automatically, and I was quite disappointed ...

enter image description here

Of course, there may be some weird people somewhere (the US is always a prime suspect) who are calling their sons Felicia, but it's quite absurd to return "male" by default. I don't like the WolframAlpha workaround because it does not even guarantee an interpretation as a name, and it is much too slow for practical work (e.g. a list of 100 names).

This is from the IGraph/M documentation:

enter image description here

POSTED BY: Szabolcs Horvát
26 days ago

Ok, I guess that we are really off-topic, but I yesterday created this function:

determineGender[str_] := 
 If[StringContainsQ[#[[2]], "error"], 
    "Name not found!", 
      {"gender" -> StringSplit[#[[-5 ;; -3]][[1]], {"<gender>", "</gender>"}][[2]],
      "male" -> StringSplit[#[[-5 ;; -3]][[2]], {"<male>", "</male>"}][[2]] , 
     "female" -> StringSplit[#[[-5 ;; -3]][[3]], {"<female>", "</female>"}][[2]]}] & 
      @(URLExecute["" <> str, "Data"])




{"gender" -> "unisex name (can be any gender)", "male" -> "true", "female" -> "true"}




{"gender" -> "male first name", "male" -> "true", "female" -> "false"}




{"gender" -> "female first name", "male" -> "false", "female" -> "true"}



POSTED BY: Marco Thiel
26 days ago

enter image description here - Congratulations! This post is now a Staff Pick as distinguished by a badge on your profile! Thank you, keep it coming!

POSTED BY: Moderation Team
27 days ago

Group Abstract Group Abstract