27
2017
Using ImageMagick with Unity
Using ImageMagick with Unity is now possible with the .NET wrapper (github)
Tutorial: Add watermark to image
– Download required DLL files from https://magick.codeplex.com/releases/view/630360 (I used Magick.NET-7.0.4.400-Q8-x64.zip)
– From the zip file, copy “net20\Magick.NET\Magick.NET-Q8-x64.dll” into your Unity project Assets/Plugins/-folder
– From the zip file, copy “net20\Magick.NET\Magick.NET-Q8-x64.Native.dll” into your Unity project root folder (where the project .sln file is also)
– Copy “System.Drawing.dll” (v2.0) from your c: drive into your Unity project Assets/Plugins/-folder (I founded mine from C:\Windows\Microsoft.NET\Framework\v2.0.50727\)
– Download few sample images from Magick.NET examples here and copy those image files into your ‘Assets/Images/’ folder (or adjust the script below to point into correct image path)
– Add the c# script below to your scene and test it!
– See more tutorial sources at Magick github samples directory
example script (c#)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using ImageMagick with unity : http://1darray.com/blog/2017/01/27/using-imagemagick-with-unity/ | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using ImageMagick; | |
public class ImageMagickTest : MonoBehaviour | |
{ | |
void Start() | |
{ | |
CreateWatermark(); | |
} | |
public void CreateWatermark() | |
{ | |
// our image paths | |
var sourcePath = Application.dataPath + "/Images/FujiFilmFinePixS1Pro.jpg"; | |
var watermarkPath = Application.dataPath + "/Images/Snakeware.png"; | |
// Read image that needs a watermark | |
using (MagickImage image = new MagickImage(sourcePath)) | |
{ | |
// Read the watermark that will be put on top of the image | |
using (MagickImage watermark = new MagickImage(watermarkPath)) | |
{ | |
// Draw the watermark in the bottom right corner | |
image.Composite(watermark, Gravity.Southeast, CompositeOperator.Over); | |
// Optionally make the watermark more transparent | |
watermark.Evaluate(Channels.Alpha, EvaluateOperator.Divide, 4); | |
// Or draw the watermark at a specific location | |
image.Composite(watermark, 200, 50, CompositeOperator.Over); | |
} | |
// Save the result | |
image.Write(Application.dataPath + "/Images/" + "FujiFilmFinePixS1Pro-watermark.jpg"); | |
} | |
} | |
} |
Related Posts
13 Comments + Add Comment
Leave a comment
Recent posts
- MotionVector Effect: Object “disappears” when paused
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
Recent Comments
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
Hi, just wondering if I will be able to make an app to publish for Android using this library on Unity?
havent tested, but i’m thinking the native library might not work.. going to try later.
That sounds really fantastic!
But ImageMagick as a common graphic php library is capable of interpretation of most of graphic file formats, in the contrary to php GD library.
Means it should enable to drag the svg, pdf, ai .tif files into Unity and display them as textures. Am I right?
that would be amazing! gotta test that soon..
Thank you for sharing Mgear. 🙂
Hey, you have tested for make an app for Android using this library? and read pdf files for display on textures?
Hello Mika, thanks a lot for this tutorial. I followed your steps and placed both .dll files in the appropriate folders, copied the System.Drawing.dll file into my Assets/Plugins/ folder, attached the ImageMagickTest.cs script to an empty game object and ran the game. I’m not getting any errors or warning messages, but unfortunately nothing happens. There is no 3rd image being created out of the original 2 images. I’ve searched many forums online and can’t seem to find a solution. Do you by any chance have this project in some repository that I could download? I’d love to take a closer look at a working, functional version of this. Thanks for your time!
did you try Refresh unity project window (right click, refresh) ?
i tested on 2018.3.x seems to work directly also. i can try to put the project somewhere later.
Thanks for the response. I realized my issue had actually come from a small detail I missed. When I clicked Build and Run, my selected architecture was incompatible with the .dll, so changing this to the correct one fixed my issue. Cheers.
Works great in editor, and then when I do a standalone windows build I get:
NotSupportedException: IL2CPP does not support marshaling delegates that point to instance methods to native code. The method we’re attempting to marshal is: ImageMagick.StreamWrapper::Write
any idea if there’s a workaround for this? It’s from the ToByteArray method here:
Texture2D newTexture = new Texture2D(2, 2, TextureFormat.ARGB32, false);
newTexture.LoadImage(image.ToByteArray(MagickFormat.Png32));
no idea.. but can you try some of these?
https://github.com/natbro/UnityPlugin/issues/2
https://answers.unity.com/questions/1229036/callbacks-from-c-to-c-are-not-working-in-540f3.html
https://forum.unity.com/threads/making-calls-from-c-to-c-with-il2cpp-instead-of-mono_runtime_invoke.295697/
Has anyone tried to make this multithreaded or use this with the job system? Would love to see an example if you’ve succeeded
pretty sure that it should work (since it doesnt call any unity methods),
could test it by calling CreateWatermark() from regular thread:
https://gist.github.com/unitycoder/8721e5bd6377ac2ffbba4f9b0b2766f0#file-multithreading-cs