How to change adaptiveThresholdImage output color in Imagick? - replace

Is it possible to change or replace output colors in Imagick adaptiveThresholdImage?
I use this code: http://phpimagick.com/Imagick/adaptiveThresholdImage
function adaptiveThresholdImage($imagePath, $width, $height, $adaptiveOffset)
{
$imagick = new \Imagick(realpath($imagePath));
$adaptiveOffsetQuantum = intval($adaptiveOffset * \Imagick::getQuantum());
$imagick->adaptiveThresholdImage($width, $height, $adaptiveOffsetQuantum);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
}
I made a picture with PS, what I need via adaptiveThresholdImage:
what I need
Unfortunatelly I can't any informations from output formatting.
Regards

It appears that you want each pixel of the output image to be a at least a certain shade of gray, or the brighter color from the output of adaptiveThresholdImage.
Probably the easiest way to do that is add:
$imagick->evaluateimage(\Imagick::EVALUATE_MAX, 0.5);
To after the call to adaptiveThresholdImage.

Related

PIL - how to insert an index, or subscript, into text?

Like this:
Calculating coordinates looks not so good, maybe there is a better way?
This code works fine (), but it's complicated always calculate where to place index for each letter.
image = Image.new('I', (300, 100), "white").convert('RGBA')
font = ImageFont.truetype(font=r"C:\Windows\Fonts\Arial.ttf", size=39)
draw = ImageDraw.Draw(image, 'RGBA')
draw.text((10, 10), "P", fill="black", font=font, align="center")
font = ImageFont.truetype(font=r"C:\Windows\Fonts\Arial.ttf", size=20)
draw.text((25, 35), "2", fill="black", font=font, align="center")
image.save(output_folder + 'test.png')
One possibility for you might be to use ImageMagick which understands Pango Markup Language - which looks kind of like HTML.
So, at the command-line you could run this:
convert -background white pango:'<span size="49152">Formula: <b>2P<sub><small><small>2</small></small></sub>O<sub><small><small>5</small></small></sub></b></span>' formula.png
which produces this PNG file:
Change to -background none to write on a piece of transparent canvas if you want to preserve whatever is underneath the text in your original image.
You can also put all the markup in a separate text file, called say "pango.txt" like this:
<span size="49152">Formula: <b>2P<sub><small><small>2</small></small></sub>O<sub><small><small>5</small></small></sub></b></span>
and pass that into ImageMagick like this:
convert pango:#pango.txt result.png
You could shell out and do this using:
subprocess.call()
Then you can easily load the resultant image and composite/paste it in where you want it - that would take about 3 lines of Python that you could put in a function.
Here is a further example of an image generated with Pango by Anthony Thyssen so you can see some of the possibilities:
There is loads of further information on Pango by Anthony here.
Note that there are also Python bindings for ImageMagick but I am not very familiar with them, but that may be cleaner than shelling out.
Keywords: Pango, PIL, Pillow, Python, markup, subscript, superscript, formula, chemical formulae, ImageMagick, image, image processing, SGML, HTML.
You can also do this sort of thing using Mathtext in Matplotlib:
#!/usr/bin/env python3
import matplotlib.pyplot as plt
plt.axes([0.025, 0.025, 0.95, 0.95])
# Some formula with superscripts, subscripts, square roots, fractions and integrals
eq = r"$ 2P_2 O_5 + H^{2j}$"
size = 50
x,y = 0.5, 0.5
alpha = 1
params = {'mathtext.default': 'regular' }
plt.rcParams.update(params)
plt.text(x, y, eq, ha='center', va='center', color="#11557c", alpha=alpha,
transform=plt.gca().transAxes, fontsize=size, clip_on=True)
# Suppress ticks
plt.xticks(())
plt.yticks(())
# Save on transparent background
plt.savefig('result.png', transparent=True)
You can also save the output in a memory buffer (without going to disk) and then use that in your PIL-based image processing.
Note that I have explicitly named and assigned all the parameters (x, y, size and alpha) so you can play with them and that makes the code look longer and more complicated than it actually is.
Keywords: Python, PIL, Pillow, maths, mathematical symbols, formula with superscripts, subscripts, square roots, fractions and integrals.

Swift Imageview Circular

I am trying to get my profile picture to display as a circular view using swift 3. This is my code:
self.view.layoutIfNeeded()
self.profileImageView.image = image
self.profileImageView.layer.cornerRadius = self.profileImageView.frame.width/2.0
self.profileImageView.clipsToBounds = true
self.profileImageView.layer.masksToBounds = true
It works well on square images. But once the image is not square this doesn't display the image as circular. What do I need to do in order to get it to be display the imageview as a circle? Or is this feature only limited to square images?
Your code is making the corner radius half the width. This works fine when height == width (so radius also == height/2), but otherwise it won't work.
To fix this, add constraints to make your profileImageView square, then set the profileImageView.contentMode = .aspectFill.
Add self.view.layoutIfNeeded() line before you are set corner radius.
self.view.layoutIfNeeded()
self.profileImageView.layer.cornerRadius = self.profileImageView.frame.width/2.0
self.profileImageView.clipsToBounds = true

Text Wrapping in Cocos2d-x

I am trying to get my text labels to automatically resize if the text is longer than the box they are in. I also want it to support multi-line functionality. I have done some searching online and saw that it used to work something like this:
CCLabelTTF::labelWithString(“This is a sentence longer than a line width.2d-x”, CGSizeMake(**0, 0**), UITextAlignmentCenter, “Thonburi”, 20);
but that seems to no longer be available in cocos so I am not sure what to do.. Right now I have my label set up as follows:
myQuestion = Label::createWithTTF("Testing to see if text wrap will work" ,c_strFontNameBase, 50);
myQuestion->setPosition(boxLabel->getContentSize().width/2, boxLabel->getContentSize().height/2);
boxLabel->addChild(myQuestion, 50);
Is there some way I can use a similar way to the top example to make mine work? This doesn't seem like it should be very hard thing to do but I am finding a lack of documentation on it online...
I believe, you can only make one dimension of the label auto-resizable i.e. either width or height can be auto-resizable. By default when you create label as follows, width of the labels are set to resize automatically with fixed height:
auto label = Label::createWithTTF("Hello World gsdhsgdh gshdghsg yutywe gdgshdgy bnbjh hshhashgy hjnbdnsdhh ghhsgdhg ghghghsd ghhghsd ghghghgsd jkjkhsdjkj ououisdusydsi kkjkxncmxcjh kcxhjxhcjx jkuiushjxchxjch hjhjchxuyuychjc ", "fonts/Marker Felt.ttf", 24);
// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
// add the label as a child to this layer
this->addChild(label, 1);
But if you want multi-line support i.e. fixed width and resizable height you just need to set dimension of the label with fixed width and zero height as:
label->setDimensions(300, 0);
I hope it will help.

How to insert an image from file into a PlotWidget (plt1 = pg.PlotWidget(w);)?

How to insert an image from file into a PlotWidget (plt1 = pg.PlotWidget(w);)?
The image is for a layout on which some calculated and plotted points should appear.
I tried to insert the image in a Qlabel behind the PlotWidget and make the PlotWidget transparent, but didn't work as transparency takes the color of the window not real transparency.
Thanks
Use QGraphicsPixmapItem:
plt1 = pg.PlotWidget(w)
img = pg.QtGui.QGraphicsPixmapItem(pg.QtGui.QPixmap(fileName))
plt1.addItem(img)
# depending on your preference, you probably want to invert the image:
img.scale(1, -1)
# OR invert the entire view:
plt.invertY(True)

Is there a way to crop an image with Raphael.js?

I am trying to fit part of in image into a raphael object.
Scaling the image works perfectly, but when I try to translate it, it ends up returning the wrong part of the image.
I am scalling the image using "S1.5,1.5,0,0", that is, I am not scalling it around the middle point, so scalling it works beautifully.
But, as I try to offset the image, the resulting image fragment is offset.
Maybe there's another way to do it in Raphael.
What I am trying to accomplish is use a fragment of an image as an image object in Raphael and I need to copy a rectangle from an external image into it.
Something like:
copy original image fragment (x0 = 100, y0 = 120, width = 300, height = 250) to the image object, which has dimensions (width = 150 and 125).
I have been looking for an answer for some time, but nothing that really helps.
Edit:
The fiddle is
/w9XSf/12/
In the example above, I am grabbing a 100 x 60px area from the original image (which is 612 x 325px), and trying to display it on the output image, which is 500 x 300px.
The scale works, but the area it is grabbing is not the one I need.
It does work, if I grab from 0, 0.
But, as I move from the top left corner of the originsl image, the actual area it gives me is farther away from what I actually need :(.
Any ideas? (I have already tried swapping the order of the T and the S in the transform string).
Thanks.
Using Raphael, the following code creates a container, to be used to display an image, duly translated and scaled. A live version of the solution is also available at http://jsfiddle.net/s6DHf/. This is a forked version of the actual problem.
var outputW = 525,
outputH = 300;
sourceX = 100,
sourceY = 100,
scaleX = 1.5,
scaleY = 1.5,
paper = new Raphael("image", outputW, outputH),
bgImg = paper.image("http://cdn3.whatculture.com/wp-content/uploads/2013/04/MAN-OF-STEEL-e1365755036183.jpg", 0, 0, 350, 200)
.transform("t" + sourceX + "," + sourceY + "s" + scaleX +","+ scaleY + ",0,0");
Check the use of "s" and "t" (in lowercase), which denotes relative scaling and relative translation, respectively. The problem was due to the use of "S" and "T" (in uppercase), which is all about absolute scaling and translation, respectively.
Raphael reference: http://raphaeljs.com/reference.html#Element.transform
Hope this helps.